2013-07-05 145 views
2

這裏每個主題麪條的情節是我迄今所做的,結果圖下方是:用不同顏色在GGPLOT2

set.seed(17) 
require(ggplot2) 
x = expand.grid(a=1:5, b=1:5) 
x$c = rnorm(25) 
png('test.png') 
p = ggplot(x, aes(a, c, group=b)) + geom_line() 
print(p) 
dev.off() 
savehistory() 

enter image description here

的目標是讓每一行是以不同的顏色顯示。

+0

將'group = b'更改爲'color = b'。 – Arun

+0

@阿倫抱歉,那是不對的。它不會將數據點分組成行,而是將它們繪製爲一行。 – qed

+4

不是。你的'b'應該是一個'factor'。嘗試'顏色=因子(b)' – Arun

回答

3

使用ggplot2評論給你正確的答案:

你應該強迫B到A的因素,將其作爲顏色AES。

使用lattice沒有必要強迫b鍵因素:

library(lattice) 
xyplot(c~a,data =x,groups=b,type='l') 

enter image description here

或者用latticeExtra獲得ggplot2主題:

library(latticeExtra) 
xyplot(c~a,data =x,groups=b,type='l', 
     par.settings = ggplot2like(),axis=axis.grid) 

enter image description here