ggplot未绘制正确的颜色 [英] ggplot not plotting the correct color

查看:68
本文介绍了ggplot未绘制正确的颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

gb <- read.csv('results-gradient-boosting.csv')
p <- ggplot(gb) + geom_point(aes(x = pred, y = y),alpha = 0.4, fill = 'darkgrey', size = 2) + 
geom_line(aes(x = pred, y = pred,color = 'darkgrey'),size = 0.6) + 
geom_line(aes(x = pred, y = pred + 3,color = I("darkgrey")), linetype = 'dashed',size = 0.6) + 
geom_line(aes(x = pred, y = pred -3,color = 'darkgrey'),linetype = 'dashed',size = 0.6)

我的代码在上面.我不知道为什么在将颜色放入es时为什么颜色变成红色.但是,如果我把它放在aes之外,那是正确的.谢谢你的帮助!

My code is above. I have no idea why when I put color inside aes, the color turns out to be red. But if I put it outside of aes, it is correct. Thanks for your help!

推荐答案

color="darkgrey"放在aes之外时,ggplot会将其字面意思是将该行着色为"darkgrey".但是,当您将color="darkgrey"放在aes内时,ggplot会将其表示为要将颜色映射到变量.在这种情况下,变量只有一个值:"darkgrey".但这不是颜色"darkgrey".这只是一个字符串.你什么都可以叫. ggplot选择的颜色将基于默认调色板.当您希望该变量的不同级别使用不同的颜色时,将颜色映射到该变量.

When you put color="darkgrey" outside aes, ggplot takes it literally to mean that the line should be colored "darkgrey". But when you put color="darkgrey" inside aes, ggplot takes it to mean that you want to map color to a variable. In this case, the variable has only one value: "darkgrey". But that's not the color "darkgrey". It's just a string. You could call it anything. The color ggplot chooses will be based on the default palette. Map color to a variable when you want different colors for different levels of that variable.

例如,请参见下面的示例.颜色是从ggplot的默认调色板中选择的,并且完全独立于我们在每次调用geom_line时为colour使用的名称.当您具有采用三种不同唯一值的任何颜色美感时,将获得相同的三种颜色:

For example, see what happens in the example below. The colors are chosen from ggplot's default palette and are completely independent of the names we've used for colour in each call to geom_line. You will get the same three colors when you have any color aesthetic that takes on three different unique values:

library(ggplot2)
theme_set(theme_classic())

ggplot(mtcars) +
  geom_line(aes(mpg, wt, colour="green")) +
  geom_line(aes(mpg, wt - 1, colour="blue")) +
  geom_line(aes(mpg, wt + 1, colour="star trek"))

但是现在我们将颜色放在aes之外,以便按字面意义进行提取,并注释掉第三行,因为如果不使用有效的颜色,它将导致错误.

But now we put the colors outside aes so they are taken literally, and we comment out the third line, because it will cause an error if we don't use a valid colour.

ggplot(mtcars) +
  geom_line(aes(mpg, wt), colour="green") +
  geom_line(aes(mpg, wt - 1), colour="blue") #+
  #geom_line(aes(mpg, wt + 1), colour="star trek")

请注意,如果将颜色映射到mtcars的实际列(一个具有三个唯一级别的列),我们将获得与第一个示例相同的三种颜色,但是现在它们已映射到数据的实际特征:

Note that if we map colour to an actual column of mtcars (one that has three unique levels), we get the same three colors as in the first example, but now they are mapped to an actual feature of the data:

ggplot(mtcars) +
  geom_line(aes(mpg, wt, colour=factor(cyl)))

最后,如果我们想将这些映射的颜色设置为不同的值,该怎么办?

And finally, what if we want to set those mapped colors to different values:

ggplot(mtcars) +
  geom_line(aes(mpg, wt, colour=factor(cyl))) +
  scale_colour_manual(values=c("purple", hcl(150,100,80), rgb(0.9,0.5,0.3)))

这篇关于ggplot未绘制正确的颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆