0
votes

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

1
The geoms inherit all aesthetic mappings which were set in ggplot. - Roland
@Roland Thanks got it! - Shubham Rajput
But why I got -2? I am a beginner in data science and at stack overflow is asking such question wrong here? - Shubham Rajput

1 Answers

1
votes

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