简体   繁体   中英

r, ggplot2, shape/colour. What is the difference between them?

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

Why is line 1 working but not line 2 ? Isn't it just a difference of how the plots are lookink ?

Thank you

The error message tells you what's wrong:

Error: A continuous variable can not be mapped to shape

shape needs a factor:

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

Also check how ggplot(dat,aes(a,c,colour=factor(d))) + geom_point() (a discrete colour scale) looks compared to the continuous colour scale.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM