# [R] How to plot 2 continous variables on double y-axis with 2 factors: ggplot2, gplot, lattice, sciplot?

John Kane jrkrideau at inbox.com
Thu Feb 28 18:39:18 CET 2013

```It is not at all clear to me exactly what you want but ggplot2 does not allow double-y graphs.  However does this suggest anything useful? http://grokbase.com/t/r/r-help/104yxg1q38/r-plotting-multiple-cis

John Kane

> -----Original Message-----
> From: anna at ecology.su.se
> Sent: Mon, 25 Feb 2013 07:07:22 +0100
> To: r-help at r-project.org
> Subject: [R] How to plot 2 continous variables on double y-axis with 2
> factors: ggplot2, gplot, lattice, sciplot?
>
> 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.
>
> Is there a way of doing this in ggplot2?
> I have also tried plotmeans() in the sciplot package, but was
> unsuccessful.
>
> Sincerely
> Anna Zakrisson
>
> library(plotrix)
> ?brkdn.plot
> par(family="serif",font=1)
> brkdn.plot("y1", "factor1","factor2", data=mydata,
>            mct="mean",md="sd",
>            main="",
>            cex=0.8,
>            stagger=NA,
>            xlab="factor1",
>            ylab = "y1",
>            dispbar=TRUE,
>            type="b",pch=c(0),lty=1,col=par("fg"))
> par(new=TRUE)
> brkdn.plot("y2","factor1", "factor2",data=mydata,
>            mct="mean",md="sd",
>            main="",
>            cex=0.8,
>            stagger=NA,
>            xlab="",
>            ylab = "",
>            dispbar=TRUE,
>            type="b",pch=1,lty=2,col=par("fg"))
> axis(4)
> mtext("y2",side=4,line=3)
> legend("topleft",col=c("black","black"),bty="n",
> lty=c(1:2),legend=c("y1","y2"))
>
>
> Anna Zakrisson Braeunlich
> PhD Student
>
> Department of Ecology Environment and Plant Sciences
> Stockholm University
> Svante Arrheniusv. 21A
> SE-106 91 Stockholm
>
> Lives in Berlin.
> For paper mail:
> Katzbachstr. 21
> D-10965, Berlin - Kreuzberg
> Germany/Deutschland
>
> E-mail: anna.zakrisson at su.se
> Tel work: +49-(0)3091541281
> Mobile: +49-(0)15777374888
>
> ><((((B:>`b?". . b?" `b?". .b?" `b?". . ><((((B:>`b?". . b?" `b?". .b?"
> `b?". .><((((B:>
>
> 	[[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help