Search code examples
rpanellattice

Vertical box-percentile plot with Lattice & panel.bpplot


I am drawing box-percentile plots in R, using the box-percentile panel function from Hmisc (panel.bpplot) with bwplot from lattice.

I have a numeric vector (Length), and would like to show its distribution across the levels of a factor variable (Month).

Here's an example with fake data:

For example,

set.seed(13)
Length<-sample(1:10, 1000, replace=TRUE)
Month<-sample(c("Apr","May","Jul","Aug","Sep","Nov"), 1000, replace=TRUE)

df<-cbind(Month, Length)
df<-as.data.frame(df)   
df$Month<-factor(df$Month, levels=c("Apr","May","Jul","Aug","Sep","Nov"))
df$Length<-as.numeric(df$Length)

#plot horizontal box-percentile plot; 
bwplot(Month~Length, data=df, panel=panel.bpplot)

This works fine. But I want the plots to be vertical, with Month on the x-axis and Length on the y-axis. The documentation for panel.bpplot says that horizontal plots make category levels more visible, but, for my purposes, I specifically need a vertical plot. Is there a way to modify panel.bpplot to do this?


Solution

  • I took some tips from this previous question and then merged it with your code. It seems the "trick" is setting up a rotated grid:

    require(grid)
    grid.newpage()
    pushViewport(viewport(angle = 90, name = "VP"))
    print(
        bwplot(Month~Length, data=df, panel=panel.bpplot, draw.in = "VP"
        ),
        newpage=FALSE
    )
    

    which results in:

    enter image description here