以下是我的问题示例:
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
)
现在我创建了以下名为fun
的数据框:
x y z
1 1 a 2.5
2 1 b 4.5
3 1 c 6.5
4 2 a 5.0
5 2 b 3.0
6 2 c 7.5
7 3 a 1.0
8 3 b 6.5
9 3 c 2.0
现在,我想创建一个二维图,x轴为(1,2,3),y轴为(a,b,c),值(颜色)为列{{ 1}}。
有一种简单的方法吗?谢谢!
答案 0 :(得分:1)
您在基本图形中的选择
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 a 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)))
或网格
library('ggplot2')
ggplot(data = fun, aes(x = x, y = y, colour = z)) + geom_point(size = 10)