I am trying to plot multiple factor columns in one go, using ggplot2 as plotting engine.
Plotting multiple metric columns is straight forward:
library(ggplot2)
library(dplyr) # dplyr 0.5.0 select_if
library(purrr)
data(diamonds)
diamonds %>%
select_if(is.numeric) %>%
gather %>%
ggplot(aes(x = value)) +
geom_histogram() +
facet_wrap(~key)
However, I did not success in plotting multiple factor (qualitative) columns in one shot. I would like to choose columns programmatically, ie., not directly naming them.
I tried this, but it does not produce a sensible result:
diamonds %>%
select_if(is.factor) %>%
gather %>%
ggplot(aes(x = value)) + geom_bar() +
facet_wrap(~key) +
coord_flip()
I assume that there might a solution along these lines:
diamonds %>%
select_if(is.factor) %>%
ggplot(aes(x = .[[1]])) + geom_bar()
Where .[[1]] should be replaced by some column placeholder (so here I directly named the column, which I would like to avoid, as I have a large number of columns in reality).
A for-loop will probably do the job, but I would like to get there with dplyr.



