I'd like to loop through columns with purrr::map and print a histogram for each of them. This works.
library(tidyverse)
library(patchwork)
iris %>% # example data
select(-Species) %>% # drop one non-numeric col
map( ~ ggplot(iris, aes(x = .)) + # loop through cols... for col, take it as x aes
geom_histogram() # graph histogram
) %>% # output is list of graphs
wrap_plots() # wrap list using patchwork
But the graphs are pretty cryptic without titles to tell you which graph belongs to which column.
I tried adding a ggtitle option.
library(tidyverse)
library(patchwork)
iris %>%
select(-Species) %>%
map( ~ ggplot(iris, aes(x = .)) +
geom_histogram() +
ggtitle(.)
) %>%
wrap_plots()
However, it prints the first value of each column as the title, not the colname.
head(iris, 1) # for reference
What should I be doing differently to get colnames as graph titles for the individual graphs? I can make do with proper x axis labels as well.
map2()
one can reference a second vector of column names in the pipeline as illustrated in my answer. – Len Greski