user6296218
user6296218

Reputation: 355

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


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"))

Upvotes: 1

Views: 1278

Answers (1)

Joonbum
Joonbum

Reputation: 173

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!

Upvotes: 2

Related Questions