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

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

Thank you

Roland
  • 127,288
  • 10
  • 191
  • 288
Remi.b
  • 17,389
  • 28
  • 87
  • 168

1 Answers1

6

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.

Roland
  • 127,288
  • 10
  • 191
  • 288