fish_ID | 颜色 | 安装 | 时间 |
---|---|---|---|
1 | 蓝色 | inst_0 | 136 |
1 | 绿色 | inst_0 | 40 |
1 | 红色 | inst_0 | 111 |
1 | 黄色 | inst_0 | 20 |
1 | 蓝色 | inst_3 | 112 |
1 | 绿色 | inst_3 | 13 |
1 | 红色 | inst_3 | 15 |
1 | 黄色 | inst_3 | 90 |
2 | 蓝色 | inst_0 | 110 |
2 | 绿色 | inst_0 | 80 |
2 | 红色 | inst_0 | 11 |
2 | 黄色 | inst_0 | 34 |
2 | 蓝色 | inst_3 | 19 |
2 | 绿色 | inst_3 | 130 |
2 | 红色 | inst_3 | 15 |
2 | 黄色 | inst_3 | 88 |
我这里以一部分数据为例。我有很多这样的鱼 ID 号。我想做一个配对 t 检验,比较每个颜色集在 inst_0 和 inst_3 之间花费的平均时间。我能够用这段代码很好地比较它们:
library(ggplot2)
library(ggprism)
ggplot(mydata , aes(x = factor(inst), y = time, fill = color)) +
geom_boxplot(position = position_dodge(width = 0.75)) +
scale_fill_manual(values = c("blue" = "blue", "green" = "green", "red" = "red", "yellow" =
"yellow")) +
theme_prism()
我还想从图中的配对 t 检验结果中添加 p 值,但我不确定如何对每个颜色集应用成对比较(例如 inst_0 和 inst_3 之间的蓝色比较)。我会很感激你的建议。谢谢!
请注意,您可以在使用
rstatix
后对齐 p 值的位置。
df <- data.frame(
stringsAsFactors = FALSE,
fish_ID = c(1L,1L,1L,1L,1L,1L,1L,1L,
2L,2L,2L,2L,2L,2L,2L,2L),
Color = c("blue","green","red",
"yellow","blue","green","red","yellow","blue","green",
"red","yellow","blue","green","red","yellow"),
inst = c("inst_0","inst_0","inst_0",
"inst_0","inst_3","inst_3","inst_3","inst_3","inst_0",
"inst_0","inst_0","inst_0","inst_3","inst_3",
"inst_3","inst_3"),
time = c(136L,40L,111L,20L,112L,13L,
15L,90L,110L,80L,11L,34L,19L,130L,15L,88L)
)
library(rstatix)
library(ggplot2)
library(ggprism)
df_p_val <- df |>
group_by(Color) |>
t_test(time ~ inst) |>
add_xy_position()
# align positions of p-values
df_p_val <- df_p_val |>
mutate(group1 = c(0.7,0.9, 1.1, 1.3),
group2 = c(1.7, 1.9, 2.1,2.3))
colors <- unique(df$Color) # to manually select fill, color scales
ggplot(df, aes(x=factor(inst), y=time)) +
geom_boxplot(aes(fill=Color)) +
theme_prism() +
add_pvalue(df_p_val, fontface="bold",
colour = "Color",
show.legend=F) +
scale_fill_manual(values =colors) +
scale_color_manual(values=colors)
创建于 2023-05-05 与 reprex v2.0.2