0

Hopefully a pretty simply problem to solve - I have produced the boxplot pictured using the following code

ggplot(minanalysis, aes(x=MinAnaType, y=Calcium)) +
  geom_boxplot() +
  geom_point(aes(colour=Ploidy)) +
  stat_summary(fun.y=mean, colour="black", geom="point", size=2,show.legend = FALSE) + 
  geom_text(data = means, vjust = -0.25, hjust=-0.3, size = 3, aes(label = round(Calcium, 2), 
                                                   y = Calcium + 0.08)) +
  #add a sample size n =
  geom_text(data = minanalysis %>% group_by(MinAnaType) %>% 
          summarize(Count = n(), 
          Calcium=max(Calcium) + 0.05 * diff(range(minanalysis$Calcium))),
          aes(label = paste0("n = ", Count)), 
          position = position_dodge(0.85), size=3, show.legend = FALSE) +
  # Add pairwise comparisons p-value and global p-value
  stat_compare_means(comparisons = my_comparisons, label = "p.signif", label.y = 9500, size = 4) + 
  stat_compare_means(label.y = 10500, size = 4)    

What I want is for the "n=290" and n=50" to be located underneath x axis labels 'Eviscerated' and 'WholeBody'. Having them above the boxplots themselves is too messy. Could somebody help with the code for this, I'm new to R studio.

Thanks

Harry H-W
  • 33
  • 4

1 Answers1

0

The easiest is probably to add a linebreak to your x labels. You can then add your count, either hardcoded, or programmatically. (This would be within scale_x_...(labels = ...))

E.g.

library(tidyverse)

ggplot(filter(iris, Species %in% c('setosa','virginica')), aes(Species, Sepal.Length)) +
  geom_boxplot() +
  scale_x_discrete(labels = c(setosa = paste('setosa', '\nn = 30'), 
                              virginica = paste('virginica', '\nn = 30')))
tjebo
  • 21,977
  • 7
  • 58
  • 94
  • Hi Tjebo, thank you for the reply but unfortunately I don't understand how to adapt your solution to my code above. – Harry H-W Feb 18 '20 at 14:09
  • @HarryH-W please read `https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example`. It is very difficult to help you without you providing such a working example – tjebo Feb 19 '20 at 09:20