Pablo Boswell
Pablo Boswell

Reputation: 845

R Lattice: Stacked/Superposed bwplot with groups

I am so close to producing what I want.

vcDeciles <- ceiling(runif(100,1,10))
vcY <- runif(100,0,10)
vcPlotGroups <- sample(c("Pre","Post","Both"),100,1)
vcPlotColors <- 1:length(levels(as.factor(vcPlotGroups)))

print(bwplot(vcY~vcDeciles,ylim=c(min(vcY),max(vcY)),horizontal=F,main="test"
                                       ,groups=vcPlotGroups
                                       ,col=vcPlotColors
                                       ,panel=function(x,y,...) {panel.superpose(x,y,...,panel.groups=function(x,y,col,...) {
                                         panel.bwplot(x,y,col=col,...)
                                         panel.abline(lm(y~x),col.line=col)})}
                                       ,key=list(space="top",text=list(levels(as.factor(vcPlotGroups)),lines=list(col=vcPlotColors,lwd=6)))))

The issues:

1) The colors aren't showing in the legend. Based on other plots that are working, ("Both" = black; "Post" = red, "Pre" = green)

2) The fill color of the "Post" group should not be light blue, but black. The abline is using the right color.

Upvotes: 1

Views: 193

Answers (1)

MrFlick
MrFlick

Reputation: 206546

For your key problem I think you just had a parenthesis matching problem. Then I think the easiest way to change the color of your box plots is just to pass the fill= to the panel function as well. For example

bwplot(vcY~vcDeciles,ylim=c(min(vcY),max(vcY)),
  horizontal=F,
  main="test", 
  groups=vcPlotGroups,
  col=vcPlotColors, 
  panel=function(x,y,...) {
    panel.superpose(x,y,...,panel.groups=function(x,y,col,fill,...) {
               panel.bwplot(x,y,col=col,fill=col, ...)
               panel.abline(lm(y~x),col.line=col)}
    )
  },
  key=list(space="top",
            text=list(levels(as.factor(vcPlotGroups))),
            lines=list(col=vcPlotColors,lwd=6)),
  par.settings = list(superpose.polygon=list(color="black"))
)

will return

enter image description here

Upvotes: 1

Related Questions