5

I want to have the box plotted with thicker lines. In boxplot function I simply put lwd=2, but in the lattice bwplot I can pull my hair out and haven't found a solution!enter image description here (with the box I mean the blue thing in the image above)

Sample code to work with:

require(lattice)
set.seed(123)
n <- 300
type <- sample(c("city", "river", "village"), n, replace = TRUE)
month <- sample(c("may", "june"), n, replace = TRUE)
x <- rnorm(n)
df <- data.frame(x, type, month)

bwplot(x ~ type|month, data = df, panel=function(...) {
    panel.abline(h=0, col="green")
    panel.bwplot(...)
})
Tomas
  • 57,621
  • 49
  • 238
  • 373

3 Answers3

7

As John Paul pointed out, the line widths are controlled by the the box.rectangle and box.umbrella components of lattice's graphical parameter list. (For your future reference, typing names(trellis.par.get()) is a fast way to scan the list of graphical attributes controlled by that list.)

Here's a slightly cleaner way to set those options for one or more particular figures:

thickBoxSettings <- list(box.rectangle=list(lwd=2), box.umbrella=list(lwd=2))

bwplot(x ~ type|month, data = df, 
       par.settings = thickBoxSettings,
       panel = function(...) {
           panel.abline(h=0, col="green")
           panel.bwplot(...)
       })

enter image description here

Josh O'Brien
  • 159,210
  • 26
  • 366
  • 455
  • Thanks Josh! OMG, why is lattice so contra-intuitive! I've spent almost two hours with this, just in the worst time pressure before my presentation at the conference! Only pitty that I was offline and couldn't ask here.. – Tomas Sep 17 '13 at 17:51
4

One thing you can do is get the trellis settings for the box, and change those. Try

rect.settings<-trellis.par.get("box.rectangle") #gets all rectangle settings
rect.settings$lwd<-4  #sets width to 4, you can choose what you like
trellis.par.set("box.rectangle",rect.settings)

Put these above your bwplot call and it should do it.

The box rectangle settings also has color, fill etc.

Edit to add if you get box.umbrella you can edit it to change what the lines above and below the box look like.

John Paul
  • 12,196
  • 6
  • 55
  • 75
3

There is a further feature of lattice plots that needs mention. They are really objects, so methods exist for modifying their list representations;

myBW <- bwplot(x ~ type|month, data = df, panel=function(...) {
    panel.abline(h=0, col="green")
    panel.bwplot(...)
})
newBW <- update(myBW, par.settings=list(box.rectangle=list(lwd=4) ))
plot(newBW)   # need to print or plot a grid object

You can also use trellis.focus and apply further updating function to overlay new data or text.

IRTFM
  • 258,963
  • 21
  • 364
  • 487
  • Thanks DWin! OMG, why is lattice so contra-intuitive! I've spent almost two hours with this, just in the worst time pressure before my presentation at the conference! Only pitty that I was offline and couldn't ask here.. – Tomas Sep 17 '13 at 17:51
  • It's a completely different (object-oriented to a degree) plotting paradigm. Designed for greater power, it does require adoption of a more list-oriented mind set. – IRTFM Sep 17 '13 at 17:59
  • 1
    Well, really, but it is so frustrating when the documentation is not very well-aranged and the stuff is not intuitive.. For example, `help.search("box.rectangle")` returns nothing. It would be perfect to have some documentation with just a picture of sample graph and names of all those parameters you need to set... – Tomas Sep 17 '13 at 18:38
  • Yeah. I understand. I thought that buying Sarkar's book was money well spent. It's got good discussion and helpful charts of plotting parameter names. And once I got around to reading the `help('lattice-package', package="lattice")` page, I wondered why I had not done so before. – IRTFM Sep 17 '13 at 23:59