如何按组对dplyr中的mu
-参数=“大于”进行t检验?
这是我的数据框
structure(list(Lattobacilli = c(6.39794000867204, 4.91381385238372,
7.7160033436348, 7.91907809237607, 6.6232492903979, 7.63346845557959,
7.27875360095283, 7.43136376415899, 5.54406804435028, 6.36172783601759,
4.55630250076729, 8.38021124171161, 7.94939000664491, 7.04139268515823
), Starter = structure(c(3L, 3L, 6L, 6L, 7L, 7L, 8L, 8L, 4L,
4L, 4L, 5L, 5L, 5L), .Label = c("B", "C", "Ch1", "Ds1", "Ds2",
"Sa1", "Sa2", "Sa3"), class = "factor"), Giorni = structure(c(4L,
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L), .Label = c("0",
"1", "7", "35"), class = "factor")), class = c("grouped_df",
"tbl_df", "tbl", "data.frame"), row.names = c(NA, -14L), .Names = c("Lattobacilli",
"Starter", "Giorni"), vars = "Starter", drop = TRUE, indices = list(
0:1, 8:10, 11:13, 2:3, 4:5, 6:7), group_sizes = c(2L, 3L,
3L, 2L, 2L, 2L), biggest_group_size = 3L, labels = structure(list(
Starter = structure(3:8, .Label = c("B", "C", "Ch1", "Ds1",
"Ds2", "Sa1", "Sa2", "Sa3"), class = "factor")), class = "data.frame", row.names = c(NA,
-6L), vars = "Starter", drop = TRUE, .Names = "Starter"))
我找到了这个解决方案
lattotest<-df%>%
select(Lattobacilli,Starter,Giorni)%>%
filter(Giorni=="35",!is.na(Lattobacilli))%>%
group_by(Starter)%>%
mutate(p_value=t.test(Lattobacilli, mu = 6,alternative="greater")$p.value, t_value=t.test(Lattobacilli, mu = 6,alternative="greater")$statistic)
> lattotest
# A tibble: 14 x 5
# Groups: Starter [6]
Lattobacilli Starter Giorni p_value t_value
<dbl> <fct> <fct> <dbl> <dbl>
1 6.40 Ch1 35 0.638 -0.464
2 4.91 Ch1 35 0.638 -0.464
3 7.72 Sa1 35 0.0178 17.9
4 7.92 Sa1 35 0.0178 17.9
5 6.62 Sa2 35 0.134 2.23
6 7.63 Sa2 35 0.134 2.23
7 7.28 Sa3 35 0.0179 17.8
8 7.43 Sa3 35 0.0179 17.8
9 5.54 Ds1 35 0.785 -0.982
10 6.36 Ds1 35 0.785 -0.982
11 4.56 Ds1 35 0.785 -0.982
12 8.38 Ds2 35 0.0226 4.54
13 7.95 Ds2 35 0.0226 4.54
14 7.04 Ds2 35 0.0226 4.54
比我想使用stat_pvalue_manual
将结果添加到ggbarplot中的方法相同,但是,不可能仅添加p值,但是我还需要插入我没有的括号,因为我比较了均值而不是组之间的固定值
答案 0 :(得分:1)
一种选择是仅将ggplot
与geom_text
一起使用。
首先,您需要创建一个新的数据框,其中包含每个“ x”的p值
library(dplyr)
library(ggplot2)
# I used your data frame "lattotest"
latto_p <- lattotest %>% group_by(Starter) %>% summarise(p = mean(p_value))
ggplot() +
geom_bar(data = lattotest, aes(Starter, Lattobacilli), stat = 'identity') +
geom_text(data = latto_p, aes(label = round(p,3), x = Starter, y = 1))
您可以通过在数据框中添加特定的y值来进行操作,还可以通过在标签参数
中使用paste
添加所需的任何文本。