[R] R lattice stripplot add median line to data

PIKAL Petr petr@p|k@| @end|ng |rom prechez@@cz
Fri Oct 25 14:34:53 CEST 2019


Hi

See in line

> -----Original Message-----
> From: Luigi Marongiu <marongiu.luigi using gmail.com>
> Sent: Friday, October 25, 2019 1:33 PM
> To: PIKAL Petr <petr.pikal using precheza.cz>
> Cc: Bert Gunter <bgunter.4567 using gmail.com>; r-help <r-help using r-project.org>
> Subject: Re: [R] R lattice stripplot add median line to data
>
> Ops, yes now it works. But again it gives a single value for each class 
> (normal,
> tumour, metastasis).

And you wanted more lines in each panel? You should not use "once" option in 
the function

addLine(h=aggregate(df$Aboundance, list(df$Group), median)$x, once=T)
addLine(h=aggregate(df$Aboundance, list(df$Group), median)$x, col=2)

and you have function code so you are free to modify it according to your 
needs. Its quite flexible approach, you could modify what to add to which 
panel.

Cheers
Petr





>
> On Fri, Oct 25, 2019 at 1:27 PM PIKAL Petr <petr.pikal using precheza.cz> wrote:
> >
> > Hi
> >
> > > This essentially has transformed the stripplot into a boxplot...
> >
> > NO
> >
> > I wrote:
> >
> > ***after*** your lattice plot just do...
> >
> > and I ment issue addLine after the stripplot is plotted.
> >
> > So first
> > > > > > 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
> > > > > > )
> >
> > than
> >
> > addLine(h=aggregate(df$Aboundance, list(df$Group), median)$x, once=T)
> >
> > Cheers
> > Petr
> >
> > > -----Original Message-----
> > > From: Luigi Marongiu <marongiu.luigi using gmail.com>
> > > Sent: Friday, October 25, 2019 11:24 AM
> > > To: PIKAL Petr <petr.pikal using precheza.cz>
> > > Cc: Bert Gunter <bgunter.4567 using gmail.com>; r-help
> > > <r-help using r-project.org>
> > > Subject: Re: [R] R lattice stripplot add median line to data
> > >
> > > This essentially has transformed the stripplot into a boxplot...
> > >
> > > On Fri, Oct 25, 2019 at 11:19 AM PIKAL Petr <petr.pikal using precheza.cz>
> wrote:
> > > >
> > > > Hi
> > > >
> > > > I asked similar question few yeas ago (in different context) and
> > > > besed on answers I made custom function which adds line after the
> > > > lattice graph is plotted.
> > > >
> > > > after your lattice plot just do
> > > >
> > > > addLine(h=aggregate(df$Aboundance, list(df$Group), median)$x,
> > > > once=T)
> > > >
> > > > the function is defined as follows
> > > >
> > > > addLine <- function (a = NULL, b = NULL, v = NULL, h = NULL, ...,
> > > > once = F) {
> > > >     tcL <- trellis.currentLayout()
> > > >     k <- 0
> > > >     for (i in 1:nrow(tcL)) for (j in 1:ncol(tcL)) if (tcL[i,
> > > >         j] > 0) {
> > > >         k <- k + 1
> > > >         trellis.focus("panel", j, i, highlight = FALSE)
> > > >         if (once)
> > > >             panel.abline(a = a[k], b = b[k], v = v[k], h = h[k],
> > > >                 ...)
> > > >         else panel.abline(a = a, b = b, v = v, h = h, ...)
> > > >         trellis.unfocus()
> > > >     }
> > > >   }
> > > >
> > > > Cheers
> > > > Petr
> > > >
> > > >
> > > >
> > > > > -----Original Message-----
> > > > > From: R-help <r-help-bounces using r-project.org> On Behalf Of Bert
> > > > > Gunter
> > > > > Sent: Thursday, October 24, 2019 6:23 PM
> > > > > To: Luigi Marongiu <marongiu.luigi using gmail.com>
> > > > > Cc: r-help <r-help using r-project.org>
> > > > > Subject: Re: [R] 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 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]]
> > > > >
> > > > > ______________________________________________
> > > > > 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.
> > >
> > >
> > >
> > > --
> > > Best regards,
> > > Luigi
>
>
>
> --
> Best regards,
> Luigi


More information about the R-help mailing list