r、ggplot2、shape/colour的区别是什么?

3
set.seed(123)

library(data.table)
library(ggplot2)

dat=data.table(data.frame(a=rnorm(12),b=rnorm(12),c=rep(c(1,2),6),d=rep(c(1,2,3,4),3)))

ggplot(dat,aes(a,c,colour=d)) + geom_point() # line 1

ggplot(dat,aes(a,c,shape=d)) + geom_point() # line 2

为什么第1行可以工作,而第2行不能?难道这只是图形外观的区别吗?
谢谢
1个回答

6
错误信息告诉你出了什么问题:
Error: A continuous variable can not be mapped to shape

shape需要一个因子:

ggplot(dat,aes(a,c,shape=factor(d))) + geom_point() 

同时也要比较一下使用离散颜色标度的ggplot(dat,aes(a,c,colour=factor(d))) + geom_point()与连续颜色标度的效果。


将连续变量转换为因子的另一种选择是使用 cut 函数。 - cbeleites unhappy with SX

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接