简体   繁体   中英

R ggpubr Boxplot adding summary stats label to dynamic Y axis

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)

箱线图示例

Thanks so much for any help in advance.

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)
  )

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM