[R] Three questions about plotting

Gabor Grothendieck ggrothendieck at gmail.com
Fri Feb 28 01:33:26 CET 2014


On Thu, Feb 27, 2014 at 7:19 PM, David Parkhurst <parkhurs at imap.iu.edu> wrote:
> I would like to plot three graphs, one above the other, of three "y"
> variables that have different scales against a common Date variable, as with
> the code below.
>
> Q1.  If I understand correctly, I can't use lattice graphics because my y's
> have different scales.  Is that correct?  All the lattice or trellis plots
> I've seen have common "y" scales for all plots.
>
> I have two problems with what this code produces:
> Q2.  How can I get the vertical dimension of all three plots to be the same?
> I know that I've made them different by using different mar numbers, but I
> had to do that, I thought, to leave room for date axis labels.  I don't want
> to leave wasted space between the plots.
>
> Q3.  Why are my dates not coming out in the format I've specified in the
> axis.Date statement?
>
> DateNum <- seq(8248,9247)
> Date<-as.Date(DateNum, origin="1970/01/01")
> y1<- runif(1000,0,1)
> y2<- runif(1000,0,100)
> y3<- runif(1000,0,10)
> par(mfrow=c(4,1))
> par(mar=c(0,4,0,2)+0.1)
> plot(y1 ~ Date, xaxt = "n", type = "p",cex=0.7)
> plot(y2 ~ Date, xaxt = "n", type = "p",cex=0.7)
> par(mar=c(4,4,0,2)+0.1)
> plot(y3 ~ Date, xaxt = "n", type = "p",cex=0.7)
> DateLbls <-
> seq.Date(from=as.Date("1992/08/01"),to=as.Date("1995/04/27"),by="3 months")
> axis.Date(side=1,Date,at=DateLbls, labels=DateLbls, format="%m-%y")
>

Try this:

library(lattice)
DF <- data.frame(make.groups(y1, y2, y3), Date)
xyplot(data ~ Date | which, DF, scales = list(y = list(relation =
"free")), layout = c(1, 3))

-- 
Statistics & Software Consulting
GKX Group, GKX Associates Inc.
tel: 1-877-GKX-GROUP
email: ggrothendieck at gmail.com




More information about the R-help mailing list