There is a data frame, e.g.:
df <- data.frame(k = sample(1:2, 100, replace = TRUE),
l = sample(1:2, 100, replace = TRUE),
g = sample(1:3, 100, replace = TRUE, prob = c(0.2, 0.6, 0.2)))
And I will need proportion plots for l and k grouped by g so I write myself a function:
library(tidyverse)
fun_gg_factor <- function(p) {
df %>%
group_by(g) %>%
count({{p}}) %>%
mutate(Anteil = n / sum(n)) %>%
ggplot(aes(x = {{p}}, y = Anteil)) +
geom_col(position = position_dodge()) +
facet_grid(.~g)
}
And it works as intended:
fun_gg_factor(k)
That is nice. But my rl df has more variables than k and l. Much more. So I do not want to call the function manually dozens of times like this:
fun_gg_factor(k)
fun_gg_factor(l)
fun_gg_factor(m)
.
.
.
fun_gg_factor(z)
sapply() and its forms come to mind:
sapply(c(k, l), fun_gg_factor)
That does not work, as k and l are not objects. Even if they were, that is not what I want. I do not need a plot for every element of df$k - I want plots for the different columns.
Maybe I try a loop:
for (i in c(k, l)) {
fun_gg_factor(i)
}
But no, k and l are still no objects.
Obviously my representation of the problem is lacking. How do I efficiently use different arguments for this or any similar custom function?


