In R, create 2D plot (two axes and values) from data frame -
below example of question have:
x <- c(1,1,1,2,2,2,3,3,3) y <- c('a','b','c','a','b','c','a','b','c') z <- c(2.5, 4.5, 6.5, 5.0, 3.0, 7.5, 1.0, 6.5, 2.0) fun <- data.frame( x = x, y = y, z = z )
now have created following data frame called fun
:
x y z 1 1 2.5 2 1 b 4.5 3 1 c 6.5 4 2 5.0 5 2 b 3.0 6 2 c 7.5 7 3 1.0 8 3 b 6.5 9 3 c 2.0
now, want create 2d plot x-axis being (1,2,3), y-axis being (a,b,c), , values (colors) being values of column z
.
is there easy way this? thanks!
your choice in base graphics
fun <- data.frame( x = c(1,1,1,2,2,2,3,3,3), y = c('a','b','c','a','b','c','a','b','c'), z = c(2.5, 4.5, 6.5, 5.0, 3.0, 7.5, 1.0, 6.5, 2.0) ) zz <- with(fun, (z - min(z)) / diff(range(z)) * 998) + 1 par(mar = c(5,4,4,4)) with(fun, plot(x, y, pch = 19, cex = 2, col = colorramppalette(c('black','blue','lightblue'))(1000)[zz])) ## adding color bar plotr::color.bar(c('black','blue','lightblue'), at.x = par('usr')[4] * 1.05, at.y = par('usr')[3]) with(fun, text(x = par('usr')[2] * 1.06, y = pretty(1:3), xpd = na, pos = 4, labels = pretty(fun$z, 3)))
or grid
library('ggplot2') ggplot(data = fun, aes(x = x, y = y, colour = z)) + geom_point(size = 10)
Comments
Post a Comment