R 向ggplot添加不同层数的函数

R 向ggplot添加不同层数的函数,r,function,ggplot2,R,Function,Ggplot2,我在生成ggplot时遇到问题 我的绘图是一个简单的方框图,但带有附加注释 这些注释是多重比较的显著性水平 DatamineR在这里更好地解释了这一点: 我只想画出那些明显不同的,并且希望函数是健壮的 所以,为了让它变得简单,我只想在ggplot中添加X行 例如: custom.plot <- function{ n.sig <- number of significant observations obtained by a different function

我在生成ggplot时遇到问题

我的绘图是一个简单的方框图,但带有附加注释

这些注释是多重比较的显著性水平

DatamineR在这里更好地解释了这一点:

我只想画出那些明显不同的,并且希望函数是健壮的

所以,为了让它变得简单,我只想在ggplot中添加
X

例如:

custom.plot <- function{
    n.sig <- number of significant observations obtained by a different function 
    my.plot <- ggplot(data,aes(x,y)) + geom_boxplot()
    ### HERE should be a function that adds other layers to my plot
}
这意味着:

my.plot +
 geom_line(data1,aes(x1,y1))+
 geom_line(data2,aes(x2,y2))+
 geom_line(data3,aes(x3,y3))
我已经有了我的
geom_line
函数的所有数据帧,我发现我可以使用这个函数创建函数调用

do.call("geom_line",list("data1","aes(x1,y1)")
但是我怎样才能将它变成
ggplot
的一个大函数调用呢

换句话说,我希望我为基本绘图编写的函数在ggplot2中工作:

all.f <-function(x,y){
boxplot(data[,y]~data[,x],ylab=y,xlab="")
dt <- dunn.test(data[,y],data[,x],method="bonferroni")
### Split outcome by group
out.sp <- split(data[,y],data[,x])
len.vars <- sapply(out.sp,length)
len.all <- length(len.vars)
imp.dt<-which(dt$P.adjusted<0.05)
check.pairs <- function(x){
    if(x==1){
        return(cbind(1,2))
    }else if(x==2){
        return(cbind(1,3))
    }else if(x==3){
        return(cbind(2,3))
    }else if(x==4){
        return(cbind(1,4))
    }else if(x==5){
        return(cbind(2,4))
    }else if(x==6){
        return(cbind(3,4))
    }else if(x==7){
        return(cbind(1,5))
    }else if(x==8){
        return(cbind(2,5))
    }else if(x==9){
        return(cbind(3,5))
    }else if(x==10){
        return(cbind(4,5))
    }else{
        return(NA)
        }
}
connections <- sapply(imp.dt,check.pairs)
n.connections <- length(imp.dt)
df.con <- list(NULL)
my <-max(data[,y],na.rm=T)

cord <- NULL
for(i in 1:n.connections){
    cord <- c(cord,my-((0+i)*my*0.03))
}

connect<-matrix(rbind(connections,cord),nrow=3)

boxplot(data[,y]~data[,x],ylab=y,xlab="")
if(!n.connections==0){
 for(i in 1:n.connections){
    segments(connect[1,i],connect[3,i],connect[2,i],connect[3,i])
 }
} else {
    NULL
}
}

all.f这取决于数据的结构。发布一些示例数据,并使其适合您的代码。还要为函数添加一些参数…我只需要一个函数,它可以执行以下操作:获取
my.plot
add
+
并添加
x
其中
x==geom_线(data1,aes(x1,y1))
那么x1和y1是什么呢?x2和y2呢?发布一些数据来帮助我们理解。阅读简短的答案是使用列表,
plot+list(layer1、layer2、layer3)
等等(但我怀疑你的do.call热线是否有效)。然而,更好的答案是,您不需要多个层,而只需要根据组美学划分一个数据集。实际上,
data1…datan
的内容只是“重要线”位置的坐标。你是不是建议我创建一个实际观测数据的数据库(绘制)以及“重要线”的坐标?我无法想象这样的数据库。例如,这取决于数据的结构。发布一些示例数据,并使其适合您的代码。还要为函数添加一些参数…我只需要一个函数,它可以执行以下操作:获取
my.plot
add
+
并添加
x
其中
x==geom_线(data1,aes(x1,y1))
那么x1和y1是什么呢?x2和y2呢?发布一些数据来帮助我们理解。阅读简短的答案是使用列表,
plot+list(layer1、layer2、layer3)
等等(但我怀疑你的do.call热线是否有效)。然而,更好的答案是,您不需要多个层,而只需要根据组美学划分一个数据集。实际上,
data1…datan
的内容只是“重要线”位置的坐标。你是不是建议我创建一个实际观测数据的数据库(绘制)以及“重要线”的坐标?我无法想象这样的数据库。举个例子?
all.f <-function(x,y){
boxplot(data[,y]~data[,x],ylab=y,xlab="")
dt <- dunn.test(data[,y],data[,x],method="bonferroni")
### Split outcome by group
out.sp <- split(data[,y],data[,x])
len.vars <- sapply(out.sp,length)
len.all <- length(len.vars)
imp.dt<-which(dt$P.adjusted<0.05)
check.pairs <- function(x){
    if(x==1){
        return(cbind(1,2))
    }else if(x==2){
        return(cbind(1,3))
    }else if(x==3){
        return(cbind(2,3))
    }else if(x==4){
        return(cbind(1,4))
    }else if(x==5){
        return(cbind(2,4))
    }else if(x==6){
        return(cbind(3,4))
    }else if(x==7){
        return(cbind(1,5))
    }else if(x==8){
        return(cbind(2,5))
    }else if(x==9){
        return(cbind(3,5))
    }else if(x==10){
        return(cbind(4,5))
    }else{
        return(NA)
        }
}
connections <- sapply(imp.dt,check.pairs)
n.connections <- length(imp.dt)
df.con <- list(NULL)
my <-max(data[,y],na.rm=T)

cord <- NULL
for(i in 1:n.connections){
    cord <- c(cord,my-((0+i)*my*0.03))
}

connect<-matrix(rbind(connections,cord),nrow=3)

boxplot(data[,y]~data[,x],ylab=y,xlab="")
if(!n.connections==0){
 for(i in 1:n.connections){
    segments(connect[1,i],connect[3,i],connect[2,i],connect[3,i])
 }
} else {
    NULL
}
}
data<-mtcars
data$cyl<-as.factor(data$cyl)
all.f("cyl","mpg")