使用geom_boxplot,facet_grid时,将注释或文本添加到各个箱形图 [英] Add annotation or text to individual box plots when using geom_boxplot, facet_grid

查看:100
本文介绍了使用geom_boxplot,facet_grid时,将注释或文本添加到各个箱形图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 ggplot2 并已构建了如下的箱形图:

I am using ggplot2 and have constructed a boxplot like so:

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

This was constructed using the following data and code:

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".但是,我似乎只能使字母在每个构面中都重复,或者在单个构面中都重复.

I would now like to add small letters e.g. a, b, c above some of the boxplots to denote statistical significance. For example, an "a" above the first box in the first facet and a "b" above the second and third box in the second facet. However, i can only seem to get letters repeated in every facet or all letters in a single facet.

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

Amongst many other things that didn't work at all, I have come quite close with these attempts:

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" ....的某个地方更好地指定构面,如果我只想在一个构面中进行注释,而不是在多个构面中进行标注.

I feel I need to specify the facet better somewhere "id=factor("apple".... works if I only want annotation in one facet, but not multiple.

推荐答案

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

A common way to do this is to create a new dataset that contains the plotting information. This dataset should contain information for each of your three grouping variables (fruit, group, and treatment in order to get the text placed properly. It will also include a variable for the labels you want to add for each boxplot, and the y position of where the labels should be placed above each boxplot.

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

Here is an example of how you might make this dataset. I'm using a few function from dplyr to get the three grouping variables into a dataset in the same order as the plot (ordered by fruit, then group, then treatment).

library(dplyr)

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

现在添加y位置.您可能希望每个盒子的放置位置都不同,但是在这里,我使用的是原始数据集中的 conc 的最大值,再加上一点.

Now add in the y location. You might want a different placement for each box but here I'm using the max of conc from the original dataset plus a little bit more.

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

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

Now the labels. This would be easiest if you had additional information stored somewhere to tell you which labels go on which boxplots. Here I've written it out by hand to add an "a" for the first box in the first panel and a "b" for the middle two boxes in the second panel, being careful to match the order of prot2. I've included the NA for everything else so I can use position_dodge in geom_text.

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

现在使用 position_dodge 通过 geom_text 将文本添加到绘图中,以使内容正确排列.我必须更改y限制,以便显示标签,您可以在 scale_y_continuous 之前进行此操作.

Now add the text to the plot via geom_text, using position_dodge to get things lined up appropriately. I had to change the y limits so the labels would show, which you could do earlier in scale_y_continuous.

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

这篇关于使用geom_boxplot,facet_grid时,将注释或文本添加到各个箱形图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆