[英]R: t.test in a facet_grid (ggplot)
這是一個非常具體的問題,但我已經擁有並使用了這個詳細且運行良好的代碼,所以我希望找到調整它所需的微小更改,並使其適用於下一個復雜級別。 我得到了什么:
library(ggplot2)
library(ggpubr)
head(ToothGrowth)
ToothGrowth$dose <- as.factor(ToothGrowth$dose)
# add a grouping ID for measured individuals:
ToothGrowth$ID <- rep(c(1:30),2)
# The code I am using now (basically a solution I got from my former question answered by Allan Cameron (user:12500315)):
ggplot(ToothGrowth, aes(supp, len, fill = dose, alpha = supp)) +
geom_boxplot() +
scale_fill_manual(name = "Dosis",
labels = c("0.5", "1", "2"),
values = c("darkorange2", "olivedrab", "cadetblue4")) +
scale_alpha_discrete(range = c(0.5, 1),
guide = guide_none()) +
geom_line(inherit.aes = FALSE,
aes(supp, len, group = ID),
color = "gray75") +
geom_text(data = data.frame(
x = 1.5,
y = 40,
dose = c("0.5", "1", "2"),
pval = sapply(c("0.5", "1", "2"), function(x) {
round(t.test(len ~ supp,
data = ToothGrowth[ToothGrowth$dose == x,],
paired = TRUE)$p.val, 4)})),
inherit.aes = FALSE,
aes(x = 1.5, y = 40, label = paste("T test: p value =", pval)),
check_overlap = TRUE) +
facet_grid(~dose) +
theme_classic() +
theme(legend.position = "top",
strip.background = element_rect(fill = "gray95", size = 0.25))
# Follow-up question:
# What I want to do next: having another facetting variable ('researcher')
ToothGrowth_1 <- ToothGrowth
# create a random numerical factor to multiply measures with and then enlarge the dataset by a second set of measurements from a different 'researcher':
r <- runif(60, min=0, max=3)
ToothGrowth_1$len <- ToothGrowth_1$len*r
ToothGrowth$researcher <- "A"
ToothGrowth_1$researcher <- "B"
ToothGrowth_total <- rbind(ToothGrowth, ToothGrowth_1)
現在,我想 plot 與上面相同的 plot ,但對兩個“研究人員”組(A 與 B)進行水平切面拆分。 我通過創建和交互術語“researcher”和“dose”並用 facet_wrap 替換 facet_grid 找到了一個解決方法,但我更願意看到 facet_grid 的解決方案,因為它使其他一切變得更容易。 感謝您的幫助,非常感謝!
感謝您發布后續。
做到這一點的自然方法是map
兩個級別,盡管我認為與其完全重寫來實現這一點,我可能只會使用 2 個sapply
調用 - 一個用於新因素的每個級別:
ggplot(ToothGrowth_total, aes(supp, len, fill = dose, alpha = supp)) +
geom_boxplot() +
scale_fill_manual(name = "Dosis",
labels = c("0.5", "1", "2"),
values = c("darkorange2", "olivedrab", "cadetblue4")) +
scale_alpha_discrete(range = c(0.5, 1),
guide = guide_none()) +
geom_line(inherit.aes = FALSE,
aes(supp, len, group = ID),
color = "gray75") +
geom_text(data = data.frame(
x = 1.5,
y = c(40, 40, 40, 70, 70, 70),
researcher = c("A", "A", "A", "B", "B", "B"),
dose = c("0.5", "1", "2", "0.5", "1", "2"),
pval = c(sapply(c("0.5", "1", "2"), function(x) {
round(t.test(len ~ supp,
data = subset(ToothGrowth_total, dose == x & researcher == "A"),
paired = TRUE)$p.val, 4)}),
sapply(c("0.5", "1", "2"), function(x) {
round(t.test(len ~ supp,
data = subset(ToothGrowth_total, dose == x & researcher == "B"),
paired = TRUE)$p.val, 4)}))),
inherit.aes = FALSE,
aes(x = x, y = y, label = paste("T test: p value =", pval)),
check_overlap = TRUE) +
facet_grid(researcher~dose, scales = "free_y") +
theme_classic() +
theme(legend.position = "top",
strip.background = element_rect(fill = "gray95", size = 0.25))
如果我沒記錯的話,我實際上找到了一種更簡單的方法:
ToothGrowth_total$researcher_dose <- interaction(ToothGrowth_total$researcher, ToothGrowth_total$dose)
ggplot(ToothGrowth_total, aes(supp, len, fill = dose, alpha = supp)) +
geom_boxplot() +
scale_fill_manual(name = "Dosis",
labels = c("0.5", "1", "2"),
values = c("darkorange2", "olivedrab", "cadetblue4")) +
scale_alpha_discrete(range = c(0.5, 1),
guide = guide_none()) +
geom_line(inherit.aes = FALSE,
aes(supp, len, group = ID),
color = "gray75") +
# geom_text(data = data.frame(
# x = 1.5,
# y = c(40, 40, 40, 70, 70, 70),
# researcher = c("A", "A", "A", "B", "B", "B"),
# dose = c("0.5", "1", "2", "0.5", "1", "2"),
# pval = c(sapply(c("0.5", "1", "2"), function(x) {
# round(t.test(len ~ supp,
# data = subset(ToothGrowth_total, dose == x & researcher == "A"),
# paired = TRUE)$p.val, 4)}),
# sapply(c("0.5", "1", "2"), function(x) {
# round(t.test(len ~ supp,
# data = subset(ToothGrowth_total, dose == x & researcher == "B"),
# paired = TRUE)$p.val, 4)}))),
# inherit.aes = FALSE,
# aes(x = x, y = y, label = paste("T test: p value =", pval)),
# check_overlap = TRUE) +
# => instead subsituted by:
stat_compare_means(aes(x="researcher_dose"), method = "t.test", paired = TRUE)+
facet_grid(researcher~dose, scales = "free_y") +
theme_classic() +
theme(legend.position = "top",
strip.background = element_rect(fill = "gray95", size = 0.25))
我希望我在這里沒有遺漏任何重要的東西,但它會產生相同的t.test
結果,因此我認為它是正確的。 如果沒有,請告訴我。 唯一的區別是“researcher_dose”現在也顯示為 x 軸標簽。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.