当它们在函数中时,情况就不同了,特别是当你用名称而不是值来引用它们时。尝试:
library(dplyr)
library(ggplot2)
var_freq_plot_fn <- function(df, selected_var){
df %>%
count(.data[[selected_var]]) %>%
ggplot(aes(x = n, y = .data[[selected_var]], fill = .data[[selected_var]])) +
geom_col() +
theme(legend.position = "none")
}
plot_list <- purrr::map(names(dummy_df), var_freq_plot_fn, df = dummy_df)
var_freq_plot_fn <- function(df){
purrr::map(df %>% select_if(is.character) %>% names, ~
df %>%
count(.data[[.x]]) %>%
ggplot(aes(x = n, y = .data[[.x]], fill = .data[[.x]])) +
geom_col() +
theme(legend.position = "none"))
}
var_freq_plot_fn(dummy_df)