Skip to content

Different x-axis scales using c() in latticeExtra

7 messages · John Kane, David Winsemius, Felix Andrews +1 more

#
Hi,

I would like to combine multiple xyplots into a single, multipanel
display.  Using R 3.0.1 in Ubuntu, I have used c() from latticeExtra
to combine three plots, but the x-axis for two plots are on a log
scale and the other is on a normal scale.  I also have included
equispace.log=FALSE to clean up the tick labels.  However, when I try
all of these, the x-axis scale of the first panel is used for all
three.  How do I keep different scales for the different panels?

Here is an example:
library(lattice)
library(latticeExtra)
response <- c(76, 14, 15, 44, 26, 19, 74, 123, 49, 8, 56, 17, 18)
predictor1 <- c(107, 7, 25, 501, 64, 88, 344, 367, 379, 10, 66, 31, 32)
predictor2 <- c(10, 9, 8, 10, 29, 27, 55, 48, 2, 6, 14, 10, 5)
predictor3 <- c(67, 22, 66, 41, 72, 64, 69, 63, 64, 70, 60, 75, 78)

pred1_plot <- xyplot(response ~ predictor1, scales = list(log = TRUE,
equispaced.log = FALSE),
  panel = function(x, y, ...) {
    panel.xyplot(x, y, type = c("p", "r"), cex = 2)
    panel.text(x = log10(8), y = log10(120), labels = "(a)")
  }
)

pred2_plot <- xyplot(response ~ predictor2, scales = list(log = TRUE,
equispaced.log = FALSE),
  panel = function(x, y, ...) {
    panel.xyplot(x, y, type = c("p", "r"), cex = 2)
    panel.text(x = log10(2), y = log10(120), labels = "(b)")
  }
)

pred3_plot <- xyplot(response ~ predictor3, scales = list(y = list(log
= TRUE, equispaced.log = FALSE)),
  panel = function(x, y, ...) {
    panel.xyplot(x, y, type = c("p", "r"), cex = 2)
    panel.text(x = 22, y = log10(120), labels = "(c)")
  }
)

all_plots <- c(pred1_plot, pred2_plot, pred3_plot, layout = c(3, 1), x.same = F)
update(all_plots, xlab=c("Predictor 1","Predictor 2", "Predictor 3"),
scales = list(y=list(log=T, equispaced.log=FALSE), x = c(list(log=T,
equispaced.log=FALSE), list(log=T, equispaced.log=FALSE),
list(log=F))))

update(all_plots, xlab=c("Predictor 1","Predictor 2", "Predictor 3"),
scales = c(list(log = TRUE, equispaced.log = FALSE), list(log = TRUE,
equispaced.log = FALSE), list(y=list(log=T, equispaced.log = FALSE))))

Any help is appreciated!

Thanks,
Jeff
#
No idea what I happening but does this give what you expect

library(gridExtra)
preds  <-  grid.arrange(pred1_plot,pred2_plot, pred3_plot, ncol=3)
preds

John Kane
Kingston ON Canada
____________________________________________________________
FREE ONLINE PHOTOSHARING - Share your photos online with your friends and family!
Visit http://www.inbox.com/photosharing to find out more!
#
On Jul 19, 2013, at 8:18 PM, Jeff Stevens wrote:

            
I assume there was a notice o your console that there were warnings, right? You should offer the full texts of warnings and error messages. Here the full text of the first and second warnings:
$`log scales cannot be changed via 'update'`
update.trellis(all_plots, xlab = c("Predictor 1", "Predictor 2", 
    "Predictor 3"), scales = c(list(log = TRUE, equispaced.log = FALSE), 
    list(log = TRUE, equispaced.log = FALSE), list(y = list(log = T, 
        equispaced.log = FALSE))))
$`'x' is NULL so the result will be NULL`
rep(scales[[nm]], length.out = 2)

The first one is telling you why the results should be different than you expect. I'm not entirely sure what the second one is telling you, but it doesn't sound good.
#
latticeExtra's c() can not combine logarithmic with linear x scales,
I'm afraid.  I would recommend displaying each separate plot on one
page using plot.trellis() or the gridExtra function that John Kane
mentioned.

Cheers
Felix
On 21 July 2013 02:50, David Winsemius <dwinsemius at comcast.net> wrote:

  
    
#
Many thanks, Felix.  Though, it seems like the x.same option should
allow this: "if TRUE, set the x scale relation to "same" and
recalculate panel limits using data from all panels. Otherwise, the x
scales in each panel will be as they were in the original objects (so
in general not the same), the default behaviour."  Or does this just
refer to different axis ranges on the same type of scale?

By the way, the grid.arrange feature from gridExtra seems to produce
the same output as plot.trellis using the split option.  Is there an
advantage to using grid.arrange over plot.trellis?  Also, do you have
pointers to documentation that would help me alter the plot.trellis
output to look more like the c() output (e.g., remove horizontal space
between plots and make sure plots have consistent widths?

Thanks,
Jeff
On Sat, Jul 20, 2013 at 6:45 PM, Felix Andrews <felix at nfrac.org> wrote:
#
Thanks, David.  The warnings occur after the true problem of trying to
plot the three graphs with different scales.  I should have stopped
the example after assigning all_plots <- c(...), then plotted it (and
left out the update statements).  If you do this, the x-axes do not
maintain their scales.

But I take your point that I should have paid more attention to the warnings!

Thanks,
Jeff



On Sat, Jul 20, 2013 at 11:50 AM, David Winsemius
<dwinsemius at comcast.net> wrote:
1 day later
#
Well, I finally found a work around.  It's not pretty, but it works.
I had to alter a number of padding values for par.settings in xyplot()
and minutely control the position and panel.width in plot.trellis().

Thanks for the help.

response <- c(76, 14, 15, 44, 26, 19, 74, 123, 49, 8, 56, 17, 18)
predictor1 <- c(107, 7, 25, 501, 64, 88, 344, 367, 379, 10, 66, 31, 32)
predictor2 <- c(10, 9, 8, 10, 29, 27, 55, 48, 2, 6, 14, 10, 5)
predictor3 <- c(67, 22, 66, 41, 72, 64, 69, 63, 64, 70, 60, 75, 78)

pred1_plot <- xyplot(response ~ predictor1, scales = list(log = TRUE,
equispaced.log = FALSE, y = list(tck = c(1,0))),
  par.settings = list(layout.widths = list(ylab.axis.padding = 1, ylab
= 1, axis.left = 0.8, panel = 1, between = 1, axis.right = 0,
ylab.right = 0, right.padding = 0), axis.components = list(left =
list(pad2 = 0))),
  panel = function(x, y, ...) {
    panel.xyplot(x, y, type = c("p", "r"), cex = 2)
    panel.text(x = log10(8), y = log10(120), labels = "(a)")
  }
)

pred2_plot <- xyplot(response ~ predictor2, ylab = NULL, scales =
list(log = TRUE, equispaced.log = FALSE, y = list(labels = "", tck =
0)),
  par.settings = list(layout.widths = list(ylab.axis.padding = 0, ylab
= 0, axis.left = 0, panel = 1, between = 1, axis.right = 0, ylab.right
= 0, right.padding = 0), axis.components = list(left = list(pad2 =
0))),
  panel = function(x, y, ...) {
    panel.xyplot(x, y, type = c("p", "r"), cex = 2)
    panel.text(x = log10(2), y = log10(120), labels = "(b)")
  }
)

pred3_plot <- xyplot(response ~ predictor3, ylab = NULL, scales =
list(y = list(log = TRUE, equispaced.log = FALSE, labels = "", tck =
c(0, 1))),
  par.settings = list(layout.widths = list(ylab.axis.padding = 0, ylab
= 0, axis.left = 0, panel = 1, between = 1, axis.right = 0, ylab.right
= 0, right.padding = 0), axis.components = list(left = list(pad2 =
0))),
  panel = function(x, y, ...) {
    panel.xyplot(x, y, type = c("p", "r"), cex = 2)
    panel.text(x = 22, y = log10(120), labels = "(c)")
  }
)

pdf(file = "test_plot.pdf", width = 12, height = 5)
print(pred1_plot, split = c(1,1,3,1), position = c(0.02, 0, 1, 1),
panel.width = list(x = 3.6, unit = "in"), more=T)
print(pred2_plot, split = c(2,1,3,1), position = c(0.045, 0, 1, 1),
panel.width = list(x = 3.5, unit = "in"), more=T)
print(pred3_plot, split = c(3,1,3,1), panel.width = list(x = 3.5, unit
= "in"), more=F)
dev.off()
On Sat, Jul 20, 2013 at 6:45 PM, Felix Andrews <felix at nfrac.org> wrote: