颜色分配错误 [英] wrong colour assignment
本文介绍了颜色分配错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我意识到ggplot用与我预期不同的方式将颜色分配给垂直线.
I realized that ggplot assigns the colours to the vertical lines in a different way than I expect it to do.
创建数据框:
wages <- rnorm(100, 0.9, 0.5)
ids <- as.factor(round(rnorm(100, 1342, 98)))
x <- data.frame(ids, wages)
绘制分布,指定垂直线的颜色:
Drawing the distribution, specifying colours of the vertical lines:
ggplot(x, aes(x = wages)) +
geom_density(alpha=.4, colour = "darkgrey", fill = "darkgrey") +
geom_vline(data = x, aes(xintercept = mean(x$wages), colour = "green"),
linetype = 1, size = 0.5)+
geom_vline(data = x, aes(xintercept = median(x$wages), colour = "blue"),
linetype = 1, size = 0.5) +
geom_vline(data = x, aes(xintercept = mean(x$wages)+1*sd(x$wages), colour = "red"),
linetype = 1, size = 0.5)+
xlim(c(0,3))
您的图看起来会有所不同,但是结果保持不变:中线和均值+ 1sd线的颜色已切换.有谁知道如何解决这一问题?谢谢
Your graph will look differently, however one result stays the same: The median and the mean+1sd line colours are switched. Does anyone know how to fix this? Thanks
推荐答案
当您通过颜色名称提供颜色而不是映射到变量时,应将colour=
放在aes()
之外.
As you are providing colors by their name and not mapping to variable, colour=
should be placed outside the aes()
.
ggplot(x, aes(x = wages)) +
geom_density(alpha=.4, colour = "darkgrey", fill = "darkgrey") +
geom_vline(data = x, aes(xintercept = mean(x$wages)),colour = "green",
linetype = 1, size = 0.5)+
geom_vline(data = x, aes(xintercept = median(x$wages)),colour = "blue",
linetype = 1, size = 0.5) +
geom_vline(data = x, aes(xintercept = mean(x$wages)+1*sd(x$wages)),colour = "red",
linetype = 1, size = 0.5)+
xlim(c(0,3))
这篇关于颜色分配错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文