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)
1
  • 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). Commented Dec 30, 2017 at 1:22

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.