基于R中的单个数据集生成许多多图

Generate many multi-plot based on a single dataset in R

我有以下数据集,其中包含每个 class 的 许多 运行s 的数据(即,仅在以下 两个 运行s 每个 class):

Class   Total_individuals   1   2   3   4   5
A       1000                10  6   8   5   2
A       1000                3   9   1   2   5
B       1000                7   2   6   4   8
B       1000                1   9   8   2   5
C       1000                6   4   2   8   7
C       1000                9   1   5   4   8

我想为每个 class 生成一个包含单个图的多图,如下所示:

此图显示了三个 class 中 第一个 运行 的数据:

A      10   6   8   5   2
B      7    2   6   4   8
C      6    4   2   8   7

然后,我想为第 秒 运行 的数据生成另一个多图,它们是:

A      3    9   1   2   5
B      1    9   8   2   5
C      9    1   5   4   8

为此,我编写了以下 R 脚本:

####################################
multiplot <- function(..., plotlist=NULL, file, cols=1, layout=NULL) {
  library(grid)

  # Make a list from the ... arguments and plotlist
  plots <- c(list(...), plotlist)

  numPlots = length(plots)

  # If layout is NULL, then use 'cols' to determine layout
  if (is.null(layout)) {
    # Make the panel
    # ncol: Number of columns of plots
    # nrow: Number of rows needed, calculated from # of cols
    layout <- matrix(seq(1, cols * ceiling(numPlots/cols)),
                     ncol = cols, nrow = ceiling(numPlots/cols))
  }

  if (numPlots==1) {
    print(plots[[1]])

  } else {
    # Set up the page
    grid.newpage()
    pushViewport(viewport(layout = grid.layout(nrow(layout), ncol(layout))))

    # Make each plot, in the correct location
    for (i in 1:numPlots) {
      # Get the i,j matrix positions of the regions that contain this subplot
      matchidx <- as.data.frame(which(layout == i, arr.ind = TRUE))

      print(plots[[i]], vp = viewport(layout.pos.row = matchidx$row,
                                      layout.pos.col = matchidx$col))
    }
  }
}
###################################
library(readr)
library(reshape2)
library(dplyr)
library(ggplot2)
library(scales)

dataset <- read_csv("/home/adam/Desktop/a.csv")
YaxisTitle <- "Fitness"


dataset <- dataset %>% melt(id.vars = c("Class"))
dataset <- subset(dataset, variable != "Total_individuals")
dataset <- transform(dataset, value = as.numeric(value))

myplots <- list()  # new empty list
for (x in unique(dataset$Class)){
  p2_data <- dataset %>% filter(Class == x)
  pp2 <- p2_data %>% ggplot(aes(x=variable, y=value, group=Class, colour=Class)) + 
    geom_line() + 
    scale_x_discrete(breaks = seq(0, 5, 1)) + 
    labs(x = as.character(p2_data$Class), y = YaxisTitle) + 
    theme(text = element_text(size=10),legend.position="none")

  myplots[[i]] <- pp2
  i <- i+1
}

xx <- multiplot(myplots[[1]], myplots[[2]], myplots[[3]], cols=2)

png(filename="/home/adam/Desktop/name.png")
plot(xx)
dev.off()

但是这个脚本给了我以下情节:

将所有 运行 的所有数据组合在一个图中。

所以我想要的是为三个 classes 的每个 运行 生成一个多图。

使用面:从宽到长重塑,添加 x 值和 运行N,然后绘制方面:

# example data
df1 <- read.table(text = "Class   Total_individuals   1   2   3   4   5
A       1000                10  6   8   5   2
A       1000                3   9   1   2   5
B       1000                7   2   6   4   8
B       1000                1   9   8   2   5
C       1000                6   4   2   8   7
C       1000                9   1   5   4   8", header = TRUE)

library(ggplot2)
library(tidyr)


plotDat <- df1 %>% 
  group_by(Class) %>% 
  mutate(runN = paste0("run_", row_number())) %>% 
  gather(key = "k", value = "v", -c(Class, Total_individuals, runN)) %>% 
  group_by(Class, runN) %>% 
  mutate(x = row_number())

运行 上的方面 ID:

ggplot(plotDat, aes(x, v, col = Class)) +
  geom_line() +
  facet_grid(.~runN)

或 运行 id 和 Class 方面:

ggplot(plotDat, aes(x, v, col = Class)) +
  geom_line() +
  facet_wrap(.~runN + Class, ncol = length(unique(plotDat$Class)))

或者更好的版本,如@Axemen 评论中所述:

ggplot(plotDat, aes(x, v, col = Class)) +
  geom_line() +
  facet_grid(runN ~ Class)