添加标题到使用map()创建的ggplots

jim*_*mbo 5 r ggplot2 purrr

使用map函数为我在下面创建的每个ggplot添加标题的最简单方法是什么?我希望标题反映每个数据框的名称 - 即4,6,8(圆柱体).

谢谢 :)

mtcars_split <- 
  mtcars %>%
  split(mtcars$cyl)

plots <-
  mtcars_split %>%
  map(~ ggplot(data=.,mapping = aes(y=mpg,x=wt)) + 
        geom_jitter() 
  # + ggtitle(....))

plots
Run Code Online (Sandbox Code Playgroud)

Pau*_*aul 7

使用map2names.

plots <- map2(
  mtcars_split,
  names(mtcars_split),
  ~ggplot(data = .x, mapping = aes(y = mpg, x = wt)) + 
    geom_jitter() +
    ggtitle(.y)
)
Run Code Online (Sandbox Code Playgroud)

编辑:alistaire指出这是一样的 imap

plots <- imap(
  mtcars_split,
  ~ggplot(data = .x, mapping = aes(y = mpg, x = wt)) + 
    geom_jitter() +
    ggtitle(.y)
)
Run Code Online (Sandbox Code Playgroud)