Skip to content
Prev 381486 / 398502 Next

R lattice stripplot add median line to data

Yes, it's possible to do about anything in lattice, but you have to learn
how to write custom panel functions, which takes some effort. If you want
to use lattice in this way, you should probably go through Deepayan's book.

Here is one way to do what I think you want. Note that it depends on
knowing that when the x axis is a factor, the x positions of the y
variables are at 1, 2, 3, .. etc (to the number of levels of the factor) to
draw the horizontal line segments. This is documented somewhere, but I
don't remember where.

stripplot(
   Aboundance ~ Taxon|Group,
   df,
   groups = Taxon,
   scales=list(y=list(log=T)),
   pch=16,  cex = 1.5,
   ylab = expression(bold("Number of taxons")),
   jitter.data = TRUE,
   layout=c(3,1),
   col = "black",
   # colour panels differently
   par.settings=list(strip.background=list(col=c("darkorchid3",
                                                 "darkolivegreen3",
"brown3"))),
   strip = function(...,bg) {
      strip.default(...,
                    bg =
trellis.par.get("strip.background")$col[which.packet()])
      },
   panel = function(x,y,...){
      panel.stripplot(x,y,...)
      lev <- seq_along(levels(x))
      meds <- tapply(y,x,median,na.rm = TRUE)
      for(i in lev)panel.segments(x0 = i-.25, y0 = meds[i], x1 = i+.25, y1
= meds[i],
                  lwd=2,col = "red")
   }
)

Cheers,
Bert

Bert Gunter

"The trouble with having an open mind is that people keep coming along and
sticking things into it."
-- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )


On Thu, Oct 24, 2019 at 2:22 AM Luigi Marongiu <marongiu.luigi at gmail.com>
wrote: