Jason Rupert
2009-Aug-04 21:33 UTC
[R] Stacked plots with common x-axis and different y-axis
Is there a place that shows how to create two plots that are stacked on top of each other where they share a common x-axis scale, but have differnt y-axis scale? Say have the following data: airquality Stack plot(airquality$Day, airquality$Wind) on top of plot(airquality$Day, airquality$Temp). I am interested in stacking the two on top of each other with no seam, or plotting the two lines with two different y-axis scales on the same plot. Thanks for any feedback and insights.
Jason Rupert
2009-Aug-05 03:49 UTC
[R] Stacked plots with common x-axis and different y-axis
time <- seq(0,72,12) betagal.abs <- c(0.05,0.18,0.25,0.31,0.32,0.34,0.35) cell.density <- c(0,1000,2000,3000,4000,5000,6000) #add extra space to right margin of plot within frame par(mar=c(5, 4, 4, 4) + 0.1) # Plot first set of data and draw its axis plot(time, betagal.abs, pch=16, axes=F, ylim=c(0,1), xlab="", ylab="", type="b",col="black", main="Mike's test data") axis(2, ylim=c(0,1),col="black") mtext("Beta Gal Absorbance",side=2,line=2.5) box() I found the following example, but it is not quite what I am looking for. Ideally, these one of these plots would be above the other plot, but keeping the same x-axis. It is good to see that others have had similar issues and tried to work through methods to address it. Thank you again for any feedback that might be provided. http://tolstoy.newcastle.edu.au/R/e5/help/08/12/9856.html # Allow a second plot on the same graph par(new=T) # Plot the second plot and put axis scale on right plot(time, cell.density, pch=15, xlab="", ylab="", ylim=c(0,7000), axes=F, type="b", col="red") mtext("Cell Density",side=4,col="red",line=2.5) axis(4, ylim=c(0,7000), col="red",col.axis="red") # Draw the time axis axis(1,pretty(range(time),10)) mtext("Time (Hours)",side=1,col="black",line=2.5) # Add Legend legend(5,7000,legend=c("Beta Gal","Cell Density"),text.col=c("black","red"),pch=c(16,15),col=c("black","red")) --- On Tue, 8/4/09, Jason Rupert <jasonkrupert at yahoo.com> wrote:> From: Jason Rupert <jasonkrupert at yahoo.com> > Subject: Stacked plots with common x-axis and different y-axis > To: R-help at r-project.org > Cc: jasonkrupert at yahoo.com > Date: Tuesday, August 4, 2009, 4:33 PM > Is there a place that shows how to > create two plots that are stacked on top of each other where > they share a common x-axis scale, but have differnt y-axis > scale? > > Say have the following data: airquality > Stack plot(airquality$Day, airquality$Wind) on top of? > plot(airquality$Day, airquality$Temp). > > I am interested in stacking the two on top of each other > with no seam, or plotting the two lines with two different > y-axis scales on the same plot.? > > Thanks for any feedback and insights. > > > > ? ? ? >
Jason Rupert wrote:> Is there a place that shows how to create two plots that are stacked on top of each other where they share a common x-axis scale, but have differnt y-axis scale? > > Say have the following data: airquality > Stack plot(airquality$Day, airquality$Wind) on top of plot(airquality$Day, airquality$Temp). > > I am interested in stacking the two on top of each other with no seam, or plotting the two lines with two different y-axis scales on the same plot. >Hi Jason, Have a look at twoord.plot in the plotrix package. Jim
Gabor Grothendieck
2009-Aug-06 01:09 UTC
[R] Stacked plots with common x-axis and different y-axis
Try this: library(zoo) # ignore the fact that months have different lengths z <- with(airquality, zoo(cbind(Wind, Temp), Month + (Day - 1)/31)) plot(z) # each on separate plot stacked above each other plot(z, screen = 1) # both on same plot library(lattice) xyplot(z) xyplot(z, screen = 1) ?plot.zoo has examples of using two y axes on the same plot On Tue, Aug 4, 2009 at 5:33 PM, Jason Rupert<jasonkrupert at yahoo.com> wrote:> Is there a place that shows how to create two plots that are stacked on top of each other where they share a common x-axis scale, but have differnt y-axis scale? > > Say have the following data: airquality > Stack plot(airquality$Day, airquality$Wind) on top of ?plot(airquality$Day, airquality$Temp). > > I am interested in stacking the two on top of each other with no seam, or plotting the two lines with two different y-axis scales on the same plot. > > Thanks for any feedback and insights. > > ______________________________________________ > 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. >
Jason Rupert
2009-Aug-06 13:20 UTC
[R] Stacked plots with common x-axis and different y-axis
Gabor, Thanks a ton for your insights. Two questions - Here is the code I tried to run: z <- with(airquality, zoo(cbind(Wind, Temp), Month + (Day - 1)/31)) plot(z, ylab=c("Wind (mph)", "Temp (Deg F)"), col=c("royalblue1", "red3"), xlab = c("Month Index", col="dark red")) grid() The grid does not appear to fit within the x-axis and y-axis limits, and also the Month Index text appears to be corrupted. Is there a way to fix this? Thanks again for the insights. --- On Wed, 8/5/09, Gabor Grothendieck <ggrothendieck at gmail.com> wrote:> From: Gabor Grothendieck <ggrothendieck at gmail.com> > Subject: Re: [R] Stacked plots with common x-axis and different y-axis > To: "Jason Rupert" <jasonkrupert at yahoo.com> > Cc: R-help at r-project.org > Date: Wednesday, August 5, 2009, 8:09 PM > Try this: > > library(zoo) > # ignore the fact that months have different lengths > z <- with(airquality, zoo(cbind(Wind, Temp), Month + > (Day - 1)/31)) > plot(z) # each on separate plot stacked above each other > plot(z, screen = 1) # both on same plot > > library(lattice) > xyplot(z) > xyplot(z, screen = 1) > > ?plot.zoo has examples of using two y axes on the same > plot > > > On Tue, Aug 4, 2009 at 5:33 PM, Jason Rupert<jasonkrupert at yahoo.com> > wrote: > > Is there a place that shows how to create two plots > that are stacked on top of each other where they share a > common x-axis scale, but have differnt y-axis scale? > > > > Say have the following data: airquality > > Stack plot(airquality$Day, airquality$Wind) on top of > ?plot(airquality$Day, airquality$Temp). > > > > I am interested in stacking the two on top of each > other with no seam, or plotting the two lines with two > different y-axis scales on the same plot. > > > > Thanks for any feedback and insights. > > > > ______________________________________________ > > 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. > > >
Gabor Grothendieck
2009-Aug-06 14:04 UTC
[R] Stacked plots with common x-axis and different y-axis
In multipanel displays this sort of customization must be done in the panel function: pnl <- function(...) { lines(...); grid() } library(zoo) z <- with(airquality, zoo(cbind(Wind, Temp), Month + (Day - 1)/31)) plot(z, col = c("royalblue1", "red3"), panel = pnl, xlab = "", ylab=c("Wind (mph)", "Temp (Deg F)")) or use the panel function below, instead, if you also want the dark red X Axis label. The first three lines of the function body get the panel number, the number of panels and test whether we are at the bottom panel. There are more examples of panel functions in the Examples section of ?plot.zoo pnl <- function(...) { panel.number <- parent.frame()$panel.number nser <- parent.frame()$nser # if bottom panel if (panel.number == nser) { mtext("Month Index", side = 1, line = 3, col = "dark red") } lines(...) grid() } On Thu, Aug 6, 2009 at 9:20 AM, Jason Rupert<jasonkrupert at yahoo.com> wrote:> Gabor, > > Thanks a ton for your insights. > > Two questions - Here is the code I tried to run: > z <- with(airquality, zoo(cbind(Wind, Temp), Month + (Day - 1)/31)) > plot(z, > ? ? ylab=c("Wind ?(mph)", "Temp (Deg F)"), col=c("royalblue1", "red3"), > ? ? xlab = c("Month Index", col="dark red")) > > grid() > > The grid does not appear to fit within the x-axis and y-axis limits, and also the Month Index text appears to be corrupted. > > Is there a way to fix this? > > Thanks again for the insights. > > > > > --- On Wed, 8/5/09, Gabor Grothendieck <ggrothendieck at gmail.com> wrote: > >> From: Gabor Grothendieck <ggrothendieck at gmail.com> >> Subject: Re: [R] Stacked plots with common x-axis and different y-axis >> To: "Jason Rupert" <jasonkrupert at yahoo.com> >> Cc: R-help at r-project.org >> Date: Wednesday, August 5, 2009, 8:09 PM >> Try this: >> >> library(zoo) >> # ignore the fact that months have different lengths >> z <- with(airquality, zoo(cbind(Wind, Temp), Month + >> (Day - 1)/31)) >> plot(z) # each on separate plot stacked above each other >> plot(z, screen = 1) # both on same plot >> >> library(lattice) >> xyplot(z) >> xyplot(z, screen = 1) >> >> ?plot.zoo has examples of using two y axes on the same >> plot >> >> >> On Tue, Aug 4, 2009 at 5:33 PM, Jason Rupert<jasonkrupert at yahoo.com> >> wrote: >> > Is there a place that shows how to create two plots >> that are stacked on top of each other where they share a >> common x-axis scale, but have differnt y-axis scale? >> > >> > Say have the following data: airquality >> > Stack plot(airquality$Day, airquality$Wind) on top of >> ?plot(airquality$Day, airquality$Temp). >> > >> > I am interested in stacking the two on top of each >> other with no seam, or plotting the two lines with two >> different y-axis scales on the same plot. >> > >> > Thanks for any feedback and insights. >> > >> > ______________________________________________ >> > 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. >> > >> > > > >