1

I've done a conditional boxplot with my data, with the bwplot function of the lattice library.

    A1 <- bwplot(measure ~ month | plot , data = prueba,
              strip = strip.custom(bg = 'white'),   
              cex = .8, layout = c(2, 2),
              xlab = "Month", ylab = "Total",
              par.settings = list(
                box.rectangle = list(col = 1),
                box.umbrella  = list(col = 1),
                plot.symbol   = list(cex = .8, col = 1)),
              scales = list(x = list(relation = "same"),
                            y = list(relation = "same")))

Then, I've done a xyplot because I want to add the precipitation data to the previous graph, using xyplot from lattice library also.

    B1 <- xyplot(precip ~ month | plot, data=prueba,
   type="b",
   ylab = '% precip',
   xlab = 'month',
   strip = function(bg = 'white', ...)
     strip.default(bg = 'white', ...),
   scales = list(alternating = F,
                 x=list(relation = 'same'),
                 y=list(relation = 'same')))

I've try to draw them on the same graph using grid.arrange from gridExtra library:

    grid.arrange(A1,B1)

But with this, I don't overlap the data, but the result is this boxplot and xyplot

How could I draw the precipitacion data "inside" the boxplots conditioned by plot?

Thank you

Roland
  • 127,288
  • 10
  • 191
  • 288
TeSeA
  • 55
  • 6

2 Answers2

3

Using the barley data as Andrie did, another approach with latticeExtra:

library(lattice)
library(latticeExtra)

bwplot(yield ~ year | variety , data = barley, fill = "grey") +
xyplot(yield ~ year | variety , data = barley, col = "red")

enter image description here

  • Thank you @Pascal. It seems very simple and easy, but it doesn't work with my data. I have use bwplot(...) + as.layer(xyplot(...)), but it only draws the bwplot, without giving any error. – TeSeA Aug 04 '14 at 09:57
  • Why to use `as.layer()`? There is no `as.layer()` in the example. And as you are trying to plot at the same time totals and percentage, I am not really surprised it doesn't work. –  Aug 04 '14 at 10:00
  • Sorry, I have used `as.layer()` because without it, and error said `Error in as.layer(lay) : argument "lay" is missing, with no default`. – TeSeA Aug 04 '14 at 10:04
  • Anyway, now I've tried again without `as.layer()` and the error didn't appear, all appear to work properly, but it only draws the first plot (if I use first `bwplot` it draws it, and if I use first `xyplot` it only draws it – TeSeA Aug 04 '14 at 10:06
  • 1
    Sure, `% Precip` is 33 times bigger than `Total`. –  Aug 04 '14 at 10:07
  • Of course!!!! I don't know where is my mind, I'm sorry. I have divided by 10 the precipitation data and now it works perfectly fine. I'm very grateful @Pascal – TeSeA Aug 04 '14 at 10:16
2

You need to create a custom panel function. I demonstrate with the built-in barley data:

Imagine you want to create a simple bwplot and xyplot using the barley data. Your code might look like this:

library(lattice)

bwplot(yield ~ year | variety , data = barley)
xyplot(yield ~ year | variety , data = barley)

To combine the plots, you need to create a panel function that first plots the default panel.bwplot and then the panel.xyplot. Try this:

bwplot(yield ~ year | variety , data = barley,
       panel = function(x, y, ...){
         panel.bwplot(x, y, fill="grey", ...)
         panel.xyplot(x, y, col="red", ...)
       }
)

enter image description here


There is some information about doing this in the help for ?xyplot - scroll down to the details of the panel argument.

Andrie
  • 176,377
  • 47
  • 447
  • 496
  • I need to understand how the panel function works properly, I'll try. Thanks for your answer. – TeSeA Aug 04 '14 at 09:55