【发布时间】:2021-07-23 13:50:40
【问题描述】:
这是我的数据集:
df_table <- data.frame(user = c("User1", "User2", "User3"), opened_dates = c("2021-07-01", "2021-08-02", "2021-09-03"), num_active_users = c(5, 18, 11))
我有以下函数用于为我的数据集中的所有用户创建一个绘图。
daily_active_users <- function(data, user, plot_color = "blue") {
ggplot(data, aes(x = opened_dates, y = num_active_users, fill = plot_color)) +
geom_col() +
theme(legend.position = "none") +
labs(title = paste0(user, ": Daily # Active Users"), y = "# Active Users")}
目前我必须手动指定用户来为每个人运行绘图
daily_active_users(df_table, "User1", plot_color = "blue")
我想要一种方法来遍历所有用户,而不必像上面那样手动执行此操作。这样做的最佳方法是什么?
【问题讨论】:
-
lapply(df_table%>% distinct(user)%>%pull(user), function(x) daily_active_users(df_table, x))将在list中为您提供绘图。 -
@Limey 谢谢!