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

问题描述 投票:2回答:1

我有以下数据集,其中包含每个单个类的多次运行的数据(即,下面每个类只有两次运行):

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

它结合了一个图中所有运行的所有数据。

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

r ggplot2 r-grid
1个回答
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和Class的方面:

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

© www.soinside.com 2019 - 2024. All rights reserved.