我正在使用ggplot2
并且已经构造了一个像这样的箱线图:
它是使用以下数据和代码构造的:
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_text
,position_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] 删除。
我来说两句