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

时间:2016-05-19 17:16:51

标签: r ggplot2 numbers boxplot annotate

我想在每个箱图下面添加观察次数(如图所示 - 不需要红色方块)。 :) 但是,我不知道如何注释这种类型的箱线图(见下图)。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 个答案:

答案 0 :(得分:4)

以下是使用内置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轴的范围以为标签腾出空间。

更新:我能够重现您报告的错误,但我不确定如何修复它。相反,您可以预先汇总数据,然后将其添加到绘图中。这是一个例子:

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

答案 1 :(得分:1)

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