#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"
The key difference between your single use of the function and your map() loop is that you are passing the variable names as strings in the latter. In the former you are passing bare variable names (no quotes).
The curly-curly coding works for bare variable names, but for strings you'll want to switch to using the .data pronoun.
So instead of
geom_bar(aes(x=factor(vs), y=estimate, fill={{groupvar}}), stat = "identity")
You'd have
geom_bar(aes(x=factor(vs), y=estimate, fill=.data[[groupvar]]), stat = "identity")
The .data pronoun works the same way in group_by() and facet_wrap().
Yes, you're right, that particular error didn't stem from using strings but because of the data = . inside map() (although you would have eventually had problems with {{ ).
The way you currently have your function set up you need to pass a dataset in. If you are using the same one for all plots you can do