The group aesthetic controls which rows of the data get grouped together for geom like geom_line() and geom_smooth() which use multiple rows to create one “thing” on the plot. When using geom_line() and color is discrete, group is automatically set to match it, so you get, for example, one line of each color.

ggplot(data = Births78) +
  geom_line(aes(x = date, y = births, color = wday))

When color is continuous, group is not set that way, and instead you get a single line with various colors on each segment.

ggplot(data = Births %>% filter(year %in% c(1970, 1975), month < 3)) +
  geom_line(aes(y = births, x = day_of_year, color = factor(year)))

ggplot(data = Births %>% filter(year %in% c(1970, 1975), month < 3)) +
  geom_line(aes(y = births, x = day_of_year, color = year))

That last plot isn’t very good because the grouping isn’t set to the year now. We can fix that with group. (In general, if you see vertical lines joined by little diagonals, you probably need to use group.)

ggplot(data = Births %>% filter(year %in% c(1970, 1975), month < 3)) +
  geom_line(
    aes(y = births, x = day_of_year, color = year, group = year))

More generally, you might like the groups to be formed independently of the colors. Often it is desirable to have multiple lines of the same color, for example.

ggplot(data = Births %>% filter(year %in% c(1970, 1975))) +
  geom_line(
    aes(y = births, x = day_of_year,  linetype = factor(year), 
        color = wday, group = paste(wday, year)), 
    size = 0.7)

Here are some additional examples using geom_smooth().

Births2 <- Births %>% filter(year %in% c(1971, 1976)) %>%
  mutate(
    weekend = wday %in% c("Sat", "Sun"),
    Year = factor(year))
ggplot(data = Births2) +
  geom_smooth(
    se = FALSE,
    aes(y = births, x = day_of_year, color = Year, group = paste(Year, weekend))
  )

ggplot(data = Births78 %>% mutate(weekend = wday %in% c("Sat", "Sun"))) +
  geom_smooth(se = FALSE, aes(y = births, x = date, color = weekend))
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'

ggplot(data = Births78 %>% mutate(weekend = wday %in% c("Sat", "Sun"))) +
  geom_smooth(se = FALSE, aes(y = births, x = date, color = weekend, group = wday))
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'

It is also possible to have the color vary across a line. To do this we map color to the variable determining the color and use group to specify which points are connected in a single line.

ggplot(
  data = Births78 %>% mutate(weekend = wday %in% c("Sat", "Sun"))) +
  geom_line(
    aes(y = births, x = date, 
        color = day_of_year, 
        group = wday))

You can’t do the same thing with geom_smooth() because of the way the stat converts the raw data into the data for the smooth curve (the points on the smooth curve do not correspond directly to points in the original data, where you would be trying to define the color).

ggplot(
  data = Births78 %>% mutate(weekend = wday %in% c("Sat", "Sun"))) +
  geom_smooth(se = FALSE,
    aes(y = births, x = date, 
        color = day_of_year, 
        group = wday))
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'

By the way, if you are ever interested to see the data that are available for plotting after the stat has been applied, here’s how:

last_plot() %>% layer_data() %>% head()
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'
##          x        y group PANEL  colour   fill size linetype weight alpha
## 1 2922.000 7877.100     1     1 #3366FF grey60    1        1      1   0.4
## 2 2926.608 7842.320     1     1 #3366FF grey60    1        1      1   0.4
## 3 2931.215 7809.392     1     1 #3366FF grey60    1        1      1   0.4
## 4 2935.823 7778.242     1     1 #3366FF grey60    1        1      1   0.4
## 5 2940.430 7748.795     1     1 #3366FF grey60    1        1      1   0.4
## 6 2945.038 7720.978     1     1 #3366FF grey60    1        1      1   0.4

```

Notice that the value of colour is the same in every row. I suppose you could take this data and use it to generate a new plot mapping color to x (and cleaning up the labeling manually).