I have written a function to calculate proportions and confidence intervals across groups, then plot the results.
However, I next want to be able to supply a list of variables for groupvar to allow me to map and get all desired plots with one call:
library(tidyverse)
data(mtcars)
my_graph <- function(data, groupvar){
data %>%
group_by(vs, {{groupvar}}, am) %>%
summarise(n=n()) %>%
mutate(sum = sum(n)) %>%
rowwise %>%
mutate(xx = list(broom::tidy(prop.test(n, sum, conf.level=0.95)))) %>%
unnest(xx) %>%
ggplot() +
geom_bar(aes(x=factor(vs), y=estimate, fill={{groupvar}}), stat = "identity") +
geom_errorbar(aes(x=factor(vs), ymin=conf.low, ymax = conf.high), width = 0.2) +
scale_y_continuous(labels = scales::percent_format(accuracy = 1)) +
facet_wrap(vars({{groupvar}}))+
theme(legend.position = "none")
}
#This works
my_graph(data = mtcars, groupvar = am)

#But how to map across a list of variables?
plot_vars <- mtcars %>%
select(am, gear, carb) %>%
names()
map(plot_vars, ~my_graph(data=., groupvar = .x))
#> Error in UseMethod("group_by_"): no applicable method for 'group_by_' applied to an object of class "character"
Created on 2019-07-21 by the reprex package (v0.3.0)