首页 文章

在ggplot中注释分组的boxplot - 在boxplot下面添加观察数量

提问于
浏览
2

我想在每个箱图下面添加观察数量(如图所示 - 不需要红色方块) . :)但是,我不知道如何注释这种类型的boxplot(见下图) . multiple boxplot annotate number of observations

有谁知道怎么做?

这是我用来绘制这个数字的代码 .

ggplot(data=MIOT1, aes(stage, time, fill=resp)) +
geom_boxplot(color= "black", lwd=0.3) +
stat_summary(fun.y=mean, geom="point", shape=0, size=1, colour="black", position=position_dodge(width=0.75)) +
scale_fill_manual(values=c("grey25", "grey50", "grey67")) +
annotation_custom(mygrobA) +
scale_y_continuous(limits=c(-10,124)) +
theme(panel.grid.major = element_blank(),
    panel.grid.minor = element_blank(),
    strip.background = element_rect(colour="black"),
    panel.border = element_rect(colour = "black", fill="transparent")) +
xlab(bquote(' ')) +
ylab(bquote('Minimum Consecutive Time (s)')) +
labs(title="SATIATION\n") +
theme(axis.title.y = element_text(colour="black",size=10,face="bold"),
    axis.text.x = element_text(colour="black",size=8, face="plain"),
    axis.text.y = element_text(colour="black",size=8, face="plain"),  
    axis.title.x = element_text(colour="black",size=10,face="bold")) +  
theme(panel.background = element_rect(fill = "white")) +
theme(plot.title = element_text(lineheight=.8, size=10, face="bold")) +
theme(legend.title=element_blank(), legend.key = element_rect(fill = NA, colour = NA)) +
theme(legend.position="none") +
theme(legend.background = element_rect(fill=NA)) +
theme(plot.margin = unit(c(.25,.25,.0,.0), "cm"))<i>

示例数据MIOT1是一个数字变量(y轴),我正在考虑两个分组因素(开发阶段-x轴)和响应(无响应,沿海,泻湖) .

就像是

stage  resp  time
pre       U      100
pre       U      80
pre       U       50
pre       C       20
flex       U      80
flex       U       90
flex       C       10
flex        C      20
post      U        40
post      U        30
post      U        60
post       C      80
post      C       100
post       L       50
post       L       40

谢谢!佩德罗

2 回答

  • 3

    以下是使用内置 mtcars 数据框执行此操作的简单示例:

    ggplot(mtcars, aes(factor(cyl), mpg)) + 
      geom_boxplot() +
      geom_text(stat="count", aes(label=..count..), y=min(mtcars$mpg)- 0.6)
    

    在你的情况下,它会是这样的

    ggplot(data=MIOT1, aes(stage, time, fill=resp)) +
      geom_boxplot(color= "black", lwd=0.3) +
      geom_text(stat="count", aes(label=..count..), y=min(MIOT1$time))
    

    您可能需要调整文本标签的 y 位置,您可能还需要调整y轴的范围以为标签腾出空间 .

    UPDATE: 我能够重现您报告的错误,但我举了一个例子:

    library(dplyr)
    
    # Get counts by desired grouping variables
    counts = mtcars %>% group_by(cyl, am) %>% tally
    
    
    ggplot(mtcars, aes(factor(cyl), mpg, fill=factor(am))) + 
      geom_boxplot(position=position_dodge(0.9)) +
      geom_text(data=counts, aes(label=n, y=min(mtcars$mpg) - 0.6),
            position=position_dodge(0.9))
    
  • 0

    摘要EIPI10回答了我的问题:

    library(dplyr)
    
    # Get counts by desired grouping variables
        counts = mtcars %>% group_by(cyl, am) %>% tally
    
    
        ggplot(mtcars, aes(factor(cyl), mpg, fill=factor(am))) + 
        geom_boxplot(position=position_dodge(0.9)) +
        geom_text(data=counts, aes(label=n, y=min(mtcars$mpg) - 0.6), position=position_dodge(0.9)
    

相关问题