[R] R lattice stripplot add median line to data
Bert Gunter
bgunter@4567 @end|ng |rom gm@||@com
Thu Oct 24 18:22:38 CEST 2019
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 using gmail.com>
wrote:
> Dear all,
> I am plotting data with lattice's stripplot. Is it possible to add a
> median line to each cluster?
> This is the working example:
>
> ```
> Sample = c("A0", "A0", "A0", "A3", "A3", "A3", "A7", "A7", "A7",
> "A9", "A9", "A9", "H1", "H1",
> "H1", "H2", "H2", "H2", "H3", "H3", "H3", "P1", "P1", "P1",
> "P2", "P2", "P2", "P3",
> "P3", "P3", "P4", "P4", "P4", "P5", "P5", "P5", "P7", "P7",
> "P7", "A0", "A0", "A0",
> "A3", "A3", "A3", "A7", "A7", "A7", "A9", "A9", "A9", "H1",
> "H1", "H1", "H2", "H2",
> "H2", "H3", "H3", "H3", "P1", "P1", "P1", "P2", "P2", "P2",
> "P3", "P3", "P3", "P4",
> "P4", "P4", "P5", "P5", "P5", "P7", "P7", "P7")
> Group = rep(c("Normal", "Tumour", "Metastasis" ), 26)
> Taxon = c(rep("Microviridae", 39), rep("Caudovirales", 39))
> Aboundance = c(0, 151, 3, 0, 102, 509, 4, 1, 277, 4, 87, 7,
> 16, 13, 22, 47, 12, 1,
> 5, 251, 4, 8, 4, 2, 14, 4, 2, 10, 4, 4,
> 13, 1, 1, 5, 7, 2, 6, 6, 4, 1, 2, 1,
> 2, 1, 2, 0, 0, 2, 0, 0, 1, 0, 0, 0,
> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)
> df = data.frame(Sample, Group, Taxon, Aboundance,
> stringsAsFactors = FALSE)
> library(lattice)
> 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()])
> },
> # add median bar
> )
> ```
>
> Thank you
> --
> Best regards,
> Luigi
>
> ______________________________________________
> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>
[[alternative HTML version deleted]]
More information about the R-help
mailing list