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:
.x
would be the vector/field from the df_
, assign the grouping var cyl
.y
being used here beforeHow 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?
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))
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.