[R] How to plot 2 continous variables on double y-axis with 2 factors: ggplot2, gplot, lattice, sciplot?
Jim Lemon
jim at bitwrit.com.au
Mon Feb 25 10:22:54 CET 2013
On 02/25/2013 05:07 PM, Anna Zakrisson wrote:
> Hi,
>
> I have a data set with two continous variables that I want to plot MEANS (I
> am not intrerested in median values) on a double-y graph. I also have 2
> factors. I want the factor combinations plotted in different panes.
>
> Dummy dataset:
>
> mydata<- data.frame(factor1 = factor(rep(LETTERS[1:3], each = 40)),
> factor2 = factor(rep(c(1:4), each = 10)),
> y1 = rnorm(120, mean = rep(c(0, 3, 5), each = 40),
> sd = rep(c(1, 2, 3), each = 20)),
> y2 = rnorm(120, mean = rep(c(6, 7, 8), each = 40),
> sd = rep(c(1, 2, 3), each = 20)))
>
> I have tried plotrix(), but I everything is overlaid. Also, I am pretty sure
> that the means are wrong as I have assumed that the below calculates the
> mean of each factor level separately and not the mean per level factor 1 AND
> factor 2.
>
Hi Anna,
I don't think that brkdn.plot or twoord.plot are what you are seeking
here. Try this:
library(plotrix)
panes(matrix(c(1,2),nrow=2))
bp1mean<-matrix(by(mydata$y1,mydata[,c("factor1","factor2")],FUN=mean),ncol=3)
bp2mean<-matrix(by(mydata$y2,mydata[,c("factor1","factor2")],FUN=mean),ncol=3)
bp1sd<-matrix(by(mydata$y1,mydata[,c("factor1","factor2")],FUN=sd),ncol=3)
bp2sd<-matrix(by(mydata$y2,mydata[,c("factor1","factor2")],FUN=sd),ncol=3)
xpos<-matrix(c(0.9,1.9,2.9,3.9,1,2,3,4,1.1,2.1,3.1,4.1),ncol=3)
matplot(x=xpos,y=bp1mean,type="b",
xlim=c(0.5,4.5),ylim=c(-5,10),col="black",
pch=1:4,xaxt="n",lty=1)
axis(1,at=1:4)
dispersion(x=xpos,y=bp1mean,ulim=bp1sd)
matplot(x=xpos,y=bp2mean,type="b",
xlim=c(0.5,4.5),ylim=c(2,12),col="black",
pch=5:8,xaxt="n",lty=2)
dispersion(x=xpos,y=bp2mean,ulim=bp2sd)
axis(1,at=1:4)
I don't know whether I have things in the correct order, but I think
this is the sort of illustration you describe above.
Jim
More information about the R-help
mailing list