R ggpubr箱线图向动态Y轴添加摘要统计标签

R ggpubr箱线图向动态Y轴添加摘要统计标签,r,ggplot2,label,boxplot,ggpubr,R,Ggplot2,Label,Boxplot,Ggpubr,我想在动态y轴最大值处的方框图上添加摘要统计信息 在实际数据中,y轴是一个动态下拉列表,一个值在0-6之间;另一个在0-100之间。在下面的示例中,我已经硬编码了我希望标签的位置,但我无法在实际数据中硬编码它们 有没有办法: 在y轴上方的图形外部设置标签?这样即使轴发生变化,标签也不会移动 或者有没有办法将其设置为Y+n的最大值 例如: # library library(ggplot2) library(ggpubr) # create a data frame variety=rep(LE

我想在动态y轴最大值处的方框图上添加摘要统计信息

在实际数据中,y轴是一个动态下拉列表,一个值在0-6之间;另一个在0-100之间。在下面的示例中,我已经硬编码了我希望标签的位置,但我无法在实际数据中硬编码它们

有没有办法:

在y轴上方的图形外部设置标签?这样即使轴发生变化,标签也不会移动

或者有没有办法将其设置为Y+n的最大值

例如:

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


非常感谢您事先提供的帮助

通过@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)
  )

'label.y=inf'?看看一些想法的选择。我很喜欢ggpmisc做类似的东西。谢谢@MarkNeal这真的很有帮助。我成功地将上面的示例与INF一起使用。我以前不知道这一点,非常感谢您的时间。很高兴它起到了作用。不要忘记对特别有用的评论和答案进行投票。