[R] Odp: boxplot with 2 treatments and 2 variables (with 2 corresponding y-axes)

Petr PIKAL petr.pikal at precheza.cz
Mon Apr 18 08:29:45 CEST 2011


Hi

r-help-bounces at r-project.org napsal dne 15.04.2011 16:20:23:

> Hi,
> 
> I am a complete newcomer to R and although I can plot standard box-plots 
I am 
> struggling with this...
> 
> I have two treatments - A & B, and 2 variables 1 & 2. I want to compare 
> boxplots of variable 1 with variable 2 for each treatment. I would also 
like 
> them to all be on the same graphic.
> 
> I would like treatment to be on the x axis (A and B) and above A and B 
have 
> their corresponding boxplots for both variables.I would also like 2 
seperate 
> y-axes, one for each variable - Variable 1 has a continuous y axis, and 
> variable 2 is a percentage (of variable 1 in-fact, not that this should 
make a
> difference).
> 
> It would also be helpful if the boxplots were a different colour for 
each 
> variable and that this corresponded somehow to the relevant axis. Or if 
> someone can think of another way to show which treatment each boxplot is 
that 
> would be gratefully recieved
> 

Here is one option

x<-rnorm(10)
y<-rnorm(10)+5
trt<-sample(letters[1:2], 10, replace=T)
df<-cbind(c(x,y), rep(trt,2), rep(c("v1","v2"), each=10))
df<-as.data.frame(df)
df[,1]<-as.numeric(as.character(df[,1]))
boxplot(split(df[,1], interaction(df[,2], df[,3])))
 
however you have only one y axis. Similar result but better structured you 
can get using ggplot2 or lattice packages.

Having 2 y axes is rather confusing but you can scale your values and add 
axis annotation with
axis(....) but I doubt it is worth an effort.

Regards
Petr




> Hope this makes sense and isn't too much of a pain to answer!
> 
> Thanks in advance
> Dave
> 
> 
>    [[alternative HTML version deleted]]
> 
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide 
http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list