Grouping variables in ggplot2

# Amend the plot to add another smooth layer with dummy grouping
fcyl=factor(mtcars$cyl)
ggplot(mtcars, aes(x = wt, y = mpg, color = fcyl)) +
  geom_point() +
  stat_smooth(method = "lm", se = FALSE) +
  stat_smooth(method = "lm", se = FALSE, aes(group=1))

I don't understand aes(group=1) in the last line. Can anyone explain it? A very similar question is:

# Amend the plot
ggplot(mtcars, aes(x = wt, y = mpg, color = fcyl)) +
  geom_point() +
  # Map color to dummy variable "All"
  stat_smooth(aes(color = "All"), se = FALSE) +
  stat_smooth(method = "lm", se = FALSE)

I don't understand # Map color to dummy variable "All". (aes(color = "All"))

Have you tried looking at the charts produced with and without the elements you are trying to understand ?

the line is from all the data, rather than from the grouped data

1 Like

I tried and I can see the difference (a negative sloped line). But I don't know what the line is.

I see. What if I change it to group =2. It seems the same as group=1.

yeah, its a dummy grouping variable. the same is used for every value, so they are in a common group, whether that common group has the value, 1 , or 2 or anything else, is irrelevant to its use.

1 Like

still don't understand the 2nd question. aes(color = "All") ?