Search code examples
rggplot2yaxis

How to adjust position of Y axis label to be in the middle


I have code like this:

data <- read.csv("test.csv", stringsAsFactors=FALSE, header=TRUE)

# Graph
myplot=ggplot(data, aes(fill=condition, y=value, x=condition)) + 
    geom_bar(position="dodge", stat="identity") +
    scale_fill_viridis(discrete = T, option = "E") +
    ylab("Performance (ns/day)") +
    facet_wrap(~specie) +
    theme_ipsum()

myplot + theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(), legend.title=element_blank(), 
panel.background = element_blank(), axis.title.x = element_blank(), axis.text.x=element_blank(), axis.ticks.x=element_blank(), axis.title.y = element_text(angle=90, vjust = 0.5))

and my data looks like this:

> dput(data)
structure(list(specie = c("gmx mdrun -gpu_id 1 -ntomp 16 -s benchMEM.tpr -nsteps 10000", 
"gmx mdrun -gpu_id 1 -ntomp 16 -s benchMEM.tpr -nsteps 10000", 
"gmx mdrun -gpu_id 1 -ntomp 16 -s benchMEM.tpr -nsteps 10000", 
"gmx mdrun -gpu_id 1 -ntomp 16 -s benchMEM.tpr -nsteps 10000", 
"gmx mdrun -gpu_id 1 -ntomp 16 -s MD_15NM_WATER.tpr -nsteps 10000", 
"gmx mdrun -gpu_id 1 -ntomp 16 -s MD_15NM_WATER.tpr -nsteps 10000", 
"gmx mdrun -gpu_id 1 -ntomp 16 -s MD_15NM_WATER.tpr -nsteps 10000", 
"gmx mdrun -gpu_id 1 -ntomp 16 -s MD_15NM_WATER.tpr -nsteps 10000"
), condition = c("Extreme-SYCL", "Saber-SYCL", "Extreme-CUDA", 
"Saber-CUDA", "Extreme-SYCL", "Saber-SYCL", "Extreme-CUDA", "Saber-CUDA"
), value = c(75.8, 77.771, 63.297, 78.046, 34.666, 50.052, 32.07, 
59.815)), class = "data.frame", row.names = c(NA, -8L))

When I plot this my Y axis title is on the very top of the axis. I need it to be on the middle and font to be bigger. Can you please help?enter image description here


Solution

  • Set hjust=.5 to place the title at the center of the axis and use size= to increase the font size:

    library(ggplot2)
    library(viridis)
    library(hrbrthemes)
    
    myplot <- ggplot(data, aes(fill = condition, y = value, x = condition)) +
      geom_bar(position = "dodge", stat = "identity") +
      scale_fill_viridis(discrete = T, option = "E") +
      ylab("Performance (ns/day)") +
      facet_wrap(~specie) +
      theme_ipsum()
    
    myplot + theme(
      panel.grid.major = element_blank(),
      panel.grid.minor = element_blank(),
      legend.title = element_blank(),
      panel.background = element_blank(),
      axis.title.x = element_blank(),
      axis.text.x = element_blank(),
      axis.ticks.x = element_blank(),
      axis.title.y = element_text(angle = 90, hjust = 0.5, size = 20)
    )
    

    enter image description here