如何为列出的数据框的所有变量绘制多个图

How to plot multiple plots for all variables of listed dataframes

我有一个列出的数据框,代码为:

df1 <- data.frame(ID = rep(c("P-1000", "P-10001", "P-10002", "P-1003", "P-1004"),3),
                Visit = c(rep("M1",5), rep("M4",5), rep("M17",5)),
                Value = runif(15))

df2 <- data.frame(ID = rep(c("P-1000", "P-10001", "P-10002", "P-1003", "P-1004"),3),
                Visit = c(rep("M1",5), rep("M4",5), rep("M17",5)),
                Value = runif(15))

df3 <- data.frame(ID = rep(c("P-1000", "P-10001", "P-10002", "P-1003", "P-1004"),3),
                Visit = c(rep("M1",5), rep("M4",5), rep("M17",5)),
                Value = runif(15))

sampledata<- list(df1, df2, df3)
names(sampledata)<- c("A", "C", "Z")

我正在为其绘制绘图。我正在尝试为每个数据帧的每个 ID 绘制图表(例如,1 个图表用于数据帧 A 中的 P-1000)。最后,我想将这些图按数据帧保存在单独的 pdf 中(例如,1 个 pdf 用于所有与数据帧 A 有关的图,另一个用于数据帧 B,等等)。我不太清楚该怎么做。我目前试用的代码如下:

sample_data_plots <- lapply(seq_along(sampledata),function(i) {
  sampledata[[i]] %>% filter(!is.na(Visit)) %>% group_by(ID) %>%
    mutate(Visit = factor(Visit, levels = c("M1", "M4", "M17"))) %>%
    ggplot(aes(x = Visit, y = Value)) + geom_boxplot() +
    labs(title = paste("Values of", names(sampledata)[i], "for", "ID", 
#this is the variable ID
),
         y = "Value",
         x = "Visit")}) 

我在 运行 中没有问题:

lapply(seq_along(sampledata), function(i) {
  sampledata[[i]] %>% filter(!is.na(Visit)) %>% 
    mutate(Visit = factor(Visit, levels = c "M1", "M4", "M17"))) %>% 
    ggplot(aes(x = Visit, y = Value)) + geom_boxplot() + 
    labs(title = paste("Value", names(sampledata)[i]),
         y = "Value",
         x = "Visit") })

但这给出了所有 ID 的每个数据帧的图表

如有任何帮助,我们将不胜感激。谢谢!

考虑 purrr::map2 to apply a function across list of data frames and their names and dplyr::group_map 按 ID 组应用函数。

build_plot <- function(sub_df) {
  sub_df %>%
     filter(!is.na(Visit)) %>% 
     mutate(Visit = factor(Visit, levels=c("M1", "M4", "M17"))) %>% 
     ggplot(aes(x=Visit, y=Value)) + 
       geom_boxplot() + 
       labs(title = paste("Value", sub_df$ID[1]), y="Value", x="Visit")
}

run_groups <- function(main_df, df_name) {
  # BUILD PLOTS BY ID
  plot_list <- main_df %>%
      group_by(ID) %>%
      group_map(build_plot)

  # SAVE PLOTS TO SINGLE PDF
  ggplot2::ggsave(
      filename = paste0(df_name, "_plots.pdf"), 
      plot = gridExtra::marrangeGrob(plot_list, nrow=1, ncol=1),
      width = 15, height = 9
  )

  return(plot_list)
}

# CREATE PDFS BY DATA FRAME
myplots <- purr::map2(sample_data, names(sample_data), run_groups)

类似的 base R 方法分别是 lapply 的兄弟:Mapmapply 的包装器)和 byby 的包装器19=]).

run_groups <- function(main_df, df_name) {
  # BUILD PLOTS BY ID
  plot_list <- by(main_df, main_df$ID, build_plot)
  ...
}

# CREATE PDFS BY DATA FRAME
myplots <- Map(run_groups, sample_data, names(sample_data))