How to change panel labels and x-axis sublabels in a lattice bwplot

Alex Mayer picture Alex Mayer · Feb 5, 2013 · Viewed 11.4k times · Source

I'm 2 weeks into using RStudio (MacOS) so please forgive me if I'm overlooking an obvious function that would solve my problem.
As a project, I am attempting to reproduce a box plot graph with 4 plots representing Net Benefit, given disease type -- "non-severe"(0) or "severe"(1) -- as an x-axis label, and given treatment -- "talk therapy"(0) or "drug therapy"(1) -- as an x-axis sub-label.

Here is my script so far:

tx <- c(0,0,0,0,1,1,1,1)
dztype <- c(1,0,1,0,0,0,1,1)
NBwtp1000 <- c(-5500,-4000,-5000,-1000,-5000,-5000,-2800,-2000)
require(lattice)
bwplot(NBwtp1000 ~ paste0("Tx ", tx) | paste0("Disease Severity ", dztype),
       xlab="Talk Therapy (Tx 0) or Drug Therapy (Tx 1)", 
       ylab="Net Benefit @ wtp 1000", horizontal=FALSE)

If you run the code, you'll see the box-and-whisker plot: I'm almost there thanks to some informative posts on this forum about lattice's bwplot function.

However, I am still far from happy with the result. I used the paste0 function to add string descriptors to the X-axis sublabels for treatment group (originally labelled "1,2" and now showing as "Tx 0, Tx 1"), but I would ideally like those sublabels to say "Talk Therapy" and "Drug Therapy", respectively. (I simply didn't know how to do away with the existing numeric labels.)
Similarly, I would like the panel labels to say "Not Severe" where the label is currently 0 and "Severe" where the label is currently 1.

Answer

plannapus picture plannapus · Feb 5, 2013

The simplest way as far as I can see is to convert your tx and dztype to factors with the appropriate level names.

tx <- factor(tx, levels=c(0,1), labels=c("Talk Therapy", "Drug Therapy"))
dztype <- factor(dztype, levels=c(0,1), labels=c("Not severe", "Severe"))
bwplot(NBwtp1000 ~ tx | dztype, xlab="Talk Therapy or Drug Therapy", 
       ylab="Net Benefit @ wtp 1000", horizontal=FALSE)

enter image description here

Another solution is to use argument scale for the tx levels and argument strip for dztype:

tx <- c(0,0,0,0,1,1,1,1)
dztype <- c(1,0,1,0,0,0,1,1)
bwplot(NBwtp1000 ~ tx | dztype, xlab="Talk Therapy or Drug Therapy", 
       ylab="Net Benefit @ wtp 1000", horizontal=FALSE,
       scales=list(x=list(labels=c("Talk therapy","Drug Therapy"))), 
       strip=strip.custom(var.name="Disease severity", 
                          factor.levels=c(" Not Severe", "Severe"),
                          strip.levels=rep(TRUE,2)))

enter image description here