0
votes

I would like to add summary statistics on a box plot at the max of a dynamic y axis.

In the real data the y axis is a dynamic dropdown, one value is between 0 - 6; and the other between 0 - 100. In the example below I have hard coded where I would like the labels to be, but I cannot hard code them in the real data.

Is there a way to either:

Set labels outside the graph above the y axis? So that the labels will not move even if the axis changes?

Or is there a way to set it to max of Y + n?

Example:

# library
library(ggplot2)
library(ggpubr)

# create a data frame
variety=rep(LETTERS[1:7], each=40)
treatment=rep(c("high","low"),each=20)
note=seq(1:280)+sample(1:150, 280, replace=T)
data=data.frame(variety, treatment ,  note)

# grouped boxplot
ggplot(data, aes(x = variety, y = note, fill = treatment)) +
  geom_boxplot() +
  scale_fill_manual(values = c("#79AAB9", "#467786")) +
  stat_compare_means(aes(group = treatment), label = "p.format") +
  stat_summary(
    fun.data = function(x)
      data.frame(y = 460, label = paste(round(median(
        x
      ), 1))),
    geom = "text",
    aes(group = treatment),
    hjust = 0.5,
    position = position_dodge(0.9)
  ) +
  stat_summary(
    fun.data = function(x)
      data.frame(y = 445, label = paste("n", length(x))),
    geom = "text",
    aes(group = treatment),
    hjust = 0.5,
    position = position_dodge(0.9)
  ) +
  expand_limits(y = 100)

Boxplot example

Thanks so much for any help in advance.

1
‘label.y=inf’? Look at some options here for ideas. I quite like ggpmisc for similar stuff.Mark Neal
Thanks @MarkNeal thats really helpful. I managed to get the above example working with INF. I didn't know about this before, really grateful for your time.Keelin
Glad it helped. Don’t forget to upvote comments and answers that are particularly helpful.Mark Neal

1 Answers

1
votes

Managed to get the following working with suggestion from @MarkNeal

# library
library(ggplot2)
library(ggpubr)

# create a data frame
variety=rep(LETTERS[1:7], each=40)
treatment=rep(c("high","low"),each=20)
note=seq(1:280)+sample(1:150, 280, replace=T)
data=data.frame(variety, treatment ,  note)

# grouped boxplot
ggplot(data, aes(x = variety, y = note, fill = treatment)) +
  geom_boxplot() +
  scale_fill_manual(values = c("#79AAB9", "#467786")) +
  stat_compare_means(aes(group = treatment), label = "p.format", vjust = 3) +
  stat_summary(
    fun.data = function(x)
      data.frame(y= Inf, label = paste(round(median(
        x
      ), 1))),
    geom = "text",
    aes(group = treatment),
    hjust = 0.5, vjust = 1,
    position = position_dodge(0.9)
  ) +
  stat_summary(
    fun.data = function(x)
      data.frame(y = Inf, label = paste("n", length(x))),
    geom = "text",
    aes(group = treatment),
    hjust = 0.5, vjust = 2,
    position = position_dodge(0.9)
  )

enter image description here