0

Suppose I have two different data sets as follows, Data set 1 is lamda_BnT_3Com with dimension 30(nrow) by 1(ncol), I am plotting each 10 row with different color as

 pdf(file = "multi_plots.pdf")
    lamda_BnT_3Com=data.frame(lamda=c(0.09090055, 0.08797034, 0.09230524 ,0.09071845, 0.07000510, 0.05441981, 0.03730227, 0.07757363,
    0.07070489, 0.08195083, 0.08027035, 0.08115150, 0.11095167 ,0.09385823, 0.08916492, 0.07074638,
    0.07256362 ,0.08997501 ,0.09230641, 0.10240990 ,0.08788253 ,0.09349434 ,0.07997615 ,0.06859022,
   , 0.07610182, 0.07356230 ,0.07115823 ,0.06471446, 0.07449782 ,0.07144768)


par(mfrow=c(1,1))
loop.vector=1:ncol(lamda_BnT_3Com)
col.set=c("green","blue","purple","deeppink","darkorchid","darkmagenta","black","khaki")

for(b in loop.vector) {
  x.lamda<-lamda_BnT_3Com[,b]
  plot(x.lamda, type = "n", ylab="", xlab="",
       main=colnames(lamda_BnT_3Com)[b]) 
  mtext("trace plots of lamda for 3Com",line=-1.5, cex=1, outer = TRUE)
  for (k in 1:3){
    lamda_k=lamda_BnT_3Com[((nrow(lamda_BnT_3Com)/3)*k-((nrow(lamda_BnT_3Com)/3)-1)):
                             ((nrow(lamda_BnT_3Com)/3)*k),b]

    lines(((nrow(lamda_BnT_3Com)/3)*k-((nrow(lamda_BnT_3Com)/3)-1)):
            ((nrow(lamda_BnT_3Com)/3)*k),lamda_k,
          col=col.set[k])
    legend("topleft", bg="transparent",inset=0.05,legend=paste0("chain_",1:3),
           col=col.set, lty=1,box.lty=0, cex=0.8)
  }

}

Data set 2 is beta_BnT_2Cub with dimension 30(nrow) by 8(ncol) and plotting each 10 rows with different colors for each column as

head(beta_BnT_2Cub)

 beta0_C1  beta1_C1     beta2_C1      beta3_C1 beta0_C2   beta1_C2   beta2_C2     beta3_C2
1 6.012670 0.2777887  0.005647684 -0.0018061417 6.932491 -0.7845227 0.13098251 -0.005575552
2 6.086134 0.2219228  0.016464083 -0.0023880278 6.802323 -0.7457841 0.12749113 -0.005550782
3 6.014848 0.2778800  0.005228006 -0.0017090344 6.795580 -0.6879021 0.11005922 -0.004261526
4 6.082433 0.2591686  0.003690211 -0.0013860060 6.784797 -0.7235735 0.12234100 -0.005197434
5 5.998062 0.3265980 -0.007621821 -0.0008641481 6.587021 -0.5789524 0.09456842 -0.003607626
6 6.042630 0.2793871  0.003364040 -0.0015935694 6.850518 -0.7200823 0.12204105 -0.005271528




par(mfrow=c(2,2))
col.set=c("green","blue","purple","deeppink","darkorchid","darkmagenta","black","khaki")
loop.vector=1:ncol(beta_BnT_2Cub)
for(b in loop.vector) {
  x.beta<-beta_BnT_2Cub[,b]

  beta <- substr(sub("^beta", '', names(beta_BnT_2Cub)[b]),1,1)
  Cn <- substr(sub("^beta", '',names(beta_BnT_2Cub)[b]),3,4)
  plot(x.beta, type = "n", ylab="", xlab="",
       main=bquote(beta[.(beta)]~.(Cn)), 
       cex.main=1) 
  mtext("trace plots of betas for 2Cub before relab",line=-1.5, cex=1, outer = TRUE)
  for (k in 1:3){
    beta_k=beta_BnT_2Cub[((nrow(beta_BnT_2Cub)/3)*k-((nrow(beta_BnT_2Cub)/3)-1)):
                           ((nrow(beta_BnT_2Cub)/3)*k),b]


    lines(((nrow(beta_BnT_2Cub)/3)*k-((nrow(beta_BnT_2Cub)/3)-1)):
            ((nrow(beta_BnT_2Cub)/3)*k),beta_k,
          col=col.set[k])
    legend("topleft", bg="transparent",inset=0.05,legend=paste0("chain_",1:3),
           col=col.set, lty=1,box.lty=0, cex=0.8)
  }
}


dev.off()

How can I do these plots using ggplot2 package? Any help is highly appreciated.

Uddin
  • 754
  • 5
  • 18
  • 2
    Can you provide a small reproducible example of both datasets ? Such as `dput(head(df[,1:5]))` ? See here: https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example. Can you also provide an example fo the plot you are getting currently ? To use `ggplot2`, you will need to reshape your dataframe. C – dc37 Feb 23 '20 at 06:19
  • edited and added small data set for convenience. – Uddin Feb 23 '20 at 06:36
  • 1
    Can you show what you've tried to port this code to ggplot2 syntax and where you got stuck? Generally you would create plots into a list, combine them using one of the packages for this ([gridExtra, cowplot, patchwork](https://biolitika.si/how-to-combine-plots-and-table-made-with-ggplot-or-grid-graphics-in-r.html)) and print the final result to a pdf. If you have more images, you would create one ggplot2 image at a time and use `pdf(onefile=TRUE, ...)` to store everything in one file. – Roman Luštrik Feb 23 '20 at 07:00
  • @RomanLuštrik I didn't try it yet. I am a new learner of ggplot2. – Uddin Feb 23 '20 at 14:16
  • 1
    Are you looking for the same graph than in your recent question: https://stackoverflow.com/questions/60364056/how-to-make-similar-plots-using-ggplot2-in-r/60364740#60364740 ? Data looks very similar – dc37 Feb 23 '20 at 17:35
  • Yes, got the answer – Uddin Feb 23 '20 at 18:08

0 Answers0