使用geom_boxplot,facet_grid时,将注释或文本添加到各个箱形图

丽贝卡

我正在使用ggplot2并且已经构造了一个像这样的箱线图:

水果ggplot

它是使用以下数据和代码构造的:

structure(list(group = c("C", "C", "C", "C", "C", "C", "C", "C", 
"C", "C", "V", "V", "V", "V", "V", "V", "V", "V", "V", "V", "C", 
"C", "C", "C", "C", "C", "C", "C", "C", "V", "V", "V", "V", "V", 
"V", "V", "V", "V", "V", "C", "C", "C", "C", "C", "V", "V", "V", 
"V", "V", "V", "V", "V", "V", "V", "C", "C", "C", "C", "C", "C", 
"C", "C", "C", "C", "C", "C", "C", "C", "C", "C", "C", "C", "C", 
"C", "V", "V", "V", "V", "V", "V", "V", "V", "V", "V", "C", "C", 
"C", "C", "C", "C", "C", "C", "C", "C", "V", "V", "V", "V", "V", 
"V", "V", "V", "V", "V", "C", "C", "C", "C", "C", "C", "C", "C", 
"C", "V", "V", "V", "V", "V", "V", "V", "V", "V", "V"), fruit = c("apple", 
"apple", "apple", "apple", "apple", "apple", "apple", "apple", 
"apple", "apple", "apple", "apple", "apple", "apple", "apple", 
"apple", "apple", "apple", "apple", "apple", "apple", "apple", 
"apple", "apple", "apple", "apple", "apple", "apple", "apple", 
"apple", "apple", "apple", "apple", "apple", "apple", "apple", 
"apple", "apple", "apple", "apple", "apple", "apple", "apple", 
"apple", "orange", "orange", "orange", "orange", "orange", "orange", 
"orange", "orange", "orange", "orange", "orange", "orange", "orange", 
"orange", "orange", "orange", "orange", "orange", "orange", "orange", 
"orange", "orange", "orange", "orange", "orange", "orange", "orange", 
"orange", "orange", "orange", "orange", "orange", "orange", "orange", 
"orange", "orange", "orange", "orange", "orange", "orange", "orange", 
"orange", "orange", "orange", "orange", "pear", "pear", "pear", 
"pear", "pear", "pear", "pear", "pear", "pear", "pear", "pear", 
"pear", "pear", "pear", "pear", "pear", "pear", "pear", "pear", 
"pear", "pear", "pear", "pear", "pear", "pear", "pear", "pear", 
"pear", "pear", "pear", "pear", "pear", "pear", "pear"), treatment =  c("pre", 
"pre", "pre", "pre", "pre", "post", "post", "post", "post", "post", 
"pre", "pre", "pre", "pre", "pre", "post", "post", "post", "post", 
"post", "pre", "pre", "pre", "pre", "pre", "post", "post", "post", 
"post", "pre", "pre", "pre", "pre", "pre", "post", "post", "post", 
"post", "post", "pre", "pre", "pre", "pre", "pre", "pre", "pre", 
"pre", "pre", "pre", "post", "post", "post", "post", "post", 
"pre", "pre", "pre", "pre", "pre", "post", "post", "post", "post", 
"post", "pre", "pre", "pre", "pre", "pre", "post", "post", "post", 
"post", "post", "pre", "pre", "pre", "pre", "pre", "post", "post", 
"post", "post", "post", "pre", "pre", "pre", "pre", "pre", "pre", 
"pre", "pre", "pre", "pre", "pre", "pre", "pre", "pre", "pre", 
"post", "post", "post", "post", "post", "pre", "pre", "pre", 
"pre", "pre", "post", "post", "post", "post", "pre", "pre", "pre", 
"pre", "pre", "post", "post", "post", "post", "post"), subgroup = c(66, 
66, 66, 66, 66, 66, 66, 66, 66, 66, 67, 67, 67, 67, 67, 67, 67, 
67, 67, 67, 68, 68, 68, 68, 68, 68, 68, 68, 68, 69, 69, 69, 69, 
69, 69, 69, 69, 69, 69, 70, 70, 70, 70, 70, 71, 71, 71, 71, 71, 
71, 71, 71, 71, 71, 72, 72, 72, 72, 72, 72, 72, 72, 72, 72, 73, 
73, 73, 73, 73, 73, 73, 73, 73, 73, 74, 74, 74, 74, 74, 74, 74, 
74, 74, 74, 75, 75, 75, 75, 75, 76, 76, 76, 76, 76, 77, 77, 77, 
77, 77, 77, 77, 77, 77, 77, 78, 78, 78, 78, 78, 78, 78, 78, 78, 
79, 79, 79, 79, 79, 79, 79, 79, 79, 79), conc = c(0.074, 0.204, 
0.118, 0.25, 0.159, 0.283, 0.253, 0.436, 0.295, 0.335, 0.204, 
0.424, 0.254, 0.217, 0.281, 0.153, 0.309, 0.184, 0.341, 0.168, 
0.14, 0.213, 0.29, 0.266, 0.522, 0.219, 0.34, 0.317, 0.053, 0.196, 
0.072, 0.185, 0.269, 0.157, 0.344, 0.398, 0.304, 0.173, 0.212, 
0.197, 0.154, 0.289, 0.212, 0.217, 0.314, 0.337, 0.116, 0.209, 
0.256, 0.165, 0.216, 0.284, 0.148, 0.151, 0.105, 0.097, 0.201, 
0.125, 0.208, 0.334, 0.209, 0.269, 0.388, 0.182, 0.12, 0.28, 
0.253, 0.226, 0.257, 0.193, 0.314, 0.262, 0.12, 0.415, 0.2, 0.209, 
0.202, 0.08, 0.341, 0.221, 0.598, 0.327, 0.258, 0.655, 0.071,  
0.209, 0.234, 0.19, 0.117, -0.01, 0.074, 0.038, 0.06, 0.088, 
0.034, 0.057, 0.099, 0.046, 0.096, 0.166, 0.045, 0.104, 0.08, 
0.248, 0.031, 0.056, 0.101, 0.077, 0.069, 0.251, 0.585, 0.427, 
0.145, 0.109, 0.125, 0.079, 0.068, 0.064, 0.184, 0.202, 0.263, 
0.302, 0.136)), .Names = c("group", "fruit", "treatment", "subgroup", 
"conc"), row.names = c(NA, -123L), class = "data.frame")

require(ggplot2)
plot<-ggplot(prot, aes(x=group, y=conc, fill=factor(treatment)))+ 
geom_boxplot() + 
scale_fill_manual(values=cbPalette)+
theme_bw()+
facet_grid(.~fruit)+    
labs(fill="treatment status", y="Concentration (mg.ml-1) ") +
theme(legend.position="bottom", axis.title.y=element_text(size=12, hjust=0.5, vjust=1, face="bold"), axis.text.y=element_text(size=8), axis.title.x=element_text(size=12, hjust=0.5, vjust=0.5, face="bold"), axis.text.x=element_text(size=8))  +
scale_y_continuous(limits=c(-0.01,0.7), breaks=seq(0,0.7,0.1))

我现在想在一些方框图中添加小写字母,例如a,b,c,以表示统计意义。例如,在第一个构面的第一个框上方的“ a”和在第二个构面的第二个和第三框上方的“ b”。但是,我似乎只能使字母在每个方面或单个方面的所有字母重复出现。

在许多其他根本不起作用的事情中,我与这些尝试非常接近:

plot+ annotate("text", label =c("a", "b", "c"), size = 4, x =c(2,1,2), y = c(0.5, 0.3, 0.4))

和:

df1<-data.frame(group=c(0.8, 1.2, 1.8, 2.2), conc=c(0.4, 0.4), lab="text", id=factor("apple", levels=c("apple", "orange", "pear")), treatment=factor(2, levels=c("pre", "post")))

plot+ geom_text(data=df1, label=c("a", "b", "a", "d"))

我觉得我需要在“ id = factor(“ apple” ....“某处指定更好的构面,如果我只想在一个构面中进行注释,而不是多个。

奥史密斯

一种常见的方法是创建一个包含绘图信息的新数据集。此数据集应包含有关三个分组变量(水果,分组和处理)中每个变量的信息,以便正确放置文本。还应包含要为每个箱线图添加的标签的变量,以及该变量的y位置。标签应放置在每个箱线图的上方。

这是如何制作此数据集的示例。我正在使用dplyr中的一些函数,将三个分组变量按与情节相同的顺序放入数据集中(先按水果,然后分组,然后进行处理)。

library(dplyr)

prot2 = distinct(prot, fruit, group, treatment) %>%
    arrange(fruit, group, treatment)

现在添加y位置。您可能需要为每个框分配不同的位置,但是在这里,我使用的conc是原始数据集中的最大值,再加上更多的值。

prot2$yloc = max(prot$conc) + .05

现在是标签。如果您将其他信息存储在某处,以告诉您哪些标签贴在哪些箱形图上,这将是最简单的。在这里,我手动编写了代码,在第一个面板的第一个方框中添加了一个“ a”,在第二个面板的中间两个方框中添加了一个“ b”,请小心匹配prot2我已经将NA包含在其他所有内容中,因此可以position_dodge中使用geom_text

prot2$label = c("a", NA, NA, NA, NA, "b", "b", NA, NA, NA, NA, NA)

现在,通过将该文本添加到绘图中geom_textposition_dodge以使内容正确排列。我必须更改y限制,以便显示标签,您可以在早些时候进行操作scale_y_continuous

plot + 
    ylim(NA, max(prot$conc) + .05) +
    geom_text(data = prot2, aes(y = yloc, label = label), 
               position = position_dodge(width = .75))

在此处输入图片说明

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

将geom_boxplot与facet_grid和free_y一起使用

将geom_boxplot图例添加到geom_violin图图例中

使用facet_grid在geom_boxplot上的Tukey测试结果

将geom_text添加到2D facet_grid ggplot

将标签添加到facet_wrap箱图

如何使用ggplot_build和ggplot_gtable调整facet_grid框架和箱形图之间的距离

重新排列facet_grid并将其添加到ggplot2的geom_bar中

如何使用ggplot2将标准误差线添加到箱形图和晶须图中?

使用ggplot facet_grid R图添加图例

将两个边距都添加到facet_grid中,但不添加组合

使用R / ggplot2将标签添加到geom_bar()中的各个%

将图像注释添加到箱线图

将geom_smooth添加到箱线图

将观测值数量添加到水平箱图

使用plotly中的标记将抖动添加到箱形图中

使用R中的标记将抖动添加到分组的箱形图中

将累积数量添加到使用facet_wrap绘制的geom_bar图中

将基于新数据的折线图添加到一系列箱形图

在Matplotlib中将密度或箱形图添加到图的边距

将副标题文本添加到 grid.table 图

ggplot2 - 使用 geom_boxplot 生成带有自定义胡须的水平箱线图时出错

如何使用场景将注释添加到scatter3D Plotly图?

使用facet_grid和多个数据点(R ggplot2)时,将geom_point和geom_line组合

使用 ggplot2 将颜色列添加到 geom_bar 图

使用geom_rect将阴影矩形添加到现有图

使用 facet_grid 时如何使 geom_rect 具有一致的阴影?

如何将facet_grid()与geom_histogram()结合使用

使用免费软件将文本注释和图像添加到PDF文件

当使用facet_wrap应用于箱形图时,删除一个tableGrob