如何使用显示 CLD 字母的箱线图显示 Tukey 检验的结果
How to show the results of a Tukey test with boxplots showing CLD letters
我收集了 216 个人的数据。我测量了每个人体内相同 7 种物质的浓度,用 Sub1:Sub7 表示。这些物质的浓度在不同地点的个体中可能不同。我感兴趣的是可以根据这些物质的浓度将这些人分类的精细程度。我也有兴趣了解这些物质如何相互关联,因为某些物质的浓度可能会影响其他物质的浓度。我的数据集中的每个个体都由一个唯一的 ID 号表示。三个 "nested" 分组变量(位置、州和地区)可用于区分这些个体。每个州都有多个位置,多个州是更大区域的一部分。例如,位置:APNG、BLEA 和 NEAR 中的个体都在 FL,而位置中的个体:CACT、OYLE 和 PIY 都在 GA。 FL 和 GA 都在 A 区。我用这个函数进行了方差分析:
library(tidyverse)
library(multicomp)
library(multicompView)
tests <- list()
Groups <- c(1:3)
Variables <- 6:12
for(i in Groups){
Group <- as.factor(data[[i]])
for(j in Variables)
{
test_name <- paste0(names(data)[j], "_by_", names(data[i]))
Response <- data[[j]]
sublist <- list()
sublist$aov <- aov(Response ~ Group)
sublist$tukey <- TukeyHSD(sublist$aov)
sublist$multcomp <- multcompLetters(extract_p(sublist$tukey$Group))
tests[[test_name]] <- sublist
}
}
#i can access the results like this:
lapply(tests, function(x) summary(x$aov))
#and access the compact letter display results like this:
lapply(tests, function(x) x$multcomp)
使用对象 tests
,我如何告诉 R 创建 TukeyHSD
结果的箱线图并显示 CLD 字母并将图粘贴到 pdf 上?
这个网站:r-graph-gallery.com/84-tukey-test.html 解释了如何做到这一点,但我无法让它与对象一起工作 tests
.
这是我的数据:
> dput(data)
structure(list(Region = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L,
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 5L, 5L,
5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L,
5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 4L, 4L, 4L, 4L,
4L, 4L, 4L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L,
5L), .Label = c("A", "B", "C", "D", "E"), class = "factor"),
State = structure(c(2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 10L, 10L, 10L,
10L, 10L, 10L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 10L, 10L,
10L, 10L, 10L, 10L, 10L, 10L, 7L, 7L, 7L, 7L, 7L, 7L, 7L,
7L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 5L, 5L, 5L, 5L, 5L,
5L, 5L, 5L, 5L, 5L, 5L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L,
4L, 4L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 6L, 6L,
6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 8L,
8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 12L, 12L, 12L, 12L, 12L,
12L, 12L, 12L, 12L, 12L), .Label = c("DE", "FL", "GA", "MA",
"MD", "ME", "NC", "NH", "NY", "SC", "VA", "VT"), class = "factor"),
Location = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L,
4L, 4L, 4L, 4L, 4L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 12L,
12L, 12L, 12L, 12L, 12L, 12L, 12L, 14L, 14L, 14L, 14L, 14L,
14L, 14L, 14L, 18L, 18L, 18L, 18L, 18L, 18L, 18L, 18L, 18L,
16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 17L, 17L,
17L, 17L, 17L, 17L, 17L, 20L, 20L, 20L, 20L, 20L, 20L, 22L,
22L, 22L, 22L, 22L, 22L, 22L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 15L, 15L, 15L, 15L, 15L,
15L, 15L, 15L, 15L, 15L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L,
8L, 8L, 8L, 8L, 8L, 8L, 10L, 10L, 10L, 10L, 10L, 10L, 10L,
10L, 10L, 10L, 10L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L,
11L, 11L, 11L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L,
13L, 13L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L,
6L, 6L, 6L, 6L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L,
5L, 5L, 5L, 19L, 19L, 19L, 19L, 19L, 19L, 19L, 19L, 19L,
19L, 19L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L
), .Label = c("APNG", "BATO", "BLEA", "CACT", "CHAG", "CHOG",
"COTR", "DTU", "HAB", "LOP", "MASV", "NEAR", "NGUP", "OYLE",
"PIRT", "PIY", "PKE", "PONO", "PPP", "ROG", "VONG", "YENQ"
), class = "factor"), Sex = structure(c(1L, 1L, 1L, 2L, 1L,
1L, 2L, 2L, 2L, 2L, 1L, 1L, 2L, 2L, 2L, 1L, 1L, 2L, 1L, 2L,
2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 1L,
1L, 2L, 2L, 1L, 1L, 2L, 2L, 2L, 1L, 1L, 1L, 2L, 2L, 2L, 1L,
1L, 1L, 2L, 1L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 2L, 1L,
2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 1L,
2L, 1L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 1L, 2L, 1L, 2L, 2L, 2L,
1L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 2L, 1L, 2L, 2L, 2L, 1L,
2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 2L, 1L, 2L,
2L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 2L, 1L, 2L, 2L, 2L,
1L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 2L, 1L,
2L, 2L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 2L, 1L, 2L, 2L,
2L, 1L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 2L,
1L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 2L, 1L, 2L,
2L, 2L, 1L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L,
2L), .Label = c("F", "M"), class = "factor"), ID = 1:216,
Sub1 = c(0.03, 0.03, 0.03, 0.04, 0.04, 0.03, 0.03, 0.03,
0.03, 0.03, 0.04, 0.03, 0.04, 0.03, 0.03, 0.03, 0.02, 0.04,
0.03, 0.03, 0.03, 0.02, 0.04, 0.04, 0.02, 0.03, 0.02, 0.03,
0.05, 0.03, 0.03, 0.03, 0.03, 0.03, 0.03, 0.03, 0.03, 0.03,
0.03, 0.03, 0.04, 0.03, 0.04, 0.06, 0.03, 0.03, 0.03, 0.03,
0.02, 0.03, 0.03, 0.03, 0.04, 0.03, 0.02, 0.02, 0.04, 0.03,
0.04, 0.03, 0.03, 0.03, 0.05, 0.03, 0.03, 0.04, 0.03, 0.02,
0.04, 0.02, 0.03, 0.02, 0.02, 0.04, 0.03, 0.02, 0.03, 0.03,
0.05, 0.04, 0.03, 0.02, 0.03, 0.05, 0.02, 0.04, 0.03, 0.05,
0.03, 0.04, 0.02, 0.03, 0.02, 0.03, 0.03, 0.03, 0.02, 0.05,
0.03, 0.03, 0.04, 0.02, 0.02, 0.04, 0.05, 0.03, 0.03, 0.02,
2.03, 2.03, 2.03, 2.04, 2.04, 2.03, 2.03, 2.03, 2.03, 2.03,
2.04, 2.03, 2.04, 2.03, 2.03, 2.03, 2.02, 2.04, 2.03, 2.03,
2.03, 2.02, 2.04, 2.04, 2.02, 2.03, 2.02, 2.03, 2.05, 2.03,
2.03, 2.03, 2.03, 2.03, 2.03, 2.03, 2.03, 2.03, 2.03, 2.03,
2.04, 2.03, 2.04, 2.06, 2.03, 2.03, 2.03, 2.03, 2.02, 2.03,
2.03, 2.03, 2.04, 2.03, 2.02, 2.02, 2.04, 2.03, 2.04, 2.03,
2.03, 2.03, 2.05, 2.03, 2.03, 2.04, 2.03, 2.02, 2.04, 2.02,
2.03, 2.02, 2.02, 2.04, 2.03, 2.02, 2.03, 2.03, 2.05, 2.04,
2.03, 2.02, 2.03, 2.05, 2.02, 2.04, 2.03, 2.05, 2.03, 2.04,
2.02, 2.03, 2.02, 2.03, 2.03, 2.03, 2.02, 2.05, 2.03, 2.03,
2.04, 2.02, 2.02, 2.04, 2.05, 2.03, 2.03, 2.02), Sub2 = c(0.69,
1.28, 1.27, 2.25, 1.05, 1.76, 1.57, 1.09, 0.68, 1.35, 0.85,
1.55, 0.12, 0, 0.58, 1.13, 0.1, 1.9, 0.54, 1.48, 0.8, 0.52,
1.76, 1.77, 1.24, 0.63, 0.63, 0.57, 0.63, 0.53, 1.32, 1.79,
1.16, 1.11, 1.1, 1.92, 1.06, 1.18, 0.43, 0.67, 0.75, 2.37,
3.93, 0.3, 2.8, 1.25, 0.9, 1.32, 0.5, 0.4, 0.72, 0.34, 0.12,
0.89, 0.69, 1.13, 1.22, 0.88, 4.13, 1.27, 0.62, 2.9, 2.42,
0.9, 0.4, 1.29, 1.61, 0.3, 1.47, 0.36, 1.27, 0.84, 1.81,
0.18, 0.47, 1.01, 0.85, 0.59, 1.73, 0.72, 0.5, 0.83, 0.9,
0.81, 0.59, 2.84, 2.24, 2.68, 1.18, 1.36, 0.84, 1.79, 1.01,
0.34, 0.41, 2.22, 0.51, 0.42, 1.26, 2.26, 1.79, 1.43, 1.3,
1.8, 2.21, 1.65, 2.39, 0.31, 2.69, 3.28, 3.27, 4.25, 3.05,
3.76, 3.57, 3.09, 2.68, 3.35, 2.85, 3.55, 2.12, 2, 2.58,
3.13, 2.1, 3.9, 2.54, 3.48, 2.8, 2.52, 3.76, 3.77, 3.24,
2.63, 2.63, 2.57, 2.63, 2.53, 3.32, 3.79, 3.16, 3.11, 3.1,
3.92, 3.06, 3.18, 2.43, 2.67, 2.75, 4.37, 5.93, 2.3, 4.8,
3.25, 2.9, 3.32, 2.5, 2.4, 2.72, 2.34, 2.12, 2.89, 2.69,
3.13, 3.22, 2.88, 6.13, 3.27, 2.62, 4.9, 4.42, 2.9, 2.4,
3.29, 3.61, 2.3, 3.47, 2.36, 3.27, 2.84, 3.81, 2.18, 2.47,
3.01, 2.85, 2.59, 3.73, 2.72, 2.5, 2.83, 2.9, 2.81, 2.59,
4.84, 4.24, 4.68, 3.18, 3.36, 2.84, 3.79, 3.01, 2.34, 2.41,
4.22, 2.51, 2.42, 3.26, 4.26, 3.79, 3.43, 3.3, 3.8, 4.21,
3.65, 4.39, 2.31), Sub3 = c(1.32, 0.19, 0.27, 0.73, 0.41,
0.37, 0.89, 1.35, 0.49, 1.32, 0.69, 0, 0.57, 0.24, 0.23,
0.71, 0, 0, 0, 0.58, 0.32, 1.1, 0.45, 0.61, 0.38, 0.3, 0.01,
0.06, 0.48, 0.62, 0.64, 1.96, 0.61, 0.43, 0.25, 0.34, 0.17,
0.57, 0.1, 0.6, 1.07, 0.44, 0.12, 0.55, 0.08, 0.56, 0.59,
0.66, 0.44, 0.58, 0.75, 0.99, 0.77, 0.57, 0.35, 0.18, 0.16,
0.31, 0.04, 0.17, 0.46, 0.19, 0.8, 0.61, 1.14, 0.3, 0.08,
0.25, 0.78, 1.07, 0.38, 0.17, 0.42, 0.48, 0.55, 0.74, 2.98,
1.96, 0.51, 0.63, 0, 0.52, 0.32, 0.23, 0.31, 0.09, 0.06,
0.26, 0.23, 0.58, 1.49, 0.46, 0.33, 0.37, 1.16, 0.91, 0.41,
0.72, 0.2, 0.84, 0.71, 0.56, 0.34, 0.68, 0.81, 0.52, 0.78,
0.19, 3.32, 2.19, 2.27, 2.73, 2.41, 2.37, 2.89, 3.35, 2.49,
3.32, 2.69, 2, 2.57, 2.24, 2.23, 2.71, 2, 2, 2, 2.58, 2.32,
3.1, 2.45, 2.61, 2.38, 2.3, 2.01, 2.06, 2.48, 2.62, 2.64,
3.96, 2.61, 2.43, 2.25, 2.34, 2.17, 2.57, 2.1, 2.6, 3.07,
2.44, 2.12, 2.55, 2.08, 2.56, 2.59, 2.66, 2.44, 2.58, 2.75,
2.99, 2.77, 2.57, 2.35, 2.18, 2.16, 2.31, 2.04, 2.17, 2.46,
2.19, 2.8, 2.61, 3.14, 2.3, 2.08, 2.25, 2.78, 3.07, 2.38,
2.17, 2.42, 2.48, 2.55, 2.74, 4.98, 3.96, 2.51, 2.63, 2,
2.52, 2.32, 2.23, 2.31, 2.09, 2.06, 2.26, 2.23, 2.58, 3.49,
2.46, 2.33, 2.37, 3.16, 2.91, 2.41, 2.72, 2.2, 2.84, 2.71,
2.56, 2.34, 2.68, 2.81, 2.52, 2.78, 2.19), Sub4 = c(0.63,
0.05, 0.2, 0.41, 0.43, 0.54, 0.26, 0.78, 0.13, 0.8, 0.47,
0.65, 0, 0.22, 0.45, 0.85, 0.47, 0, 0.62, 0.59, 0.14, 0.8,
0.9, 0.88, 0.56, 0.56, 0.47, 0.24, 0.62, 1.77, 0.56, 0.99,
0.21, 0.9, 0.62, 0.58, 0.41, 0.97, 0.2, 0.9, 0.68, 0.52,
0.14, 1.27, 0.63, 0.51, 0.12, 0.61, 0.31, 0.43, 0.62, 1.18,
0.95, 0.59, 0.39, 0.26, 0.53, 0.77, 0.4, 0.39, 0, 0.19, 0.82,
1.1, 0.46, 0.25, 0.29, 0.2, 2.01, 0.36, 0.62, 0.54, 0.48,
0.87, 0.66, 1.46, 2.59, 1.37, 1.28, 0.99, 0.71, 0.32, 0.64,
0.66, 0.47, 0.48, 0.38, 0.67, 0.18, 1.02, 0.54, 0.53, 0.25,
0.43, 1.02, 0.58, 0.58, 0.48, 0.2, 0.7, 0.38, 0.28, 0.65,
1.21, 1.03, 0.38, 0.6, 0.44, 2.63, 2.05, 2.2, 2.41, 2.43,
2.54, 2.26, 2.78, 2.13, 2.8, 2.47, 2.65, 2, 2.22, 2.45, 2.85,
2.47, 2, 2.62, 2.59, 2.14, 2.8, 2.9, 2.88, 2.56, 2.56, 2.47,
2.24, 2.62, 3.77, 2.56, 2.99, 2.21, 2.9, 2.62, 2.58, 2.41,
2.97, 2.2, 2.9, 2.68, 2.52, 2.14, 3.27, 2.63, 2.51, 2.12,
2.61, 2.31, 2.43, 2.62, 3.18, 2.95, 2.59, 2.39, 2.26, 2.53,
2.77, 2.4, 2.39, 2, 2.19, 2.82, 3.1, 2.46, 2.25, 2.29, 2.2,
4.01, 2.36, 2.62, 2.54, 2.48, 2.87, 2.66, 3.46, 4.59, 3.37,
3.28, 2.99, 2.71, 2.32, 2.64, 2.66, 2.47, 2.48, 2.38, 2.67,
2.18, 3.02, 2.54, 2.53, 2.25, 2.43, 3.02, 2.58, 2.58, 2.48,
2.2, 2.7, 2.38, 2.28, 2.65, 3.21, 3.03, 2.38, 2.6, 2.44),
Sub5 = c(1.14, 1.38, 1.5, 1.43, 1.65, 1.34, 1.29, 1.72, 1.32,
1.17, 1.19, 1.35, 1.34, 1.06, 1.24, 1.33, 1.2, 1.31, 1.29,
1.37, 1.42, 1.08, 1.77, 1.32, 1.2, 1.14, 1.48, 0.98, 1.33,
1.65, 1.24, 1.43, 1.41, 1.2, 1.42, 1.09, 1.04, 1.57, 0.78,
1.37, 0.99, 1.4, 1.13, 1.34, 1.35, 1.23, 0.93, 0.94, 1.02,
1.16, 1.08, 0.96, 1.33, 1.19, 1.25, 1.44, 1.62, 1.27, 1.4,
1.4, 1.29, 1.53, 1.43, 1.33, 1.25, 1.82, 1.45, 1.36, 1.38,
1.34, 1.29, 1.86, 1.15, 1.31, 1.21, 1.23, 1.42, 1.57, 1.23,
0.99, 1.33, 1.74, 1.03, 1.33, 1.41, 1.01, 0.97, 1.46, 1.55,
1.04, 1.22, 1.19, 1.74, 1.64, 1.35, 1.34, 1.21, 1.55, 1.31,
1.5, 1.45, 1.21, 0.83, 1.17, 1.25, 1.54, 1.5, 1.11, 3.14,
3.38, 3.5, 3.43, 3.65, 3.34, 3.29, 3.72, 3.32, 3.17, 3.19,
3.35, 3.34, 3.06, 3.24, 3.33, 3.2, 3.31, 3.29, 3.37, 3.42,
3.08, 3.77, 3.32, 3.2, 3.14, 3.48, 2.98, 3.33, 3.65, 3.24,
3.43, 3.41, 3.2, 3.42, 3.09, 3.04, 3.57, 2.78, 3.37, 2.99,
3.4, 3.13, 3.34, 3.35, 3.23, 2.93, 2.94, 3.02, 3.16, 3.08,
2.96, 3.33, 3.19, 3.25, 3.44, 3.62, 3.27, 3.4, 3.4, 3.29,
3.53, 3.43, 3.33, 3.25, 3.82, 3.45, 3.36, 3.38, 3.34, 3.29,
3.86, 3.15, 3.31, 3.21, 3.23, 3.42, 3.57, 3.23, 2.99, 3.33,
3.74, 3.03, 3.33, 3.41, 3.01, 2.97, 3.46, 3.55, 3.04, 3.22,
3.19, 3.74, 3.64, 3.35, 3.34, 3.21, 3.55, 3.31, 3.5, 3.45,
3.21, 2.83, 3.17, 3.25, 3.54, 3.5, 3.11), Sub6 = c(0.2, 0.15,
0.16, 0.14, 0.19, 0.12, 0.14, 0.35, 0.29, 0.25, 0.06, 0.16,
0.18, 0.65, 0.18, 0.12, 0.42, 0.09, 0.13, 0.12, 0.22, 0.49,
0.18, 0.11, 0.29, 0.16, 0.18, 0.15, 0.46, 0.19, 0.15, 0.19,
0.1, 0.09, 0.11, 0.14, 0.1, 0.31, 0.53, 0.32, 0.23, 0.18,
0.14, 0.38, 0.19, 0.1, 0.14, 0.08, 0.21, 0.13, 0.08, 0.08,
0.26, 0.14, 0.17, 0.09, 0.09, 0.22, 0.26, 0.09, 0.3, 0.16,
0.17, 0.09, 0.12, 0.17, 0.14, 0.34, 0.12, 0.21, 0.1, 0.27,
0.11, 0.13, 0.15, 0.17, 0.21, 0.16, 0.12, 0.36, 0.16, 0.17,
0.27, 0.32, 0.15, 0.13, 0.14, 0.15, 0.1, 0.26, 0.25, 0.08,
0.25, 0.19, 0.38, 0.08, 0.64, 0.71, 0.1, 0.18, 0.12, 0.13,
0.1, 1.17, 0.14, 0.19, 0.14, 0.24, 2.2, 2.15, 2.16, 2.14,
2.19, 2.12, 2.14, 2.35, 2.29, 2.25, 2.06, 2.16, 2.18, 2.65,
2.18, 2.12, 2.42, 2.09, 2.13, 2.12, 2.22, 2.49, 2.18, 2.11,
2.29, 2.16, 2.18, 2.15, 2.46, 2.19, 2.15, 2.19, 2.1, 2.09,
2.11, 2.14, 2.1, 2.31, 2.53, 2.32, 2.23, 2.18, 2.14, 2.38,
2.19, 2.1, 2.14, 2.08, 2.21, 2.13, 2.08, 2.08, 2.26, 2.14,
2.17, 2.09, 2.09, 2.22, 2.26, 2.09, 2.3, 2.16, 2.17, 2.09,
2.12, 2.17, 2.14, 2.34, 2.12, 2.21, 2.1, 2.27, 2.11, 2.13,
2.15, 2.17, 2.21, 2.16, 2.12, 2.36, 2.16, 2.17, 2.27, 2.32,
2.15, 2.13, 2.14, 2.15, 2.1, 2.26, 2.25, 2.08, 2.25, 2.19,
2.38, 2.08, 2.64, 2.71, 2.1, 2.18, 2.12, 2.13, 2.1, 3.17,
2.14, 2.19, 2.14, 2.24), Sub7 = c(0.01, 0, 0, 0.01, 0, 0,
0.01, 0.01, 0.02, 0.03, 0.01, 0, 0.03, 0, 0.02, 0, 0, 0,
0.01, 0.03, 0.03, 0.02, 0.02, 0.02, 0.01, 0.01, 0.01, 0,
0, 0.05, 0.02, 0.04, 0.02, 0, 0.02, 0.02, 0.02, 0.04, 0.01,
0.02, 0.04, 0.02, 0.01, 0.01, 0.01, 0.01, 0.03, 0.02, 0,
0.02, 0.05, 0.14, 0, 0.01, 0, 0.01, 0.01, 0, 0.01, 0.02,
0.01, 0.02, 0.01, 0.03, 0.05, 0.06, 0.03, 0.02, 0.11, 0.05,
0.02, 0.02, 0, 0.01, 0, 0.01, 0.06, 0.04, 0.02, 0.02, 0,
0.02, 0.01, 0.02, 0.01, 0, 0.01, 0.01, 0.02, 0.01, 0.02,
0.01, 0, 0.01, 0.06, 0.01, 0.02, 0.01, 0.01, 0.03, 0.02,
0.03, 0.03, 0.02, 0.09, 0, 0.19, 0.02, 2.01, 2, 2, 2.01,
2, 2, 2.01, 2.01, 2.02, 2.03, 2.01, 2, 2.03, 2, 2.02, 2,
2, 2, 2.01, 2.03, 2.03, 2.02, 2.02, 2.02, 2.01, 2.01, 2.01,
2, 2, 2.05, 2.02, 2.04, 2.02, 2, 2.02, 2.02, 2.02, 2.04,
2.01, 2.02, 2.04, 2.02, 2.01, 2.01, 2.01, 2.01, 2.03, 2.02,
2, 2.02, 2.05, 2.14, 2, 2.01, 2, 2.01, 2.01, 2, 2.01, 2.02,
2.01, 2.02, 2.01, 2.03, 2.05, 2.06, 2.03, 2.02, 2.11, 2.05,
2.02, 2.02, 2, 2.01, 2, 2.01, 2.06, 2.04, 2.02, 2.02, 2,
2.02, 2.01, 2.02, 2.01, 2, 2.01, 2.01, 2.02, 2.01, 2.02,
2.01, 2, 2.01, 2.06, 2.01, 2.02, 2.01, 2.01, 2.03, 2.02,
2.03, 2.03, 2.02, 2.09, 2, 2.19, 2.02)), class = "data.frame", row.names = c(NA,
-216L))
我认为您的 tests
对象的问题在于它包含太多信息以弄清楚如何绘制它。
在这里,我只关注 Regions
列,但您可以将相同的工作流程应用于数据集的其他分类列。
1) 我们需要为每种物质获取与每个区域关联的标签(字母),所以回收你的loop
,我这样做了:
library(multcomp)
library(multcompView)
Labels_box = NULL
Group <- as.factor(data[,"Region"])
for(j in 6:12)
{
Response <- data[, j]
TUKEY <- TukeyHSD(aov(lm(Response ~ Group)))
MultComp <- multcompLetters(extract_p(TUKEY$Group))
Region <- names(MultComp$Letters)
Labels <- MultComp$Letters
df <- data.frame(Region, Labels)
df$Substance <- colnames(data)[j]
if(j == 1){Labels_box = df}
else{Labels_box = rbind(Labels_box,df)}
}
现在,数据集 Labels_box
应该如下所示:
head(Labels_box)
Region Labels Substance
B B a Sub1
C C b Sub1
D D b Sub1
E E b Sub1
A A a Sub1
B1 B a Sub2
2) 接下来,为了将它们添加到每个箱线图的顶部,我们必须为每个标签定义 y
位置。因此,我们将使用 dplyr
和 tidyr
:
计算每种物质在每个区域的最大值
library(tidyverse)
Max_Val <- data %>% pivot_longer(., cols = starts_with("Sub"), names_to = "Substance", values_to = "Value") %>%
group_by(Region, Substance) %>% summarise(MAX = max(Value)+0.2)
# A tibble: 6 x 3
# Groups: Region [1]
Region Substance MAX
<fct> <chr> <dbl>
1 A Sub1 0.26
2 A Sub2 4.13
3 A Sub3 1.55
4 A Sub4 2.21
5 A Sub5 2.06
6 A Sub6 0.85
我们使用 left_join
:
合并 Labels_box
和 Max_Val
数据集
Labels_box <- left_join(Labels_box, Max_Val, by = c("Region" = "Region", "Substance" = "Substance"))
Region Labels Substance MAX
1 B a Sub1 0.25
2 C b Sub1 2.25
3 D b Sub1 2.26
4 E b Sub1 2.25
5 A a Sub1 0.26
6 B a Sub2 4.33
3) 最后,我们需要以长格式重塑您 data
中每种物质的所有值,以匹配 ggplot
使用的语法。为此,我们可以重新使用 2):
中看到的 pivot_longer
函数
library(tidyverse)
data_box <- data %>% pivot_longer(., cols = starts_with("Sub"), names_to = "Substance", values_to = "Value")
# A tibble: 6 x 7
Region State Location Sex ID Substance Value
<fct> <fct> <fct> <fct> <int> <chr> <dbl>
1 A FL APNG F 1 Sub1 0.03
2 A FL APNG F 1 Sub2 0.69
3 A FL APNG F 1 Sub3 1.32
4 A FL APNG F 1 Sub4 0.63
5 A FL APNG F 1 Sub5 1.14
6 A FL APNG F 1 Sub6 0.2
我们几乎准备好了,但是为了设置一个由 Tukey 测试识别的配色组,我们需要在我们的 data_box
上添加标签。
为此,我们可以做一个 left_join
:
data_box <- left_join(data_box,Labels_box, by = c("Region" = "Region", "Substance" = "Substance"))
# A tibble: 6 x 9
Region State Location Sex ID Substance Value Labels MAX
<fct> <fct> <fct> <fct> <int> <chr> <dbl> <fct> <dbl>
1 A FL APNG F 1 Sub1 0.03 a 0.26
2 A FL APNG F 1 Sub2 0.69 a 4.13
3 A FL APNG F 1 Sub3 1.32 a 1.55
4 A FL APNG F 1 Sub4 0.63 a 2.21
5 A FL APNG F 1 Sub5 1.14 a 2.06
6 A FL APNG F 1 Sub6 0.2 a 0.85
4) 现在,我们准备绘制所有内容:
library(ggplot2)
ggplot(data_box, aes(x = Region, y = Value, fill = Labels))+
geom_boxplot()+
geom_text(data = Labels_box,aes( x = Region, y = MAX, label = Labels))+
facet_grid(.~Substance, scales = "free")
你得到这个:
看起来满意吗?
我收集了 216 个人的数据。我测量了每个人体内相同 7 种物质的浓度,用 Sub1:Sub7 表示。这些物质的浓度在不同地点的个体中可能不同。我感兴趣的是可以根据这些物质的浓度将这些人分类的精细程度。我也有兴趣了解这些物质如何相互关联,因为某些物质的浓度可能会影响其他物质的浓度。我的数据集中的每个个体都由一个唯一的 ID 号表示。三个 "nested" 分组变量(位置、州和地区)可用于区分这些个体。每个州都有多个位置,多个州是更大区域的一部分。例如,位置:APNG、BLEA 和 NEAR 中的个体都在 FL,而位置中的个体:CACT、OYLE 和 PIY 都在 GA。 FL 和 GA 都在 A 区。我用这个函数进行了方差分析:
library(tidyverse)
library(multicomp)
library(multicompView)
tests <- list()
Groups <- c(1:3)
Variables <- 6:12
for(i in Groups){
Group <- as.factor(data[[i]])
for(j in Variables)
{
test_name <- paste0(names(data)[j], "_by_", names(data[i]))
Response <- data[[j]]
sublist <- list()
sublist$aov <- aov(Response ~ Group)
sublist$tukey <- TukeyHSD(sublist$aov)
sublist$multcomp <- multcompLetters(extract_p(sublist$tukey$Group))
tests[[test_name]] <- sublist
}
}
#i can access the results like this:
lapply(tests, function(x) summary(x$aov))
#and access the compact letter display results like this:
lapply(tests, function(x) x$multcomp)
使用对象 tests
,我如何告诉 R 创建 TukeyHSD
结果的箱线图并显示 CLD 字母并将图粘贴到 pdf 上?
这个网站:r-graph-gallery.com/84-tukey-test.html 解释了如何做到这一点,但我无法让它与对象一起工作 tests
.
这是我的数据:
> dput(data)
structure(list(Region = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L,
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 5L, 5L,
5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L,
5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 4L, 4L, 4L, 4L,
4L, 4L, 4L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L,
5L), .Label = c("A", "B", "C", "D", "E"), class = "factor"),
State = structure(c(2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 10L, 10L, 10L,
10L, 10L, 10L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 10L, 10L,
10L, 10L, 10L, 10L, 10L, 10L, 7L, 7L, 7L, 7L, 7L, 7L, 7L,
7L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 5L, 5L, 5L, 5L, 5L,
5L, 5L, 5L, 5L, 5L, 5L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L,
4L, 4L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 6L, 6L,
6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 8L,
8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 12L, 12L, 12L, 12L, 12L,
12L, 12L, 12L, 12L, 12L), .Label = c("DE", "FL", "GA", "MA",
"MD", "ME", "NC", "NH", "NY", "SC", "VA", "VT"), class = "factor"),
Location = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L,
4L, 4L, 4L, 4L, 4L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 12L,
12L, 12L, 12L, 12L, 12L, 12L, 12L, 14L, 14L, 14L, 14L, 14L,
14L, 14L, 14L, 18L, 18L, 18L, 18L, 18L, 18L, 18L, 18L, 18L,
16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 17L, 17L,
17L, 17L, 17L, 17L, 17L, 20L, 20L, 20L, 20L, 20L, 20L, 22L,
22L, 22L, 22L, 22L, 22L, 22L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 15L, 15L, 15L, 15L, 15L,
15L, 15L, 15L, 15L, 15L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L,
8L, 8L, 8L, 8L, 8L, 8L, 10L, 10L, 10L, 10L, 10L, 10L, 10L,
10L, 10L, 10L, 10L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L,
11L, 11L, 11L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L,
13L, 13L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L,
6L, 6L, 6L, 6L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L,
5L, 5L, 5L, 19L, 19L, 19L, 19L, 19L, 19L, 19L, 19L, 19L,
19L, 19L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L
), .Label = c("APNG", "BATO", "BLEA", "CACT", "CHAG", "CHOG",
"COTR", "DTU", "HAB", "LOP", "MASV", "NEAR", "NGUP", "OYLE",
"PIRT", "PIY", "PKE", "PONO", "PPP", "ROG", "VONG", "YENQ"
), class = "factor"), Sex = structure(c(1L, 1L, 1L, 2L, 1L,
1L, 2L, 2L, 2L, 2L, 1L, 1L, 2L, 2L, 2L, 1L, 1L, 2L, 1L, 2L,
2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 1L,
1L, 2L, 2L, 1L, 1L, 2L, 2L, 2L, 1L, 1L, 1L, 2L, 2L, 2L, 1L,
1L, 1L, 2L, 1L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 2L, 1L,
2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 1L,
2L, 1L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 1L, 2L, 1L, 2L, 2L, 2L,
1L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 2L, 1L, 2L, 2L, 2L, 1L,
2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 2L, 1L, 2L,
2L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 2L, 1L, 2L, 2L, 2L,
1L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 2L, 1L,
2L, 2L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 2L, 1L, 2L, 2L,
2L, 1L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 2L,
1L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 2L, 1L, 2L,
2L, 2L, 1L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L,
2L), .Label = c("F", "M"), class = "factor"), ID = 1:216,
Sub1 = c(0.03, 0.03, 0.03, 0.04, 0.04, 0.03, 0.03, 0.03,
0.03, 0.03, 0.04, 0.03, 0.04, 0.03, 0.03, 0.03, 0.02, 0.04,
0.03, 0.03, 0.03, 0.02, 0.04, 0.04, 0.02, 0.03, 0.02, 0.03,
0.05, 0.03, 0.03, 0.03, 0.03, 0.03, 0.03, 0.03, 0.03, 0.03,
0.03, 0.03, 0.04, 0.03, 0.04, 0.06, 0.03, 0.03, 0.03, 0.03,
0.02, 0.03, 0.03, 0.03, 0.04, 0.03, 0.02, 0.02, 0.04, 0.03,
0.04, 0.03, 0.03, 0.03, 0.05, 0.03, 0.03, 0.04, 0.03, 0.02,
0.04, 0.02, 0.03, 0.02, 0.02, 0.04, 0.03, 0.02, 0.03, 0.03,
0.05, 0.04, 0.03, 0.02, 0.03, 0.05, 0.02, 0.04, 0.03, 0.05,
0.03, 0.04, 0.02, 0.03, 0.02, 0.03, 0.03, 0.03, 0.02, 0.05,
0.03, 0.03, 0.04, 0.02, 0.02, 0.04, 0.05, 0.03, 0.03, 0.02,
2.03, 2.03, 2.03, 2.04, 2.04, 2.03, 2.03, 2.03, 2.03, 2.03,
2.04, 2.03, 2.04, 2.03, 2.03, 2.03, 2.02, 2.04, 2.03, 2.03,
2.03, 2.02, 2.04, 2.04, 2.02, 2.03, 2.02, 2.03, 2.05, 2.03,
2.03, 2.03, 2.03, 2.03, 2.03, 2.03, 2.03, 2.03, 2.03, 2.03,
2.04, 2.03, 2.04, 2.06, 2.03, 2.03, 2.03, 2.03, 2.02, 2.03,
2.03, 2.03, 2.04, 2.03, 2.02, 2.02, 2.04, 2.03, 2.04, 2.03,
2.03, 2.03, 2.05, 2.03, 2.03, 2.04, 2.03, 2.02, 2.04, 2.02,
2.03, 2.02, 2.02, 2.04, 2.03, 2.02, 2.03, 2.03, 2.05, 2.04,
2.03, 2.02, 2.03, 2.05, 2.02, 2.04, 2.03, 2.05, 2.03, 2.04,
2.02, 2.03, 2.02, 2.03, 2.03, 2.03, 2.02, 2.05, 2.03, 2.03,
2.04, 2.02, 2.02, 2.04, 2.05, 2.03, 2.03, 2.02), Sub2 = c(0.69,
1.28, 1.27, 2.25, 1.05, 1.76, 1.57, 1.09, 0.68, 1.35, 0.85,
1.55, 0.12, 0, 0.58, 1.13, 0.1, 1.9, 0.54, 1.48, 0.8, 0.52,
1.76, 1.77, 1.24, 0.63, 0.63, 0.57, 0.63, 0.53, 1.32, 1.79,
1.16, 1.11, 1.1, 1.92, 1.06, 1.18, 0.43, 0.67, 0.75, 2.37,
3.93, 0.3, 2.8, 1.25, 0.9, 1.32, 0.5, 0.4, 0.72, 0.34, 0.12,
0.89, 0.69, 1.13, 1.22, 0.88, 4.13, 1.27, 0.62, 2.9, 2.42,
0.9, 0.4, 1.29, 1.61, 0.3, 1.47, 0.36, 1.27, 0.84, 1.81,
0.18, 0.47, 1.01, 0.85, 0.59, 1.73, 0.72, 0.5, 0.83, 0.9,
0.81, 0.59, 2.84, 2.24, 2.68, 1.18, 1.36, 0.84, 1.79, 1.01,
0.34, 0.41, 2.22, 0.51, 0.42, 1.26, 2.26, 1.79, 1.43, 1.3,
1.8, 2.21, 1.65, 2.39, 0.31, 2.69, 3.28, 3.27, 4.25, 3.05,
3.76, 3.57, 3.09, 2.68, 3.35, 2.85, 3.55, 2.12, 2, 2.58,
3.13, 2.1, 3.9, 2.54, 3.48, 2.8, 2.52, 3.76, 3.77, 3.24,
2.63, 2.63, 2.57, 2.63, 2.53, 3.32, 3.79, 3.16, 3.11, 3.1,
3.92, 3.06, 3.18, 2.43, 2.67, 2.75, 4.37, 5.93, 2.3, 4.8,
3.25, 2.9, 3.32, 2.5, 2.4, 2.72, 2.34, 2.12, 2.89, 2.69,
3.13, 3.22, 2.88, 6.13, 3.27, 2.62, 4.9, 4.42, 2.9, 2.4,
3.29, 3.61, 2.3, 3.47, 2.36, 3.27, 2.84, 3.81, 2.18, 2.47,
3.01, 2.85, 2.59, 3.73, 2.72, 2.5, 2.83, 2.9, 2.81, 2.59,
4.84, 4.24, 4.68, 3.18, 3.36, 2.84, 3.79, 3.01, 2.34, 2.41,
4.22, 2.51, 2.42, 3.26, 4.26, 3.79, 3.43, 3.3, 3.8, 4.21,
3.65, 4.39, 2.31), Sub3 = c(1.32, 0.19, 0.27, 0.73, 0.41,
0.37, 0.89, 1.35, 0.49, 1.32, 0.69, 0, 0.57, 0.24, 0.23,
0.71, 0, 0, 0, 0.58, 0.32, 1.1, 0.45, 0.61, 0.38, 0.3, 0.01,
0.06, 0.48, 0.62, 0.64, 1.96, 0.61, 0.43, 0.25, 0.34, 0.17,
0.57, 0.1, 0.6, 1.07, 0.44, 0.12, 0.55, 0.08, 0.56, 0.59,
0.66, 0.44, 0.58, 0.75, 0.99, 0.77, 0.57, 0.35, 0.18, 0.16,
0.31, 0.04, 0.17, 0.46, 0.19, 0.8, 0.61, 1.14, 0.3, 0.08,
0.25, 0.78, 1.07, 0.38, 0.17, 0.42, 0.48, 0.55, 0.74, 2.98,
1.96, 0.51, 0.63, 0, 0.52, 0.32, 0.23, 0.31, 0.09, 0.06,
0.26, 0.23, 0.58, 1.49, 0.46, 0.33, 0.37, 1.16, 0.91, 0.41,
0.72, 0.2, 0.84, 0.71, 0.56, 0.34, 0.68, 0.81, 0.52, 0.78,
0.19, 3.32, 2.19, 2.27, 2.73, 2.41, 2.37, 2.89, 3.35, 2.49,
3.32, 2.69, 2, 2.57, 2.24, 2.23, 2.71, 2, 2, 2, 2.58, 2.32,
3.1, 2.45, 2.61, 2.38, 2.3, 2.01, 2.06, 2.48, 2.62, 2.64,
3.96, 2.61, 2.43, 2.25, 2.34, 2.17, 2.57, 2.1, 2.6, 3.07,
2.44, 2.12, 2.55, 2.08, 2.56, 2.59, 2.66, 2.44, 2.58, 2.75,
2.99, 2.77, 2.57, 2.35, 2.18, 2.16, 2.31, 2.04, 2.17, 2.46,
2.19, 2.8, 2.61, 3.14, 2.3, 2.08, 2.25, 2.78, 3.07, 2.38,
2.17, 2.42, 2.48, 2.55, 2.74, 4.98, 3.96, 2.51, 2.63, 2,
2.52, 2.32, 2.23, 2.31, 2.09, 2.06, 2.26, 2.23, 2.58, 3.49,
2.46, 2.33, 2.37, 3.16, 2.91, 2.41, 2.72, 2.2, 2.84, 2.71,
2.56, 2.34, 2.68, 2.81, 2.52, 2.78, 2.19), Sub4 = c(0.63,
0.05, 0.2, 0.41, 0.43, 0.54, 0.26, 0.78, 0.13, 0.8, 0.47,
0.65, 0, 0.22, 0.45, 0.85, 0.47, 0, 0.62, 0.59, 0.14, 0.8,
0.9, 0.88, 0.56, 0.56, 0.47, 0.24, 0.62, 1.77, 0.56, 0.99,
0.21, 0.9, 0.62, 0.58, 0.41, 0.97, 0.2, 0.9, 0.68, 0.52,
0.14, 1.27, 0.63, 0.51, 0.12, 0.61, 0.31, 0.43, 0.62, 1.18,
0.95, 0.59, 0.39, 0.26, 0.53, 0.77, 0.4, 0.39, 0, 0.19, 0.82,
1.1, 0.46, 0.25, 0.29, 0.2, 2.01, 0.36, 0.62, 0.54, 0.48,
0.87, 0.66, 1.46, 2.59, 1.37, 1.28, 0.99, 0.71, 0.32, 0.64,
0.66, 0.47, 0.48, 0.38, 0.67, 0.18, 1.02, 0.54, 0.53, 0.25,
0.43, 1.02, 0.58, 0.58, 0.48, 0.2, 0.7, 0.38, 0.28, 0.65,
1.21, 1.03, 0.38, 0.6, 0.44, 2.63, 2.05, 2.2, 2.41, 2.43,
2.54, 2.26, 2.78, 2.13, 2.8, 2.47, 2.65, 2, 2.22, 2.45, 2.85,
2.47, 2, 2.62, 2.59, 2.14, 2.8, 2.9, 2.88, 2.56, 2.56, 2.47,
2.24, 2.62, 3.77, 2.56, 2.99, 2.21, 2.9, 2.62, 2.58, 2.41,
2.97, 2.2, 2.9, 2.68, 2.52, 2.14, 3.27, 2.63, 2.51, 2.12,
2.61, 2.31, 2.43, 2.62, 3.18, 2.95, 2.59, 2.39, 2.26, 2.53,
2.77, 2.4, 2.39, 2, 2.19, 2.82, 3.1, 2.46, 2.25, 2.29, 2.2,
4.01, 2.36, 2.62, 2.54, 2.48, 2.87, 2.66, 3.46, 4.59, 3.37,
3.28, 2.99, 2.71, 2.32, 2.64, 2.66, 2.47, 2.48, 2.38, 2.67,
2.18, 3.02, 2.54, 2.53, 2.25, 2.43, 3.02, 2.58, 2.58, 2.48,
2.2, 2.7, 2.38, 2.28, 2.65, 3.21, 3.03, 2.38, 2.6, 2.44),
Sub5 = c(1.14, 1.38, 1.5, 1.43, 1.65, 1.34, 1.29, 1.72, 1.32,
1.17, 1.19, 1.35, 1.34, 1.06, 1.24, 1.33, 1.2, 1.31, 1.29,
1.37, 1.42, 1.08, 1.77, 1.32, 1.2, 1.14, 1.48, 0.98, 1.33,
1.65, 1.24, 1.43, 1.41, 1.2, 1.42, 1.09, 1.04, 1.57, 0.78,
1.37, 0.99, 1.4, 1.13, 1.34, 1.35, 1.23, 0.93, 0.94, 1.02,
1.16, 1.08, 0.96, 1.33, 1.19, 1.25, 1.44, 1.62, 1.27, 1.4,
1.4, 1.29, 1.53, 1.43, 1.33, 1.25, 1.82, 1.45, 1.36, 1.38,
1.34, 1.29, 1.86, 1.15, 1.31, 1.21, 1.23, 1.42, 1.57, 1.23,
0.99, 1.33, 1.74, 1.03, 1.33, 1.41, 1.01, 0.97, 1.46, 1.55,
1.04, 1.22, 1.19, 1.74, 1.64, 1.35, 1.34, 1.21, 1.55, 1.31,
1.5, 1.45, 1.21, 0.83, 1.17, 1.25, 1.54, 1.5, 1.11, 3.14,
3.38, 3.5, 3.43, 3.65, 3.34, 3.29, 3.72, 3.32, 3.17, 3.19,
3.35, 3.34, 3.06, 3.24, 3.33, 3.2, 3.31, 3.29, 3.37, 3.42,
3.08, 3.77, 3.32, 3.2, 3.14, 3.48, 2.98, 3.33, 3.65, 3.24,
3.43, 3.41, 3.2, 3.42, 3.09, 3.04, 3.57, 2.78, 3.37, 2.99,
3.4, 3.13, 3.34, 3.35, 3.23, 2.93, 2.94, 3.02, 3.16, 3.08,
2.96, 3.33, 3.19, 3.25, 3.44, 3.62, 3.27, 3.4, 3.4, 3.29,
3.53, 3.43, 3.33, 3.25, 3.82, 3.45, 3.36, 3.38, 3.34, 3.29,
3.86, 3.15, 3.31, 3.21, 3.23, 3.42, 3.57, 3.23, 2.99, 3.33,
3.74, 3.03, 3.33, 3.41, 3.01, 2.97, 3.46, 3.55, 3.04, 3.22,
3.19, 3.74, 3.64, 3.35, 3.34, 3.21, 3.55, 3.31, 3.5, 3.45,
3.21, 2.83, 3.17, 3.25, 3.54, 3.5, 3.11), Sub6 = c(0.2, 0.15,
0.16, 0.14, 0.19, 0.12, 0.14, 0.35, 0.29, 0.25, 0.06, 0.16,
0.18, 0.65, 0.18, 0.12, 0.42, 0.09, 0.13, 0.12, 0.22, 0.49,
0.18, 0.11, 0.29, 0.16, 0.18, 0.15, 0.46, 0.19, 0.15, 0.19,
0.1, 0.09, 0.11, 0.14, 0.1, 0.31, 0.53, 0.32, 0.23, 0.18,
0.14, 0.38, 0.19, 0.1, 0.14, 0.08, 0.21, 0.13, 0.08, 0.08,
0.26, 0.14, 0.17, 0.09, 0.09, 0.22, 0.26, 0.09, 0.3, 0.16,
0.17, 0.09, 0.12, 0.17, 0.14, 0.34, 0.12, 0.21, 0.1, 0.27,
0.11, 0.13, 0.15, 0.17, 0.21, 0.16, 0.12, 0.36, 0.16, 0.17,
0.27, 0.32, 0.15, 0.13, 0.14, 0.15, 0.1, 0.26, 0.25, 0.08,
0.25, 0.19, 0.38, 0.08, 0.64, 0.71, 0.1, 0.18, 0.12, 0.13,
0.1, 1.17, 0.14, 0.19, 0.14, 0.24, 2.2, 2.15, 2.16, 2.14,
2.19, 2.12, 2.14, 2.35, 2.29, 2.25, 2.06, 2.16, 2.18, 2.65,
2.18, 2.12, 2.42, 2.09, 2.13, 2.12, 2.22, 2.49, 2.18, 2.11,
2.29, 2.16, 2.18, 2.15, 2.46, 2.19, 2.15, 2.19, 2.1, 2.09,
2.11, 2.14, 2.1, 2.31, 2.53, 2.32, 2.23, 2.18, 2.14, 2.38,
2.19, 2.1, 2.14, 2.08, 2.21, 2.13, 2.08, 2.08, 2.26, 2.14,
2.17, 2.09, 2.09, 2.22, 2.26, 2.09, 2.3, 2.16, 2.17, 2.09,
2.12, 2.17, 2.14, 2.34, 2.12, 2.21, 2.1, 2.27, 2.11, 2.13,
2.15, 2.17, 2.21, 2.16, 2.12, 2.36, 2.16, 2.17, 2.27, 2.32,
2.15, 2.13, 2.14, 2.15, 2.1, 2.26, 2.25, 2.08, 2.25, 2.19,
2.38, 2.08, 2.64, 2.71, 2.1, 2.18, 2.12, 2.13, 2.1, 3.17,
2.14, 2.19, 2.14, 2.24), Sub7 = c(0.01, 0, 0, 0.01, 0, 0,
0.01, 0.01, 0.02, 0.03, 0.01, 0, 0.03, 0, 0.02, 0, 0, 0,
0.01, 0.03, 0.03, 0.02, 0.02, 0.02, 0.01, 0.01, 0.01, 0,
0, 0.05, 0.02, 0.04, 0.02, 0, 0.02, 0.02, 0.02, 0.04, 0.01,
0.02, 0.04, 0.02, 0.01, 0.01, 0.01, 0.01, 0.03, 0.02, 0,
0.02, 0.05, 0.14, 0, 0.01, 0, 0.01, 0.01, 0, 0.01, 0.02,
0.01, 0.02, 0.01, 0.03, 0.05, 0.06, 0.03, 0.02, 0.11, 0.05,
0.02, 0.02, 0, 0.01, 0, 0.01, 0.06, 0.04, 0.02, 0.02, 0,
0.02, 0.01, 0.02, 0.01, 0, 0.01, 0.01, 0.02, 0.01, 0.02,
0.01, 0, 0.01, 0.06, 0.01, 0.02, 0.01, 0.01, 0.03, 0.02,
0.03, 0.03, 0.02, 0.09, 0, 0.19, 0.02, 2.01, 2, 2, 2.01,
2, 2, 2.01, 2.01, 2.02, 2.03, 2.01, 2, 2.03, 2, 2.02, 2,
2, 2, 2.01, 2.03, 2.03, 2.02, 2.02, 2.02, 2.01, 2.01, 2.01,
2, 2, 2.05, 2.02, 2.04, 2.02, 2, 2.02, 2.02, 2.02, 2.04,
2.01, 2.02, 2.04, 2.02, 2.01, 2.01, 2.01, 2.01, 2.03, 2.02,
2, 2.02, 2.05, 2.14, 2, 2.01, 2, 2.01, 2.01, 2, 2.01, 2.02,
2.01, 2.02, 2.01, 2.03, 2.05, 2.06, 2.03, 2.02, 2.11, 2.05,
2.02, 2.02, 2, 2.01, 2, 2.01, 2.06, 2.04, 2.02, 2.02, 2,
2.02, 2.01, 2.02, 2.01, 2, 2.01, 2.01, 2.02, 2.01, 2.02,
2.01, 2, 2.01, 2.06, 2.01, 2.02, 2.01, 2.01, 2.03, 2.02,
2.03, 2.03, 2.02, 2.09, 2, 2.19, 2.02)), class = "data.frame", row.names = c(NA,
-216L))
我认为您的 tests
对象的问题在于它包含太多信息以弄清楚如何绘制它。
在这里,我只关注 Regions
列,但您可以将相同的工作流程应用于数据集的其他分类列。
1) 我们需要为每种物质获取与每个区域关联的标签(字母),所以回收你的loop
,我这样做了:
library(multcomp)
library(multcompView)
Labels_box = NULL
Group <- as.factor(data[,"Region"])
for(j in 6:12)
{
Response <- data[, j]
TUKEY <- TukeyHSD(aov(lm(Response ~ Group)))
MultComp <- multcompLetters(extract_p(TUKEY$Group))
Region <- names(MultComp$Letters)
Labels <- MultComp$Letters
df <- data.frame(Region, Labels)
df$Substance <- colnames(data)[j]
if(j == 1){Labels_box = df}
else{Labels_box = rbind(Labels_box,df)}
}
现在,数据集 Labels_box
应该如下所示:
head(Labels_box)
Region Labels Substance
B B a Sub1
C C b Sub1
D D b Sub1
E E b Sub1
A A a Sub1
B1 B a Sub2
2) 接下来,为了将它们添加到每个箱线图的顶部,我们必须为每个标签定义 y
位置。因此,我们将使用 dplyr
和 tidyr
:
library(tidyverse)
Max_Val <- data %>% pivot_longer(., cols = starts_with("Sub"), names_to = "Substance", values_to = "Value") %>%
group_by(Region, Substance) %>% summarise(MAX = max(Value)+0.2)
# A tibble: 6 x 3
# Groups: Region [1]
Region Substance MAX
<fct> <chr> <dbl>
1 A Sub1 0.26
2 A Sub2 4.13
3 A Sub3 1.55
4 A Sub4 2.21
5 A Sub5 2.06
6 A Sub6 0.85
我们使用 left_join
:
Labels_box
和 Max_Val
数据集
Labels_box <- left_join(Labels_box, Max_Val, by = c("Region" = "Region", "Substance" = "Substance"))
Region Labels Substance MAX
1 B a Sub1 0.25
2 C b Sub1 2.25
3 D b Sub1 2.26
4 E b Sub1 2.25
5 A a Sub1 0.26
6 B a Sub2 4.33
3) 最后,我们需要以长格式重塑您 data
中每种物质的所有值,以匹配 ggplot
使用的语法。为此,我们可以重新使用 2):
pivot_longer
函数
library(tidyverse)
data_box <- data %>% pivot_longer(., cols = starts_with("Sub"), names_to = "Substance", values_to = "Value")
# A tibble: 6 x 7
Region State Location Sex ID Substance Value
<fct> <fct> <fct> <fct> <int> <chr> <dbl>
1 A FL APNG F 1 Sub1 0.03
2 A FL APNG F 1 Sub2 0.69
3 A FL APNG F 1 Sub3 1.32
4 A FL APNG F 1 Sub4 0.63
5 A FL APNG F 1 Sub5 1.14
6 A FL APNG F 1 Sub6 0.2
我们几乎准备好了,但是为了设置一个由 Tukey 测试识别的配色组,我们需要在我们的 data_box
上添加标签。
为此,我们可以做一个 left_join
:
data_box <- left_join(data_box,Labels_box, by = c("Region" = "Region", "Substance" = "Substance"))
# A tibble: 6 x 9
Region State Location Sex ID Substance Value Labels MAX
<fct> <fct> <fct> <fct> <int> <chr> <dbl> <fct> <dbl>
1 A FL APNG F 1 Sub1 0.03 a 0.26
2 A FL APNG F 1 Sub2 0.69 a 4.13
3 A FL APNG F 1 Sub3 1.32 a 1.55
4 A FL APNG F 1 Sub4 0.63 a 2.21
5 A FL APNG F 1 Sub5 1.14 a 2.06
6 A FL APNG F 1 Sub6 0.2 a 0.85
4) 现在,我们准备绘制所有内容:
library(ggplot2)
ggplot(data_box, aes(x = Region, y = Value, fill = Labels))+
geom_boxplot()+
geom_text(data = Labels_box,aes( x = Region, y = MAX, label = Labels))+
facet_grid(.~Substance, scales = "free")
你得到这个:
看起来满意吗?