如何解决R:在facet_gridggplot中进行t.test
这是一个非常具体的问题,但是我已经拥有并使用了这个详细且运行良好的代码,因此,我希望找到调整它并使其适用于下一级别的复杂性所需的微小更改。 我得到了什么:
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,group = ID),color = "gray75") +
geom_text(data = data.frame(
x = 1.5,y = 40,dose = c("0.5",pval = sapply(c("0.5",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)
现在,我想绘制与上述相同的图,但对两个“研究者”组(A与B)进行水平构面拆分。 我通过创建“研究人员”和“剂量”的交互项并用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,group = ID),color = "gray75") +
geom_text(data = data.frame(
x = 1.5,y = c(40,40,70,70),researcher = c("A","A","B","B"),dose = c("0.5","2","0.5",pval = c(sapply(c("0.5",function(x) {
round(t.test(len ~ supp,data = subset(ToothGrowth_total,dose == x & researcher == "A"),paired = TRUE)$p.val,4)}),sapply(c("0.5",dose == x & researcher == "B"),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,color = "gray75") +
# geom_text(data = data.frame(
# x = 1.5,# y = c(40,# researcher = c("A",# dose = c("0.5",# pval = c(sapply(c("0.5",function(x) {
# round(t.test(len ~ supp,# data = subset(ToothGrowth_total,# paired = TRUE)$p.val,# sapply(c("0.5",function(x) {
# round(t.test(len ~ supp,# data = subset(ToothGrowth_total,# paired = TRUE)$p.val,# inherit.aes = FALSE,# aes(x = x,# check_overlap = TRUE) +
# => instead subsituted by:
stat_compare_means(aes(x="researcher_dose"),method = "t.test",paired = TRUE)+
facet_grid(researcher~dose,size = 0.25))
我希望我在这里不会错过任何重要的事情,但是它会产生相同的t.test
结果,因此我认为这是正确的。如果没有,请告诉我!
唯一的区别是'researcher_dose'现在也显示为x轴标签。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。