I have a df with two variables, genotype and treatment; in a second dataframe I already have significance letters associated with the combination of both variables. So genotype1:treatment1 might be A, genotype1:treatment2 B etc.
I would like the letters to be plotted below or above the corresponding genotype boxplot.
Example code:
# Load the necessary library
library(ggplot2)
# Example data (replace with your own data)
set.seed(123)
treatment <- rep(c("Treatment1", "Treatment2"), each = 25)
genotype <- rep(c("Genotype1", "Genotype2", "Genotype3", "Genotype4", "Genotype5"), times = 10)
value <- rnorm(50, mean = 10, sd = 2)
data <- data.frame(treatment, genotype, value)
# Example dataframe with letters for significance
significance_df <- data.frame(
treatment = c("Treatment1", "Treatment1", "Treatment2", "Treatment2"),
genotype = c("Genotype1", "Genotype2", "Genotype3", "Genotype4"),
letters = c("A", "B", "C", "D")
)
# Calculate x-coordinate for each letter
significance_df$x_pos <- with(significance_df, ave(as.numeric(treatment), treatment, FUN = function(x) seq_along(x)))
# Create the boxplot
ggplot(data, aes(x = treatment, y = value, fill = genotype)) +
geom_boxplot() +
labs(x = "Treatment", y = "Value", title = "Boxplot of Treatment vs Value by Genotype") +
scale_fill_manual(values = c("Genotype1" = "blue", "Genotype2" = "red", "Genotype3" = "green", "Genotype4" = "orange", "Genotype5" = "purple")) +
theme_minimal() +
geom_text(data = significance_df, aes(label = letters, y = 7, x = x_pos), vjust = -1)
Unfortunatelly this results in the letters stacking on the treatment positions making them indistinguishable.
Is there a way to control the position of the letters on the x direction to make them correspond with the boxplots?