[R] Stacked plots with common x-axis and different y-axis
Gabor Grothendieck
ggrothendieck at gmail.com
Thu Aug 6 16:04:36 CEST 2009
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.
>> >
>>
>
>
>
>
More information about the R-help
mailing list