Anna Zakrisson
2013-Feb-25 06:07 UTC
[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@su.se Tel work: +49-(0)3091541281 Mobile: +49-(0)15777374888 LinkedIn: se.linkedin.com/pub/anna-zakrisson-braeunlich/33/5a2/51b><((((º>`•. . • `•. .• `•. . ><((((º>`•. . • `•. .•`•. .><((((º> [[alternative HTML version deleted]]
Jim Lemon
2013-Feb-25 09:22 UTC
[R] How to plot 2 continous variables on double y-axis with 2 factors: ggplot2, gplot, lattice, sciplot?
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
John Kane
2013-Feb-28 17:39 UTC
[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? 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: 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 > stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > 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!
John Kane
2013-Mar-01 15:20 UTC
[R] How to plot 2 continous variables on double y-axis with 2 factors: ggplot2, gplot, lattice, sciplot?
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 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:?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? grokbase.com/t/r/r-help/104yxg1q38/r-plotting-multiple-cis [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: se.linkedin.com/pub/anna-zakrisson-braeunlich/33/5a2/51b [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 > stat.ethz.ch/mailman/listinfo/r-help [stat.ethz.ch/mailman/listinfo/r-help] > PLEASE do read the posting guide > R-project.org/posting-guide.html [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 inbox.com/earth [inbox.com/earth] ____________________________________________________________ FREE 3D EARTH SCREENSAVER - Watch the Earth right on your desktop!
Apparently Analagous Threads
- Ggplot2: Moving legend, change fill and removal of space between plots when using grid.arrange() possible use of facet_grid?
- ggpliot2: reordering of factors in facets facet.grid(). Reordering of factor on x-axis no problem.
- introducing jitter in overlapping graphs using ggplots (plotmeans). Also sciplot.
- lattice: double y - problem changing axis color after doubleYScale
- ggplot2: changing strip text in facet_grid and a legend text problem