2016-04-26 942 views
0

我很開心探索R和ggplot2。在使用ggplot2和geom_jitter時,我現在有一個問題需要更改我的數據點的漸變。這裏是我使用的代碼:如何更改ggplot2中數據點的顏色範圍geom_jitter()

t1 <- ggplot(mtcars, aes(x=as.factor(cyl),y=mpg)) 
t2 <- geom_boxplot(outlier.shape=NA) 
t3 <- geom_jitter(width=0.3, size=3, aes(color = disp)) 
t1 + t2 + t3 

回報: enter image description here

不過,我想高數是黑暗的。或者更好 - 我想用紅色表示最高的數字,用綠色表示最低的數字(只是一個例子)。

謝謝!

回答

1

這工作的順序爲:

t1 <- ggplot(mtcars, aes(x=as.factor(cyl),y=mpg)) 
t2 <- geom_boxplot(outlier.shape=NA) 
t3 <- geom_jitter(width=0.3, size=3, aes(color = disp)) 
t4 <- scale_colour_gradient(low="green",high="red") 
t1 + t2 + t3 + t4 
+0

謝謝拉夫。這工作完美! –

+0

還有一個問題:爲什麼你在括號中顯示? t3 < - geom_jitter(width = 0.3,size = 3,aes(color =(disp))) –

+0

這是我的錯誤,它使用'as.numeric()'將其轉換爲數字,因爲漸變對數值 –

0

您需要定義DISP作爲分類變量指示變量

​​