rggplot2groupinggrouped-bar-chart

Issue with titles in ggplot multiple grouping bar


I have a code like this:

library(reshape2)
library(ggplot2)

df <- read.csv("B5.csv", stringsAsFactors=FALSE, header=TRUE)

df.long<-melt(df,id.vars=c("ID","Type","Annee"))

df.long$value <- gsub(" ", "", df.long$value)
df.long$value <- sub("O", "0", df.long$value)
df.long$value <- as.numeric(df.long$value)

myplot =ggplot(df.long,aes(variable,value,fill=as.factor(Annee)))+
   geom_bar(position="dodge",stat="identity")+
   ylab("Simulation Progress (%)") +
   facet_wrap(~Type,nrow=3)

myplot + theme(panel.grid.major = element_blank(), legend.title=element_blank(), panel.grid.minor = element_blank(),
panel.background = element_blank(), axis.title.x = element_blank(), axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1),
axis.line = element_line(colour = "black"))

My issue is that title on the top left plot is not fitting in the gray box. How can I fix that? Also it might be good to have a vertical line dividing two plots on the left from two plots on the right. How that can be done?

> dput(df)
structure(list(ID = 1:8, Type = c("gmx mdrun -ntmpi 8 -ntomp 1 -s benchPEP.tpr -nsteps 10000 -resethway",
"gmx mdrun -ntmpi 8 -ntomp 1 -s benchPEP.tpr -nsteps 10000 -resethway",
"gmx mdrun -ntmpi 8 -s benchPEP.tpr -nsteps 4000 -resetstep 3000",
"gmx mdrun -ntmpi 8 -s benchPEP.tpr -nsteps 4000 -resetstep 3000",
"gmx mdrun -ntmpi 8 -s benchPEP.tpr -nsteps -1 -maxh 1.0 -resethway",
"gmx mdrun -ntmpi 8 -s benchPEP.tpr -nsteps -1 -maxh 1.0 -resethway",
"gmx mdrun -ntmpi 8 -ntomp 1 -s benchPEP.tpr -nsteps -1 -maxh 1.0 -resethway -noconfout",
"gmx mdrun -ntmpi 8 -ntomp 1 -s benchPEP.tpr -nsteps -1 -maxh 1.0 -resethway -noconfout"
), Annee = c("SYCL", "CUDA", "SYCL", "CUDA", "SYCL", "CUDA",
"SYCL", "CUDA"), Domain.decomp. = c("2. 1", "2", "2. 1", "2. 1",
"2.1", "2", "2. 1", "2"), DD.com..load = c(0, 0, 0, 0, 3.7, 3,
0, 0), Neighbor.search = c("3.7", "3. 1", "3.7", "3.9", "0. 1",
"O. 1", "3.5", "3. 1"), Launch.PP.GPU.ops. = c("0. 1", "0", "0.2",
"0", "1 .6", "1 . 5", "0.2", "0. 1"), Comm..coord. = c("1 .6",
"1 .0", "1 .5", "1 .3", "1 .5", "1 .3", "1 . 5", "1 .6"), Force = c("1 . 5",
"1 .2", "1 .4", "1 .2", "1 .3", "1 . 1", "1 .5", "1 .2"), Wait...Comm..F = c("1 .3",
"1 .7", "1 .2", "1 .0", "66.7", "68.8", "1 .2", "1 .2"), PIE.mesh = c("65.6",
"70.9", "61 .0", "61 .4", "0", "0", "67.6", "69.2"), Wait.Bonded.GPU = c(0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L), wait.GPU.NB.nonloc. = c(0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L), Wait.GPU.NB.local = c(0, 0, 0, 0, 7.4,
5.7, 0, 0), NB.X.F.buffer.ops. = c("7.3", "4.4", "6. 7", "5",
"0. 1", "0. 1", "7.2", "5.5"), Write.traje = c("0.3", "0.3",
"1 .2", "1 .3", "6.4", "6. 1", "O. 1", "0. 1"), Update = c(6.3,
4.3, 5.7, 4.9, 8.2, 9.5, 6.2, 5.6), Constraints = c("8.9", "9.7",
"1 1 .6", "13.3", "0.3", "0.4", "8. 1", "9.5"), Comm..energies = c("0.9",
"0.9", "3.3", "3.9", "8.4", "8. 5", "0.3", "0.4"), PIE.redist..X.F = c("8. 1",
"8.7", "7.9", "7.4", "29.9", "30.1", "8. 1", "8. 1"), PIE.spread = c("29.7",
"30.6", "27.2", "29.6", "20.3", "20.2", "30. 1", "30.4"), PIE.gather = c("19.9",
"21 .3", "18.7", "19", "6.4", "8.4", "20", "20.6"), PIE.3D.FFT = c("6",
"8.6", "5.7", "4.3", "1 .0", "1 .1", "7.6", "8.4"), PIE.3D.FFT.comm. = c("1 .2",
"1 .0", "0.9", "0.7", "1 .2", "0. 5", "1 .0", "1 .1"), PIE.solve.Elec = c(0.7,
0.5, 0.6, 0.3, 0.7, 0.5, 0.7, 0.5)), class = "data.frame", row.names = c(NA,
-8L))

enter image description here


Solution

  • Here's something you could work on as a starter:

    library(reshape2)
    library(ggplot2)
    
    df.long<-melt(df,id.vars=c("ID","Type","Annee"))
    
    df.long$value <- gsub(" ", "", df.long$value)
    df.long$value <- sub("O", "0", df.long$value)
    df.long$value <- as.numeric(df.long$value)
    
    myplot <-  ggplot(df.long,aes(variable,value,fill=as.factor(Annee)))+
      geom_bar(position="dodge",stat="identity")+
      ylab("Simulation Progress (%)") +
      facet_wrap(~Type,nrow=3, labeller = label_wrap_gen(width = 25)) + 
      theme_bw() +
      theme(panel.grid = element_blank(),
            panel.spacing = unit(0, "mm"),
            axis.title.x = element_blank(), 
            axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1))
    
    myplot
    

    Created on 2023-06-19 with reprex v2.0.2