Shubham Rajput
Shubham Rajput

Reputation: 302

Difference in passing the object inside aes() in ggplot and passing the same object outside ggplot

What is the difference in mapping the Species to color aesthetic inside ggplot and inside geom_point. I am using iris data set.

ggplot(aes(x = Sepal.Length, y = Petal.Length, color = Species), data = 
trainData)+
geom_point()+
geom_smooth()

AND

ggplot(aes(x = Sepal.Length, y = Petal.Length), data = trainData)+
geom_point(aes(color = Species))+
geom_smooth()

The graph I am getting:

Output for the first code enter image description here

Output for the second code enter image description here

Upvotes: 0

Views: 470

Answers (1)

Andrew Chisholm
Andrew Chisholm

Reputation: 6567

It's probably because the aes() call in the second case colours the points but this is not carried forward to the colour for the smooth line. Changing the second example to add an explicit call to aes(color...) for the geom_smooth() call results in the same result as the first example.

ggplot(aes(x = Sepal.Length, y = Petal.Length), data = trainData) +
geom_point(aes(color = Species)) +
geom_smooth(aes(color=Species))

Upvotes: 1

Related Questions