简体   繁体   English

如何在ggplot2 geom_jitter()中更改数据点的颜色范围

[英]How to change the color range of data points in ggplot2 geom_jitter()

I am having fun exploring R and ggplot2. 我在探索R和ggplot2时很开心。 I am now having a problem to change the gradient of my data points when using ggplot2 and geom_jitter. 现在,使用ggplot2和geom_jitter时,我无法更改数据点的渐变。 Here is the code I am using: 这是我正在使用的代码:

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

returns: 返回: 在此处输入图片说明

However, I would like the higher number to be dark. 但是,我希望更高的数字是暗的。 Or even better - I would like to have the highest numbers in red and the lowest numbers in green (just an example). 甚至更好-我想用红色表示最高的数字,用绿色表示最低的数字(仅作为示例)。

Thanks! 谢谢!

This works : 这有效:

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

您需要将disp定义为表明变量顺序的分类变量

mtcars$disp = factor(mtcars$disp, levels = names_of_levels_ordered)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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