ggplot2 color - Displays a different color when specified 'red'

和自甴很熟 提交于 2021-02-05 08:03:29

问题



I am trying to understand how ggplot2 handles the aesthetics for color.

The two ggplot commands shown below display different colors.The second command displays a lighter color, and in addition prints a legend.
I appreciate if anyone can throw some light on this concept.

data(iris)

#1st command
ggplot(iris) + geom_point(aes(Sepal.Length,Sepal.Width), color = "red")

#2nd command
ggplot(iris) + geom_point(aes(Sepal.Length,Sepal.Width,  color = "red"))

回答1:


aes() maps between variables in the input data and visual properties such as color, shape, etc. Therefore, your first command which assigned "red" as a color outside of aes() is correct. Your second command seems incorrect (although it will work without any errors) as you did not match color to your variable in aes(). You can map a variable (e.g., Species in iris data) to color inside of aes() such as,

ggplot(iris) + geom_point(aes(Sepal.Length,Sepal.Width,  color = Species))

I don't know how ggplot2 handles this, but I think you will get the same color for your plot regardless of color names inside of aes() in your second command.

For example, those three codes below will plot the same with the same light red color (except the legends).

ggplot(iris) + geom_point(aes(Sepal.Length,Sepal.Width,  color = "red"))
ggplot(iris) + geom_point(aes(Sepal.Length,Sepal.Width,  color = "black"))
ggplot(iris) + geom_point(aes(Sepal.Length,Sepal.Width,  color = "blue"))

Hope this helps!



来源:https://stackoverflow.com/questions/37054176/ggplot2-color-displays-a-different-color-when-specified-red

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!