2013-04-30 30 views
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行?這不就是情節怎麼樣的差異嗎?r,ggplot2,形狀/顏色。他們有什麼區別?

謝謝

回答

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()(離散色標)的外觀相比,連續的色階。

+0

將連續變量轉換爲因子的替代方法是「cut」。 – cbeleites 2013-04-30 15:51:37

相關問題