0
##Example data to illustrate problem:
type=c("grp1","grp2","grp1","grp3","grp3","grp3","grp4")
num=c(1,1,2,4,3,5,1)
cols=c(rep("red",5),"green","red")

library(lattice)
bwplot(num~type)
par(new=T)
stripplot(num~type,col=cols)

I like the additional information displayed by the box plot but I need the information conveyed by the coloured points in the strip chart. Obviously par(new=T) doesn't work, so how can I overlay the points onto the box plot?

Jessica B
  • 321
  • 1
  • 4
  • 15

2 Answers2

2

You can define a panel function like this :

library(lattice)
bwplot(num~type,panel=function(x,y,...){
    panel.bwplot(x,y,...)
    panel.stripplot(x,y,col=cols,...)
})

enter image description here

agstudy
  • 119,832
  • 17
  • 199
  • 261
  • a good answer, though points plotted as "outliers" by `bwplot` would then be plotted again by the `stripplot`. if using `jitter.data=TRUE` for the stripplot, it would appear that there were twice the outliers. – mac Oct 02 '14 at 19:13
0

the answer by agstudy is quite good, though as I pointed out in a comment, any outliers plotted by bwplot will also be plotted by stripplot, which could get confusing.

Note this is only an issue if you were using jitter.data=TRUE as an option to stripplot, otherwise the duplicate points would plot directly over top of one another and you'd never know they were there. If the data had outliers, and you used jitter.data=TRUE, you'd see twice as many outliers as you should.

the following approach suppresses the outliers from bwplot to avoid this issue:

bwstrip <- function(x,y,...){
  panel.stripplot(x,y,col=cols,do.out=FALSE,jitter.data=TRUE,...)
  panel.bwplot(x,y,...)
}
bwplot(lobe.depths,panel=bwstrip)
mac
  • 3,137
  • 1
  • 28
  • 42