1

I added the significance letters to this graph in powerpoint. I would like to add the significance letters above each box in R. Can I modify my ggplot code to include letters above each box?

Boxplot

code:

p1 <- ggplot(beta_data, 
             aes(x=reorder(interaction, distwu,FUN = median),
                            y=distwu, fill=interaction)) +
  geom_boxplot()+ 
  theme(legend.position="none", axis.text.x = element_text(angle = 45, hjust = 1))  +
  labs(x="Treatment interaction", y = "Distance to centroid") + 
  ylim(0,1.0)
Axeman
  • 32,068
  • 8
  • 81
  • 94
Becca
  • 107
  • 2
  • 8
  • 2
    You would need to create an additional dataframe with the following columns: 1. The items listed on the x-axis (call that column x); 2. The y-value (which would be the top quartile, plus a value to push it above the edge of the box - call that column y); 3. The text you want displayed for each point on the axis (call that column label). Then you need to add the following layer to your `ggplot` code: `geom_text(data = newdf, aes(x = x, y = y, label = label)`. – Phil Dec 30 '17 at 01:22

1 Answers1

3

Make life simple with automatic placement by stat_summary. You didn't share any data, so here's an example:

ggplot(mtcars, aes(factor(cyl), mpg)) +
  geom_boxplot() +
  stat_summary(geom = 'text', label = letters[1:3], fun.y = max, vjust = -1)

enter image description here

Axeman
  • 32,068
  • 8
  • 81
  • 94