I want to plot data for a two-factorial experiment in a simple boxplot. The attached diagram shows an example. The grouping structure of the second factor should be indicated by horizontal lines under the axis labels (red lines in the example).

I can solve the problem as shown in the code snipped below. But there is a problem:

When the plot is (vertically) resized, the red lines are not anymore drawn at the desired vertical position. I am looking for a solution for this problem. The text can be placed at a particular "line" in the margin. Isn't there such a possibility to draw lines at a particular "line" instead of giving the user coordinates?

Code snipped used to generate the plot including the red lines:

# generate some values of a two-factorial experiment

values = rnorm(6*10)

lvl1 = c("one","two","three")

lvl2 = c("control","treated")

factor1 = rep(gl(3,10,labels=lvl1),2)

factor2 = gl(2,30,labels=lvl2)

plotgrp = factor(paste(factor1, factor2), levels=c(sapply(lvl1,paste,lvl2)))

# boxplot

at=c(1:3,5:7)

boxplot(values~plotgrp,at=at,xaxt="n",xlab="",las=2,ylab="",main="Example")

axis(1,at=at,labels=rep(lvl1,2),tick=FALSE)

mtext(lvl2,1,line=3,at=c(2,6))

# add the horizontal lines

cxy = par("cxy")

d = 3*cxy[1]

ypos = par("usr")[3] - 2.75*cxy[2]

segments(c(1,5)-d,c(ypos,ypos),c(3,7)+d,c(ypos,ypos),xpd=NA,lwd=2,col="red")

More Jochen Wilhelm's questions See All
Similar questions and discussions