rggplot2sample-size

Print sample size outside of boxplot boundary with ggplot in R


I'd like to display the sample size of each species on my boxplot, but the plot is fairly cluttered. Is there a way to display the sample size on the "outside" of the plot with "N" as the header (sample size should go in the red area I highlighted)?

enter image description here

df <- data.frame(year = rep(c(2019, 2020), each = 10),
                 month = rep(c("March", "October"), each = 1), 
                 site = rep(c("1", "2", "3", "4", "5"), each = 2),
                 type = rep(c("baitfish", "shark"), each = 1),
                 salinity = sample(x = 20:35, size  = 20, replace = TRUE),
                 num = sample(x = 0:10, size  = 20, replace = TRUE))

count_by_row <- data.frame(df[rep(row.names(df), df$num), 1:5])
    
    
    ggplot(data=subset(count_by_row, !is.na(salinity)), aes(x = reorder(type, -salinity, FUN = median), y = salinity)) +
      geom_boxplot(outlier.shape = 1, outlier.size = 2) +
      coord_flip() +
      xlab("") +
      ylab("salinity (PSU)")

Solution

  • You could add a custom annotation after increasing the right margin:

    ggplot(data=subset(count_by_row, !is.na(salinity)), 
           aes(y = reorder(type, -salinity, FUN = median), x = salinity)) +
      geom_boxplot(outlier.shape = 1, outlier.size = 2, orientation = "y") +
      coord_cartesian(clip = "off") +
      annotation_custom(grid::textGrob(c("N", table(count_by_row$type)),
                                       x = 1.1, y = c(0.9, 0.28, 0.72),
                                       gp = grid::gpar(cex = 1.5))) +
      ylab("") +
      xlab("salinity (PSU)") +
      theme_bw(base_size = 20) +
      theme(plot.margin = margin(20, 100, 20, 20))
    

    enter image description here