watchtower
watchtower

Reputation: 4298

Using aesthetics with multiple layers in ggplot

I am new to ggplot. I am trying to understand how to use ggplot. I am reading Wickham's book and still trying to wrap my head around how to use aes() function.

What's the difference between these two implementations of aes():

library(ggplot2)

ggplot(mpg, aes(displ, hwy, colour = class)) +
  geom_point() +
  geom_smooth(method = "lm", se = FALSE) +
  theme(legend.position = "none")

and

ggplot(mpg, aes(displ, hwy)) +
  geom_point(aes(colour = class)) +
  geom_smooth(method = "lm", se = FALSE) +
  theme(legend.position = "none")

Both of them print significantly different graphs. Any help? I am really stuck.

Upvotes: 5

Views: 2356

Answers (1)

Pj_
Pj_

Reputation: 824

In the first one you are mapping the aesthetics globally, ggplot will try to map these aesthetics to all other geom_xyz() layers.

While in the latter case, you are mapping aesethics to a specific ggplot layer (in your case geom_point())

Upvotes: 5

Related Questions