5 Extra

geoms <- list(
  geom_point(),
  geom_boxplot(aes(group = cut_width(displ, 1))),
  list(geom_point(), geom_smooth())
)

p <- ggplot(mpg, aes(displ, hwy))
lapply(geoms, function(g) p + g)
## [[1]]

## 
## [[2]]

## 
## [[3]]
## `geom_smooth()` using method = 'loess' and formula = 'y ~ x'

Exercises How could you add a geom_point() layer to each element of the following list?

plots <- list(
  ggplot(mpg, aes(displ, hwy)),
  ggplot(diamonds, aes(carat, price)),
  ggplot(faithfuld, aes(waiting, eruptions, size = density))
)

What does the following function do? What’s a better name for it?

mystery <- function(...) {
  Reduce(`+`, list(...), accumulate = TRUE)
}

mystery(
  ggplot(mpg, aes(displ, hwy)) + geom_point(), 
  geom_smooth(), 
  xlab(NULL), 
  ylab(NULL)
)
## [[1]]

## 
## [[2]]
## `geom_smooth()` using method = 'loess' and formula = 'y ~ x'

## 
## [[3]]
## `geom_smooth()` using method = 'loess' and formula = 'y ~ x'

## 
## [[4]]
## `geom_smooth()` using method = 'loess' and formula = 'y ~ x'

https://ggplot2-book.org/maps.html