Search code examples
rpdfggplot2line

Line plots for several data using ggplot2 and save as pdf


With following three different data set,

    mean=replicate(10,rnorm(10))        
    colnames(mean)=paste0(rep(c("x0","x1","x2","x3","x4"),2),"_c", rep(c(1:2), each=5))
    meanpos=replicate(10,rnorm(10))+1.5
    meanneg=replicate(10,rnorm(10))-1.5
    hcol=c(0,0.5,0,0.75,1.0,
           1.1,1.20,0,0.8,-0.025)#vector of size ncol(mean)

I can create the line plots using following for loop

par(mfrow=c(2,2))
for ( v in 1:ncol(mean)){
  plot(mean[,v], type = "l", 
       ylim = c(min(meanpos[,v],mean[,v]), 
                max(meanpos[,v],mean[,v])), 
       xlab = "sl no", ylab = "",main = colnames(mean)[v])
  abline(h=hcol[v], col="purple")
  lines(meanpos[,v], col="blue")
  lines(meanneg[,v], col="green")
}

One plot for each column and outlined as 2 by 2. Here are a few plots

enter image description here

How can I create a similar plot using the ggplot2 function with a legend for each line and save as pdf file.

Any help is appreciated


Solution

  • If you want to use ggplot2 you can format data as next. It is better if you save your data from vectors to dataframes and then you can bind all data to reshape and have the desired plot using facets instead of loops as you did. You can tune ncol argument from facet_wrap() in order to define a matrix structure. Here the code using the data you provided. I added also the steps to have dataframes and easily use ggplot2 functions:

    library(tidyverse)
    #Initial data
    set.seed(123)
    #Data
    mean=replicate(10,rnorm(10))        
    colnames(mean)=paste0(rep(c("x0","x1","x2","x3","x4"),2),"_c", rep(c(1:2), each=5))
    meanpos=replicate(10,rnorm(10))+1.5
    meanneg=replicate(10,rnorm(10))-1.5
    hcol=c(0,0.5,0,0.75,1.0,
           1.1,1.20,0,0.8,-0.025)#vector of size ncol(mean)
    

    We save data in dataframes and identify all values:

    #Concatenate all in a dataframe
    df1 <- as.data.frame(mean) 
    #Data for intercepts
    hcol=c(0,0.5,0,0.75,1.0,
           1.1,1.20,0,0.8,-0.025)
    #Dataframe
    dfh <- data.frame(name=names(df1),hcol,stringsAsFactors = F)
    #Mean pos
    df2 <- as.data.frame(meanpos)
    names(df2) <- names(df1)
    #Mean neg
    df3 <- as.data.frame(meanneg)
    names(df3) <- names(df1)
    #Assign ids
    df1$id <- 'mean'
    df2$id <- 'mean pos'
    df3$id <- 'mean neg'
    #Rows
    df1$id2 <- 1:dim(df1)[1]
    df2$id2 <- 1:dim(df2)[1]
    df3$id2 <- 1:dim(df3)[1]
    #Bind
    dfm <- rbind(df1,df2,df3)
    

    With the entire data, we reshape it to use facets:

    #Pivot
    dfm %>% pivot_longer(cols = -c(id,id2)) -> dfm2
    

    Now, the plot:

    #Sketch for plot
    G1 <- ggplot(dfm2,aes(x=id2,y=value,group=id,color=id))+
      geom_line()+
      geom_hline(data = dfh,aes(yintercept = hcol),color='purple')+
      facet_wrap(.~name,scales='free')+
      xlab("sl no")+ylab("")+
      scale_color_manual(values = c('mean'='tomato','mean pos'='blue','mean neg'='green'))+
      theme_bw()+
      theme(legend.position = 'top')
    

    You can save as .pdf with ggsave():

    #Export
    ggsave(filename = 'Plot.pdf',plot = G1,width = 35,height = 20,units = 'cm')
    

    Output:

    enter image description here