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

时间:2018-10-18 09:43:49

标签: r ggplot2 r-grid

我有以下数据集,其中包含每个单个班级的多次运行的数据(即,在以下情况下,每个班级仅两次运行):

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

我想生成一个多图,其中每个类包含一个图,如下所示:

enter image description here

此图显示了三个类别的首次运行的数据:

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()

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

enter image description here

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

所以我要为三个类的每次运行生成一个多图。

1 个答案:

答案 0 :(得分:2)

使用刻面:从宽到长整形,添加 x 值和 runN ,然后使用刻面进行绘制:

# 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)

enter image description here

或者在运行ID和类方面:

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

enter image description here

甚至更好的版本,如@Axemen的评论中所述:

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

enter image description here