通过4个因素的组合来迭代3向交互ggplot调用

时间:2018-08-03 09:51:07

标签: r ggplot2

免责声明:更好的标题建议比受欢迎的

我很难弄清楚如何遍历3个因素的不同组合对ggplot2的调用。我可能可以根据数据来描述这个最好的方法... 但是如果不清楚,请询问/评论(英语不是我的主要语言)

给出这样的数据框:

library(ggplot2)
library(dplyr)
library(gtools)

# Make up data
set.seed(1)
df <- data.frame(y = rnorm(16),
                 factor1 = gl(2, 1, 16), 
                 factor2 = gl(2, 2, 16),
                 factor3 = gl(2, 4, 16),
                 factor4 = gl(2, 8, 16))

考虑到4个因子(factor1:factor4)构成3个元素的24个唯一组合的事实,我将其称为:

  • x axis
  • lines
  • facets

并将其存储在p中:

p <- data.frame(
      permutations(4,3, c("factor1", "factor2", "factor3", "factor4")))
names<- c("x axis", "lines", "facets")
colnames(p) <- names

> head(p)
   x axis   lines  facets
1 factor1 factor2 factor3
2 factor1 factor2 factor4
3 factor1 factor3 factor2
4 factor1 factor3 factor4
5 factor1 factor4 factor2
6 factor1 factor4 factor3

如何自动绘制24个图形,以便:

  • x axislinesfacetsp的每一行中一个个地获取
  • df$y总结(mean(y, na.rm=T)的各个因素
  • 显示了情节

如果我在哪里手动进行操作,第4个图(共24个)将如下所示:

p[4,]
   x axis   lines  facets
4 factor1 factor3 factor4

df %>% group_by(factor1, factor3, factor4) %>%
      summarise(y=mean(y, na.rm=T)) %>% 
      ggplot(aes(y=y, x = factor1)) +
      geom_line(aes(group=factor3, color=factor3)) +
      facet_grid(~factor4)

而24号会是这样:

> p[24,]
    x axis   lines  facets
24 factor4 factor3 factor2

    df %>% group_by(factor4, factor3, factor2) %>%
          summarise(y=mean(y, na.rm=T)) %>% 
          ggplot(aes(y=y, x = factor4)) +
          geom_line(aes(group=factor3, color=factor3)) +
          facet_grid(~factor2)

有人可以帮我吗?

EDIT1

我已经能够编写一个for()循环来聚合数据,但是对于共振,我并不认为所有24个数据帧都是相同的:

 for(i in 1:nrow(p)){
      xaxis <- as.character(p[i,1])
      lines <- as.character(p[i,2])
      facets <- as.character(p[i,3])
      colnames(df)[colnames(df) == xaxis[i]] <- "xaxis"
      colnames(df)[colnames(df) == lines[i]] <- "lines"
      colnames(df)[colnames(df) == facets[i]] <- "facets"
      print(df %>% group_by(xaxis, lines, facets) %>%
                  summarise(y=mean(y, na.rm=T)))
      }

2 个答案:

答案 0 :(得分:2)

好的...我花了半天的时间反复尝试,但最终使它正常工作

组成数据

library(ggplot2)
library(dplyr)
library(gtools)


set.seed(1)
df <- data.frame(y = rnorm(16),
                 factor1 = gl(2, 1, 16), 
                 factor2 = gl(2, 2, 16),
                 factor3 = gl(2, 4, 16),
                 factor4 = gl(2, 8, 16))

p <- data.frame(
      permutations(4,3, c("factor1", "factor2", "factor3", "factor4")))
names<- c("xaxis", "lines", "facets")
colnames(p) <- names

绘制图

for(i in 1:nrow(p)){
      xaxis <- as.character(p[i,1])
      lines <- as.character(p[i,2])
      facets <- as.character(p[i,3])
      df2 <- df %>% rename(xaxis= colnames(df)[colnames(df) == xaxis],
                         lines= colnames(df)[colnames(df) == lines],
                         facets= colnames(df)[colnames(df) == facets])
      print(df2 %>% 
                  group_by(xaxis, lines, facets) %>%
                  summarise(y=mean(y, na.rm=T)) %>%
                  ggplot(aes(y=y, x = xaxis)) +
                  geom_line(aes(group=lines, color=lines)) +
                  facet_grid(~facets))
}

我将保留此答案,如果有人想给我展示更多优雅的作品(只是出于好奇),我很乐意接受。

答案 1 :(得分:2)

考虑将基数R的Map(包装到mapply)作为通过置换数据帧逐元素进行迭代的替代方法。

p <- data.frame(permutations(4,3, c("factor1", "factor2", "factor3", "factor4")),
                stringsAsFactors = FALSE) %>% 
         setNames(c("xaxis", "lines", "facets"))

runplots <- function(x, l, f) {      
  df2 <- df[c("y", x, l, f)] %>% 
           setNames(c("y", "xaxis", "lines", "facets"))

  print(df2 %>% 
          group_by(xaxis, lines, facets) %>%
          summarise(y=mean(y, na.rm=T)) %>%
          ggplot(aes(y=y, x = xaxis)) +
            geom_line(aes(group=lines, color=lines)) +
            facet_grid(~facets))
}

plot_list <- Map(runplots, p$xaxis, p$lines, p$facets)

并且由于您保留了输出为Map的绘图列表,因此可以将该对象用于其他地方。甚至删除print并调用图,如下所示:

# BY INDEX
plot_list[[1]]
plot_list[[2]]
...

# BY NAME (AFTER RENAMING LIST)
names(plot_list) <- gsub("factor", "f", paste(p$xaxis, p$lines, p$facets, sep="_"))
plot_list$f1_f2_f3
plot_list$f1_f2_f4
...