我正在尝试编写用于绘制我的数据的函数。数据总结了来自对照组与治疗组的一些实验的治疗后的测量值。在不同的实验中处理可能不同。我想得到一个手工比较不同实验(通过刻面)的图表,并显示对照组与不同处理的成对比较的 p 值。
这是一个例子:
set.seed(1)
library(ggplot2)
library(ggpubr) ## calculates the statistics for the graph
library(dplyr)
##creating dataset
exp1<-data.frame(exp = "exp1",treatment = rep(c("control","drug A","drug B","drug C"),10),result = runif(40,min =1,max = 10))
exp2<-data.frame(exp = "exp2","drug A"),result = runif(20,max = 10))
exp3<-data.frame(exp = "exp3",result = runif(30,max = 10))
data<-bind_rows(exp1,exp2,exp3)
##function for graphing the data
graph<-function(data,col_Name) {
treatment<-as.character(unique(data$treatment)) ## in the real dataset the treatment is an ordered factor,so use as.character here
g<-ggplot(data,aes(x=treatment,y = data[,col_Name],fill = treatment)) +
geom_boxplot(color = "black",alpha = 0.3)+
geom_dotplot(binaxis = "y",stackdir = "center",dotsize = 2,color = "black") +
facet_wrap(~exp,nrow = 1)+ #facetting the data
stat_compare_means(method = "t.test",aes(label = ..p.format..),comparisons = mapply(c,"control",treatment[2:length(treatment)],SIMPLIFY = F) ) ### with mapply I create the list of pairwise comparisons I want to have
return(g)
}
graph(data,"result")
[![在此处输入图片描述][1]][1]
这就是我目前得到的: [1]:https://i.stack.imgur.com/3qJnd.png
问题在于它仅在实验中包含所有 4 个组时才显示统计数据。我希望该函数以某种方式适用于任何给定的方面(意味着每个单独的实验)跳过此 exp 中缺少数据的组,用数据绘制组并计算与对照组成对比较的统计数据。
我想知道如何为每个方面指定/跳过缺少数据的组,以便显示每个方面的统计数据?
非常感谢! 最好!