Search code examples
rggplot2purrr

Map over some numeric columns and for each boxplot with a grouping var


I would like to purrr::map over a df and for each column produce a boxplot with a grouping var. Example attempt:

mtcars |> select(am, gear, carb) |> 
  map(~ ggplot() +
        geom_boxplot(aes(y = .x, fill = factor(mtcars$cyl))) +
        labs(title = .y))

Outcome:

Error in dots_list(..., title = title, subtitle = subtitle, caption = caption, : the ... list contains fewer than 2 elements

Desired outcome:

  1. .x would be the vector/field from the df
  2. Using the native pipe placeholder _, assign the grouping var cyl
  3. The title would be the name of the column being plotted. I'm sure I recall seeing .y being used here before

How can I purrr::map over the 3 columns am, gear and carb, for each produce a boxplot grouped/split by cyl and using hte native pipe?


Solution

  • With purrr::map, it will not define .y, but I'm inferring that you intend for it to be the column name. Use purrr::imap and your code works.

    library(dplyr)
    library(purrr)
    library(ggplot2)
    mtcars |> select(am, gear, carb) |> 
      imap(~ ggplot() +
            geom_boxplot(aes(y = .x, fill = factor(mtcars$cyl))) +
            labs(title = .y))
    

    three ggplot images

    Optional, I made the above triple plot with patchwork:

    library(patchwork)
    map(c("am", "gear", "carb"), ~ mtcars |> 
          ggplot() +
            geom_boxplot(aes(y = .data[[.x]], fill = as.factor(cyl))) +
          labs(title = .x)) |>
      Reduce(`+`, x = _)
    

    ... though this also be done more easily with faceting.