fish_data <- data.frame(
ID = c("F01", "F02", "F03", "F04", "F05", "F06"),
Control = c(47, 65, 44, 37, 62, 51),
Treat1 = c(68, 55, 62, 70, 59, 63),
Treat2 = c(78, 76, 72, 81, 76, 83),
Treat3 = c(85, 65, 81, 98, 92, 89)
)
print(fish_data)
## ID Control Treat1 Treat2 Treat3
## 1 F01 47 68 78 85
## 2 F02 65 55 76 65
## 3 F03 44 62 72 81
## 4 F04 37 70 81 98
## 5 F05 62 59 76 92
## 6 F06 51 63 83 89
library(ggplot2)
fish_data_long <- reshape2::melt(fish_data, id.vars = "ID", variable.name = "Group", value.name = "Length")
可视化
#箱线图
# 将数据转换为长格式,方便ggplot2绘图
fish_data_long <- reshape2::melt(fish_data, id.vars = "ID", variable.name = "Group", value.name = "Length")
# 绘制箱线图
ggplot(fish_data_long , aes(x = Group, y = Length)) +geom_boxplot() +labs(title = "鱼类体长比较", x = "饲料添加剂组别", y = "体长(mm)") +theme_minimal()
ggplot(fish_data_long, aes(x = Group, y = Length, fill = Group)) +
geom_violin(trim = FALSE) +
geom_point(position = position_jitterdodge(seed = 123)) +
labs(title = "鱼类体长比较 - 小提琴图", x = "饲料添加剂组别", y = "体长(mm)") +
theme_minimal()
ggplot(fish_data_long, aes(x = reorder(Group, -Length, FUN = mean), y = Length, fill = Group)) +
geom_point(position = position_dodge(width = 0.6), size = 3) +
geom_bar(stat = "summary", fun = mean, position = position_dodge(width = 0.6), fill = "black", alpha = 0.2) +
labs(title = "鱼类体长比较 - 点图", x = "饲料添加剂组别", y = "体长(mm)") +
theme_minimal()
anova_result <- aov(Length ~ Group, data = fish_data_long)
summary(anova_result)
## Df Sum Sq Mean Sq F value Pr(>F)
## Group 3 4158 1386 18.99 4.54e-06 ***
## Residuals 20 1460 73
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
#这个 ANOVA 表的结果表明,在比较的3个组别之间存在显著差异(P<0.001),且这种差异在统计学上是显著的。
tukey_result <- TukeyHSD(anova_result)
plot(tukey_result)
print(tukey_result)
## Tukey multiple comparisons of means
## 95% family-wise confidence level
##
## Fit: aov(formula = Length ~ Group, data = fish_data_long)
##
## $Group
## diff lwr upr p adj
## Treat1-Control 11.833333 -1.974279 25.64095 0.1096593
## Treat2-Control 26.666667 12.859054 40.47428 0.0001488
## Treat3-Control 34.000000 20.192388 47.80761 0.0000060
## Treat2-Treat1 14.833333 1.025721 28.64095 0.0324588
## Treat3-Treat1 22.166667 8.359054 35.97428 0.0011738
## Treat3-Treat2 7.333333 -6.474279 21.14095 0.4636215
#t检验
#根据显喜性水平,判断各组的鱼类体长是否有显著性差异
alpha <- 0.5
value <- t.test(fish_data$Treat1,fish_data$Control)
value
##
## Welch Two Sample t-test
##
## data: fish_data$Treat1 and fish_data$Control
## t = 2.3942, df = 7.4994, p-value = 0.04557
## alternative hypothesis: true difference in means is not equal to 0
## 95 percent confidence interval:
## 0.3022434 23.3644233
## sample estimates:
## mean of x mean of y
## 62.83333 51.00000
if(value$p.value<alpha){
cat("Treat1与Control有显著差异\n")
}else{
cat("Treat1与Control无显著差异\n")
}
## Treat1与Control有显著差异
value <- t.test(fish_data$Treat2,fish_data$Control)
value
##
## Welch Two Sample t-test
##
## data: fish_data$Treat2 and fish_data$Control
## t = 5.7056, df = 6.3144, p-value = 0.001051
## alternative hypothesis: true difference in means is not equal to 0
## 95 percent confidence interval:
## 15.36691 37.96642
## sample estimates:
## mean of x mean of y
## 77.66667 51.00000
if(value$p.value<alpha){
cat("Treat2与Control有显著差异\n")
}else{
cat("Treat2与Control无显著差异\n")}
## Treat2与Control有显著差异
value <- t.test(fish_data$Treat3,fish_data$Control)
value
##
## Welch Two Sample t-test
##
## data: fish_data$Treat3 and fish_data$Control
## t = 5.3142, df = 9.9657, p-value = 0.0003445
## alternative hypothesis: true difference in means is not equal to 0
## 95 percent confidence interval:
## 19.73791 48.26209
## sample estimates:
## mean of x mean of y
## 85 51
if(value$p.value<alpha){
cat("Treat3与Control有显著差异\n")
}else{
cat("Treat3与Control无显著差异\n")
}
## Treat3与Control有显著差异