Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ssh/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 错误的颜色分配_R_Ggplot2 - Fatal编程技术网

R 错误的颜色分配

R 错误的颜色分配,r,ggplot2,R,Ggplot2,我意识到ggplot将颜色分配给垂直线的方式与我预期的不同 创建数据帧: wages <- rnorm(100, 0.9, 0.5) ids <- as.factor(round(rnorm(100, 1342, 98))) x <- data.frame(ids, wages) 您的图形看起来会有所不同,但有一个结果保持不变:中间值和平均值+1sd线条颜色被切换。有人知道如何解决这个问题吗?谢谢因为您是根据颜色的名称提供颜色,而不是映射到变量,color=应该放在aes(

我意识到ggplot将颜色分配给垂直线的方式与我预期的不同

创建数据帧:

wages <- rnorm(100, 0.9, 0.5)
ids <- as.factor(round(rnorm(100, 1342, 98)))
x <- data.frame(ids, wages)


您的图形看起来会有所不同,但有一个结果保持不变:中间值和平均值+1sd线条颜色被切换。有人知道如何解决这个问题吗?谢谢

因为您是根据颜色的名称提供颜色,而不是映射到变量,
color=
应该放在
aes()
之外


color=
如果未映射到变量,则应将其置于
aes()
之外。@DidzisElferts非常感谢。成功了。@DidzisElferts你能补充一下作为答案吗?@PaulHiemstra这个问题在其他表格中已经被问过了,只是没有时间找到重复的。再次感谢你的解决方案。我对一个重复问题的存在做了一些研究,但没有发现任何重复问题。
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))
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))