[R] FW: Aligning different trellis plots
Deepayan Sarkar
deepayan at stat.wisc.edu
Wed Apr 21 02:06:46 CEST 2004
On Tuesday 20 April 2004 14:31, Wiener, Matthew wrote:
> All --
>
> I am trying to combine trellis plots and having a couple of small
> problems.
>
> I'm trying to combine two trellis plots that display data of
> different kinds. Each has a single row of plots, and I'd like to
> display them over one another. So I use
>
> print(plot1, split = c(1,1,2,1), more = TRUE)
> print(plot2, split = c(1,2,1,2))
>
> I end up with two minor issues. First, I'd like to have the top of
> the bottom plot touch the bottom of the top plot, so it all looks
> like one plot (I'd turn off the strip in the bottom one). I've
> turned off the x labels and bottom axes for the top plot, but there
> still ends up being some space between them. The effect I'm looking
> for is like setting various components of "mar" to 0 in base
> graphics, but I can't figure out how to do it.
This is doable with a bit of hacking the code. There is a hard-coded 1/2
"lines" gap on all sides of a trellis plot. I guess this should be a
user-settable parameter, but it isn't currently. You can change this in
the calculateGridLayout() function in lattice/R/print.trellis.R
(assuming you are using R 1.9.0).
> The second issue has to do with axes. My two plots have the same
> number of panels across, and I would like them to line up vertically.
> However, my y-axis labels, on the left of each plot, are slightly
> different size, and this means that the panels are of slightly
> different sizes in the two plots.
Unfortunately there's not much that can be done about this easily. The
space allocated for tick labels is exactly as much is needed; I
normally consider this an advantage, but in this case it's somewhat
irritating.
You may be able to take advantage a rarely used feature of print.trellis
which controls the panel widths. For your example, this might look
like:
print(plot.1, split = c(1,1,1,2), more = TRUE,
panel.width = list(1, "inches"))
print(plot.2, split = c(1,2,1,2),
panel.width = list(1, "inches"))
although even this is not really perfect.
> Here's a toy example that shows my two problems.
>
> df.1 <- data.frame(x = rep(1:10, 5),
> y = runif(50, 0, 10),
> group = rep(1:5, each = 10))
> df.2 <- data.frame(x = rep(1:10, 5),
> y = runif(50, -1, 1),
> group = rep(1:5, each = 10))
> plot.1 <- xyplot(y ~ x | group, data = df.1,
> scales = list(x = list(alternating = 0),
> y = list(alternating= 1)),
> xlab = "", ## same results with xlab = NULL
> layout = c(5,1,1))
> plot.2 <- xyplot(y ~ x | group, data = df.2,
> scales = list(alternating = 1),
> layout = c(5,1,1))
>
> print(plot.1, split = c(1,1,1,2), more = TRUE)
> print(plot.2, split = c(1,2,1,2))
>
> I realize that it could be the xlab = "" that is giving me trouble --
> it may still be reserving space. But xlab = NULL does the same
> thing, and I haven't been able to find anything else to try.
>
> One moderately ugly way to solve the problem is to use position
> instead of split in the print statement for the trellis plots, and
> use overlapping ranges to force the bottom of the top plot to line up
> with the top of the bottom plot. But it would be nice to have
> something a little more automatic.
>
> Or is there some better way to do this altogether -- perhaps that
> would force a single plot to contain two kinds of panels? That seems
> to really go against the principle of lattice graphics, though.
Probably not, depends on whether the conditioning variable ('group' in
your example) has the same interpretation for both sets. If they do,
your example might be modified as follows (doesn't look particularly
nice though):
df.3 <- rbind(df.1, df.2)
df.3$group2 <- rep(1:2, each = 50)
xyplot(y ~ x | group * group2, data = df.3,
scales = list(y = "free"))
Deepayan
More information about the R-help
mailing list