[R] Concatenate xyplots

Deepayan Sarkar deepayan.sarkar at gmail.com
Wed Jan 30 21:44:50 CET 2008


On 1/30/08, eite2335 <jeitel at vandals.uidaho.edu> wrote:
>
> OK, this was a silly example.
>
> This one should be not as silly:
>
> ## Created example dataset
>
> data1 = matrix(1:12, nrow=4, ncol=3)
>
> data1 = data.frame(matrix(data1, 4, 3, byrow = T))
>
> colnames(data1) = c("y1", "y2","x")
>
> data2 = data.frame(c(1,1,2,2))
>
> colnames(data2) =c("z")
>
> data = cbind(data1,data2)
>
> load(lattice)
>
> ## Create 2 graphs with the xyplot command
>
> graph1=  xyplot(y1~x|z, data = data)
> graph2 = xyplot(y2~x|z, data = data)
>
>
> ## Print both graphs on the same page
>
> print(graph1, split = c(1,1,1,2), more = T)
> print(graph2, split = c(1,2,1,2), more = F)
>
> ## There is a lot of white space between both graphs in the y direction.
> ## Since the plots share the same x axis I was wondering if there is a way
> to
> ## concatenate the plots to eliminate the white space in the y direction?

xyplot(y1 + y2 ~ x | z, data = data, outer = TRUE)

or to allow different y-axes and a common x-axis,

xyplot(y1 + y2 ~ x | z, data = data, outer = TRUE,
       scales = list(y = list(relation = "free", rot = 0)))

There are of course many other customizations possible.

If you want a common y-axis for each row, but a different one for
different rows, that's a bit harder (though not impossible).

-Deepayan


> Deepayan Sarkar wrote:
> >
> > On 1/30/08, eite2335 <jeitel at vandals.uidaho.edu> wrote:
> >>
> >> OK - here is an example:
> >>
> >> ## Create example data
> >>
> >> data = matrix(1:8, nrow=4, ncol=2)
> >>
> >> ## Name columns "x" and "y"
> >>
> >> colnames(data) = c("x", "y")
> >>
> >> data = data.frame(data)
> >>
> >> ## Create 5 graphs with the xyplot command
> >>
> >> graph1=  xyplot(y~x, data = data)
> >> graph2 = xyplot(y~x, data = data)
> >> graph3 = xyplot(y~x, data = data)
> >> graph4 = xyplot(y~x, data = data)
> >> graph5 = xyplot(y~x, data = data)
> >>
> >> ## Print all five graphs on the same page
> >>
> >> print(graph1, split = c(1,1,1,5), more = T)
> >> print(graph2, split = c(1,2,1,5), more = T)
> >> print(graph3, split = c(1,3,1,5), more = T)
> >> print(graph4, split = c(1,4,1,5), more = T)
> >> print(graph5, split = c(1,5,1,5), more = F)
> >>
> >>
> >> ## Note that there is a lot of white space between each graph in the y
> >> direction.
> >> ## Since the plots share the same x axis I was wondering if there is a
> >> way
> >> to
> >> ## concatenate the plots to eliminate the "wasted" white space in the y
> >> direction
> >> ## and make the graphs themselves larger?
> >
> > This should work:
> >
> > graph1[c(1, 1, 1, 1, 1)]
> >
> > Of course, a silly example will only get you a silly solution.
> >
> > -Deepayan



More information about the R-help mailing list