Search code examples
rggplot2r-grid

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


I have the following dataset that contains data for many runs for each single class (i.e., in the following only two runs for each 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

I would like to generate a multi plot that contains a single plot for each class as the following:

enter image description here

This plot shows the data for the first run of the three classes that are:

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

Then, I would like to generate another multi plot for the data of the second run that are:

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

For this, I wrote the following R script:

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

But this script gives me the following plot:

enter image description here

which combines all the data of all the runs in one plot.

So what I want is to generate a single multi plot for each run for the three classes.


Solution

  • Using facets: reshape from wide-to-long, add x values and runN, then plot with facets:

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

    Facet on run id:

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

    enter image description here

    Or facet on run id and Class:

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

    enter image description here

    Or even better version, as mentioned in the comments by @Axemen:

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

    enter image description here