rggplot2ggpubrggboxplot

Add custom tick on and a label y-axis with custom colour using ggboxplot()


I have a boxplot that I've created using ggboxplot. On it I have a horizontal line at the mark y=50. I want to be able to add a label at y=50, x=0 saying "50" in dark green to match the line. Currently the label overlaps the axis line and the boxplots have shifted to the right. Any suggestions?

Problem

enter image description here

Current Code

symnum.args <- list(cutpoints = c(0, 0.0001, 0.001, 0.01, 0.05, 1), symbols = c("", "", "", "", "ns"))

my_comparisons<- list(c('BVR3', 'BVR4'), c('BVR6', 'BVR1'), c('BVR6', 'BVR2'), c('BVR6', 'BVR4'), c('BVR6', 'BVR5'))
plot <- ggboxplot(TPdat, x = "loc", y = "TP", 
          ylab = "Median Annual Total Phosphorus (\U00B5g/L)", xlab = "Sampling Site", 
          outlier.shape=NA, ylim=c(0, 925)) +
          scale_x_discrete(labels=c('1','2','3','4','5','6')) +
  theme(axis.text=element_text(size=16, family='Calibri'),
        axis.title=element_text(size=16, family='Calibri')) +
  geom_hline(yintercept=50, col='dark green', linetype=2) +
  coord_cartesian(clip='off', ylim=c(0, NA)) +
  annotate("text", x=0, y=50, label="50", colour='dark green', size=5) +
  stat_compare_means(comparisons=my_comparisons, label='p.signif', 
                     symnum.args = symnum.args, 
                     tip.length = c(0.01,0.01,0.01,0.01,0.01), 
                     label.y=c(800, 825, 850, 875, 900)) +
  stat_compare_means(label.y=930, family='Calibri', size=5)

head of dataframe

structure(list(loc = structure(c(1L, 1L, 1L, 1L, 1L, 1L), levels = c("BVR1", 
"BVR2", "BVR3", "BVR4", "BVR5", "BVR6"), class = "factor"), TP = c(76L, 
71L, 86L, 48L, 58L, 73L)), row.names = c(NA, 6L), class = "data.frame")

Solution

  • Based on alistaire's answer suggested here Change color of specific tick in ggplot2 we could do:

    library(ggplot2)
    
        ggplot(iris, aes(x=Species, y=Sepal.Length)) + 
          geom_boxplot() +
          scale_y_continuous(breaks=c(5,6,7,7.5,8), 
                             labels=expression("5", "6", "7", "7.5", "8")) +
          geom_hline(yintercept=7.5, col='dark green', linetype=2) +
          theme(axis.text.y = element_text(colour = c('black', 'black','black', 'dark green', 'black')))
    
    

    I used the iris dataset to ilustrate how to get one specifick tickmark colored green. Hope it helps

    Boxplot with one tickmark green:

    enter image description here

    This method works with ggpubr, too:

    ggboxplot(iris, x = "Species", y = "Sepal.Length") +
      scale_y_continuous(breaks=c(5,6,7,7.5,8), 
                         labels=expression("5", "6", "7", "7.5", "8")) +
      geom_hline(yintercept=7.5, col='dark green', linetype=2) +
      theme(axis.text.y = element_text(colour = c('black', 'black','black', 'dark green', 'black')))