1

I am having problems generating a ggplot.

My plot is a simple box plot but with additional annotations.

Those annotations are the significance levels of multiple comparisons.

This is better explained by DatamineR here: How to draw the boxplot with significant level?

I only want to plot those that are significantly different and want the function to be robust.

So to make it stupid simple I just want to add X lines to a ggplot

example:

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
}

How can I now make a function that will add n.sig lines to my plot?

n.sig = 3

would mean the following:

my.plot +
 geom_line(data1,aes(x1,y1))+
 geom_line(data2,aes(x2,y2))+
 geom_line(data3,aes(x3,y3))

I already have all the data frames for my geom_line functions and I figured out that I can create a function call using this function

do.call("geom_line",list("data1","aes(x1,y1)")

but how can I then make this into one big function call for ggplot?

In other words I want this function that I wrote for base plot to work in 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
}
}

run it on mtcars:

data<-mtcars
data$cyl<-as.factor(data$cyl)
all.f("cyl","mpg")
Community
  • 1
  • 1
WojciechF
  • 370
  • 2
  • 14
  • It depends on how your data is structured. Post some example data, and make it fit to your code. Also add some arguments to your function... – Mike Wise Jul 03 '15 at 10:01
  • I just need a function that does the following: take `my.plot` add `+` and add `x` where `x == geom_line(data1,aes(x1,y1))` – WojciechF Jul 03 '15 at 10:52
  • so what are x1 and y1? And x2 and y2? Post some data to help us understand. And read http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – Mike Wise Jul 03 '15 at 10:58
  • 1
    the short answer would be to use a list, `plot + list(layer1, layer2, layer3)` etc. (but I doubt your do.call line works). A better answer, however, is that you don't want multiple layers, but only one data set split according to the group aesthetic. – baptiste Jul 04 '15 at 22:14
  • actually the contents of the `data1...datan` are just coordinates for the position of "significant lines". Are you suggesting that I create a database of actual observations (that are plotted) along with the coordinates for "significant lines"? I can not imagine such database. An example? – WojciechF Jul 05 '15 at 07:55

0 Answers0