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

John Kane jrkrideau at inbox.com
Fri Mar 1 16:20:55 CET 2013


This definitely looks like a job for Jim Lemon. :)

I am pretty sure that you cannot do it in ggplot2 as there is no way that I am aware of to have two y-axes except as if y2 is a transformation of y1. For example, apparently, it is possible to have a Centigrade and Fahrenheit scale--something that is handy in North America.

As I understand it, this is both for technical  and ideological reasons.  There are very strong arguments against using double-scaled graphs.  See http://stackoverflow.com/questions/3099219/how-to-use-ggplot2-make-plot-with-2-y-axes-one-y-axis-on-the-left-and-another for Hadley's point of view and some links.

If there is nothing that seems to work in plotrix I suppose you might have a look at lattice but I never use it and have no idea of its capacities.

If you describe what you are graphing some of the real data display experts, of whom I am not one, may be able to suggest workarounds or alternative ways of displaying the data.  

A last resort is  base graphics which looks nasty but possible I think.

Below is an example of how to create a simple double y-axis graph.  With a combination of either the mfcol", "mfrow" or "layout" to arranage the plots you could do it but it does not look easy.

##=============================================##
#Multiple Y-axis

# What we are doing is plotting two different graphs into the same frame or 
# space with the same x-axis scale but different y-axis scales.  There is no 
# reason why we could not have used a different x-axis scale, plotted on the 
# top of the plot (well, except if Hadley saw it, he might get violent). 

# We find that mtext() does not support rotated text for placing the y2 label
# Thus, we need to use text() and adjust the 'srt' argument to rotate the text 
# and adjust the x and y axis values for placement. We also set the 'xpd'
# argument so that the text is not clipped at the plot region.

# Create the data to be graphed
        x<-1:10
        y1<-x
        y2<-x^2

# Set the par values
        op  <- par(las=1,xaxs="r",mai=c(1, 1,1,1))

# Draw first plot        
        plot(x,y1,xlim=c(0,10),ylim=c(0,10), ylab="y1", las = 1)
        title(main="Multiple Y-Axes in R")
# Draw second plot
        par(new=TRUE)
        plot(x,y2,xlim=c(0,10),xaxt="n",yaxt="n",ylab="",pch=16)
        axis(4,at=c(0,20,40,60,80,100))
        text(11, 50, "y2", srt = 270, xpd = TRUE)
        par(op)  # reset par 

# See ?text, ?par and ?mtext for more information.

##=============================================##
John Kane
Kingston ON Canada

-----Original Message-----
From: anna at ecology.su.se
Sent: Fri, 01 Mar 2013 11:53:31 +0100
To: jrkrideau at inbox.com
Subject: RE: [R] How to plot 2 continous variables on double y-axis with 2 factors: ggplot2, gplot, lattice, sciplot?

 Hi and thank you for taking your time. I have now attached a dummy graph to this mail (stating what is wrong with it in the title). 

 Anna

 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
 LinkedIn: http://se.linkedin.com/pub/anna-zakrisson-braeunlich/33/5a2/51b

 ><((((º>`•. . • `•. .• `•. . ><((((º>`•. . • `•. .• `•. .><((((º>

	 -----Original Message-----
 From: John Kane <jrkrideau at inbox.com>
 To: Anna Zakrisson <anna at ecology.su.se>, "r-help at r-project.org" <r-help at r-project.org>
 Date: Thu, 28 Feb 2013 09:39:18 -0800
 Subject: RE: [R] How to plot 2 continous variables on double y-axis with 2 factors: ggplot2, gplot, lattice, sciplot?

 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 [http://grokbase.com/t/r/r-help/104yxg1q38/r-plotting-multiple-cis]

 John Kane
 Kingston ON Canada

 > -----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
 > LinkedIn: http://se.linkedin.com/pub/anna-zakrisson-braeunlich/33/5a2/51b [http://se.linkedin.com/pub/anna-zakrisson-braeunlich/33/5a2/51b]
 >
 > ><((((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 [https://stat.ethz.ch/mailman/listinfo/r-help]
 > PLEASE do read the posting guide
 > http://www.R-project.org/posting-guide.html [http://www.R-project.org/posting-guide.html]
 > and provide commented, minimal, self-contained, reproducible code.

 ____________________________________________________________
 FREE 3D EARTH SCREENSAVER - Watch the Earth right on your desktop!
 Check it out at http://www.inbox.com/earth [http://www.inbox.com/earth]

____________________________________________________________
FREE 3D EARTH SCREENSAVER - Watch the Earth right on your desktop!



More information about the R-help mailing list