automatically put annotations above the violin plots

Viewed 34

Is there a way to automatically put annotations above the violin plots without manually doing it? I am making a lot of graphs and would like to avoid having to create different annotations for each plot.

raw_data <- structure(list(Type = c("EY", "EY", "EY", "EY", "EY", "EY", "EY", 
"EY", "EY", "EY", "EY", "EY", "EO", "EO", "EO", "EO", "EO", "EO", 
"EO", "EO", "EO", "EO", "EO", "EO", "EO", "EO", "EO", "EY", "EY", 
"EY", "EY", "EY", "EY", "EY", "EY", "EY", "EY", "EY", "EY", "EO", 
"EO", "EO", "EO", "EO", "EO", "EO", "EO", "EO", "EO", "EO", "EO", 
"EO", "EO", "EO", "Fe", "Fe", "Fe", "Fe", "Fe", "Fe", "Fe", "Fe", 
"Fe", "Fe", "Fe", "Fe", "Fe", "Fe", "Fe", "Fe", "Fe", "Fe", "Fe", 
"Fe", "Fe", "Fe", "Fe", "Fe", "Fe", "Fe", "Fe", "Fe", "Fe", "Fe", 
"Fe", "Fe", "Fe", "Fe", "Fe", "Fe", "Fe", "Fe", "Fe", "Fe"), 
    Maturity = structure(c(2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
    2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
    2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
    2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("Before", 
    "After"), class = "factor"), Mn = c(49.65414679, 30.42267647, 
    53.57468125, 58.38531743, 11.07088615, 45.10644112, 41.33779076, 
    18.77426257, 60.36149117, 67.0260032, 38.13116392, 69.58713059, 
    66.80825471, 30.34496525, 56.90875291, 72.37427455, 22.06855022, 
    39.26030375, 35.31067394, 24.95698024, 24.88586385, 34.65821779, 
    56.2750941, 19.56655377, 45.04117517, 44.04604616, 30.4130276, 
    15.76824315, 21.50636001, 27.25777212, 17.08214603, 14.17493713, 
    34.24593956, 12.39548126, 23.59787998, 38.42021152, 32.54843776, 
    18.47119505, 26.05687922, 32.97977895, 16.91703485, 30.0626039, 
    26.72724955, 17.69433512, 22.34314521, 16.73815161, 13.08749188, 
    19.60771131, 24.08865106, 34.57472809, 14.94650052, 32.60745327, 
    29.86439635, 20.71564742, 22.32568148, 18.37220522, 24.22830773, 
    20.91660251, 20.96486868, 34.25266591, 35.57200211, 14.5736099, 
    25.67187037, 27.7352539, 42.919751, 27.1949762, 18.94238981, 
    25.25867502, 31.66215867, 18.1734287, 24.41910112, 12.07438938, 
    12.85034127, 19.75012923, 35.96030792, 30.07986478, 38.91460672, 
    41.18492241, 41.68085289, 60.93940724, 49.77930187, 17.56806894, 
    37.13306953, 47.06926881, 85.62093648, 24.31500546, 22.049633, 
    40.42923285, 87.53339143, 37.08699364, 38.29525586, 28.00272281, 
    15.58855204, 37.14164977)), row.names = c(NA, -94L), class = c("tbl_df", 
"tbl", "data.frame"))
library(ggpmisc)
library(reshape2)
library(ggbreak)
library(writexl)
library(ggthemes)
library(ggprism)
library(patchwork)
library(lemon)
library(ggpubr)
library(tidyverse)

ggplot(raw_data, aes(x = Type, y = Mn, fill = Maturity)) +
  geom_violin(trim = FALSE, position="dodge") + 
  geom_boxplot(width=.1, outlier.shape=NA, position=position_dodge(.9)) +
  scale_fill_manual(values = c("Honeydew4", "white")) + 
  theme_classic() + 
  theme(text=element_text(size=16,  family="serif", face = "bold", color = "black")) + 
  coord_capped_cart(bottom='both', left='none', gap = 0.05) +
  theme(axis.ticks.length = unit(.3, "cm")) + labs(y="", x= "")  

enter image description here

Desired output enter image description here

1 Answers

I assumed this would have been asked before, but I can't find a duplicate on stackoverflow, so here is a potential solution:

library(tidyverse)
library(lemon)
#> 
#> Attaching package: 'lemon'
#> The following object is masked from 'package:purrr':
#> 
#>     %||%
#> The following objects are masked from 'package:ggplot2':
#> 
#>     CoordCartesian, element_render

raw_data <- structure(list(Type = c("EY", "EY", "EY", "EY", "EY", "EY", "EY", 
                                    "EY", "EY", "EY", "EY", "EY", "EO", "EO", "EO", "EO", "EO", "EO", 
                                    "EO", "EO", "EO", "EO", "EO", "EO", "EO", "EO", "EO", "EY", "EY", 
                                    "EY", "EY", "EY", "EY", "EY", "EY", "EY", "EY", "EY", "EY", "EO", 
                                    "EO", "EO", "EO", "EO", "EO", "EO", "EO", "EO", "EO", "EO", "EO", 
                                    "EO", "EO", "EO", "Fe", "Fe", "Fe", "Fe", "Fe", "Fe", "Fe", "Fe", 
                                    "Fe", "Fe", "Fe", "Fe", "Fe", "Fe", "Fe", "Fe", "Fe", "Fe", "Fe", 
                                    "Fe", "Fe", "Fe", "Fe", "Fe", "Fe", "Fe", "Fe", "Fe", "Fe", "Fe", 
                                    "Fe", "Fe", "Fe", "Fe", "Fe", "Fe", "Fe", "Fe", "Fe", "Fe"), 
                           Maturity = structure(c(2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
                                                  2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
                                                  2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
                                                  1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
                                                  1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
                                                  1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
                                                  2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("Before", 
                                                                                                      "After"), class = "factor"), Mn = c(49.65414679, 30.42267647, 
                                                                                                                                          53.57468125, 58.38531743, 11.07088615, 45.10644112, 41.33779076, 
                                                                                                                                          18.77426257, 60.36149117, 67.0260032, 38.13116392, 69.58713059, 
                                                                                                                                          66.80825471, 30.34496525, 56.90875291, 72.37427455, 22.06855022, 
                                                                                                                                          39.26030375, 35.31067394, 24.95698024, 24.88586385, 34.65821779, 
                                                                                                                                          56.2750941, 19.56655377, 45.04117517, 44.04604616, 30.4130276, 
                                                                                                                                          15.76824315, 21.50636001, 27.25777212, 17.08214603, 14.17493713, 
                                                                                                                                          34.24593956, 12.39548126, 23.59787998, 38.42021152, 32.54843776, 
                                                                                                                                          18.47119505, 26.05687922, 32.97977895, 16.91703485, 30.0626039, 
                                                                                                                                          26.72724955, 17.69433512, 22.34314521, 16.73815161, 13.08749188, 
                                                                                                                                          19.60771131, 24.08865106, 34.57472809, 14.94650052, 32.60745327, 
                                                                                                                                          29.86439635, 20.71564742, 22.32568148, 18.37220522, 24.22830773, 
                                                                                                                                          20.91660251, 20.96486868, 34.25266591, 35.57200211, 14.5736099, 
                                                                                                                                          25.67187037, 27.7352539, 42.919751, 27.1949762, 18.94238981, 
                                                                                                                                          25.25867502, 31.66215867, 18.1734287, 24.41910112, 12.07438938, 
                                                                                                                                          12.85034127, 19.75012923, 35.96030792, 30.07986478, 38.91460672, 
                                                                                                                                          41.18492241, 41.68085289, 60.93940724, 49.77930187, 17.56806894, 
                                                                                                                                          37.13306953, 47.06926881, 85.62093648, 24.31500546, 22.049633, 
                                                                                                                                          40.42923285, 87.53339143, 37.08699364, 38.29525586, 28.00272281, 
                                                                                                                                          15.58855204, 37.14164977)), row.names = c(NA, -94L), class = c("tbl_df", 
                                                                                                                                                                                                         "tbl", "data.frame"))
summary_df <- raw_data %>%
  mutate(Maturity = ifelse(Maturity == "Before", "a", "x")) %>%
  group_by(Type, Maturity) %>%
  summarise(max_val = max(density(Mn)[[1]]) + 4) %>%
  ungroup()
#> `summarise()` has grouped output by 'Type'. You can override using the
#> `.groups` argument.
  
ggplot(raw_data, aes(x = Type, y = Mn, fill = Maturity)) +
  geom_violin(trim = FALSE, position="dodge") + 
  geom_boxplot(width=.1, outlier.shape=NA, position=position_dodge(.9)) +  
  scale_fill_manual(values = c("Honeydew4", "white")) +
  scale_y_continuous(breaks = seq(0, 100, 25)) +
  theme_classic() + 
  theme(text=element_text(size=16,  family="serif", face = "bold", color = "black")) + 
  coord_capped_cart(bottom='both', left='none', gap = 0.05)+ 
  theme(axis.ticks.length = unit(.3, "cm")) + 
  labs(y="", x= "") +
  geom_text(data = summary_df,
            aes(label = Maturity,
                y = max_val,
                x = Type,
                group = Maturity),
            position = position_dodge(width = 0.9),
            check_overlap = TRUE,
            show.legend = FALSE,
            inherit.aes = FALSE)

Created on 2022-09-13 by the reprex package (v2.0.1)

Related