[R] how to plot numeric variable against several categories with lattice bwplot?

Deepayan Sarkar deep@y@n@@@rk@r @end|ng |rom gm@||@com
Mon Jan 17 14:48:10 CET 2022


On Mon, Jan 17, 2022 at 7:01 PM PIKAL Petr <petr.pikal using precheza.cz> wrote:
>
> Hi Luigi
>
> Not sure how to do it in lattice but in ggplot you could use for cycle
>
> for(i in 3:5) {
> print(ggplot(df, aes(x=df[,i], y=y))+geom_boxplot()+ xlab(names(df)[i]))
> }
>
> It is better to put plots to pdf file

Pretty much the same with lattice, e.g.,

flist <- sprintf("y ~ %s", setdiff(names(df), "y"))
plist <- lapply(flist, function(f) bwplot(as.formula(f), df))

print(plist)

You could then use latticeExtra::c.trellis() to combine them into a
single plot, but YMMV.

library(latticeExtra)
names(plist) <- flist
do.call(c, plist)

Best,
-Deepayan


>
> Cheers
> Petr
>
> > -----Original Message-----
> > From: R-help <r-help-bounces using r-project.org> On Behalf Of Luigi Marongiu
> > Sent: Monday, January 17, 2022 1:31 PM
> > To: Rolf Turner <r.turner using auckland.ac.nz>
> > Cc: r-help <r-help using r-project.org>
> > Subject: Re: [R] how to plot numeric variable against several categories
> with
> > lattice bwplot?
> >
> > Yes, I would like to have a boxplot of y against each of the other
> variables, each
> > into a separate panel, without having to type the command for each pair (I
> have
> > 23 categories in the real data...).
> > Thank you for the solid line code (I thought it might have been a simple
> > parameter to add...)
> >
> > On Mon, Jan 17, 2022 at 4:22 AM Rolf Turner <r.turner using auckland.ac.nz>
> > wrote:
> > >
> > >
> > > On Thu, 13 Jan 2022 20:38:04 +0100
> > > Luigi Marongiu <marongiu.luigi using gmail.com> wrote:
> > >
> > > > Hello,
> > > > I have a numerical variable (x) and a series of categories. I would
> > > > like to make a box plot of x against each of the categories. How can
> > > > I arrange the data so that I can accomplish it with lattice?
> > > > At the moment I got this:
> > > > ```
> > > > df = data.frame(x = c(rep(1,5), rep(2,5), rep(3,5)), y = rnorm(15),
> > > > f1 = rep(letters[1:5],3),
> > > > f2 = rep(letters[7:9],5),
> > > > f3 = c(rep(LETTERS[10:12],4), LETTERS[10:12])) df$x = factor(df$x)
> > > > df$f1 = factor(df$f1)
> > > > df$f2 = factor(df$f2)
> > > > df$f3 = factor(df$f3)
> > > > library(lattice)
> > > > bwplot(y ~ x, data = df, auto.key = TRUE ) ``` Also, is it possible
> > > > to have the wishers in solid line?
> > > > Thank you
> > >
> > > It is not at all clear to me what your are trying to do.  If you
> > > really want "to make a box plot of x against each of the categories",
> > > you could just do:
> > >
> > > bwplot(x ~ f1, data = df, auto.key = TRUE ) bwplot(x ~ f2, data = df,
> > > auto.key = TRUE ) bwplot(x ~ f3, data = df, auto.key = TRUE )
> > >
> > > successively.  But perhaps you meant "y" rather than "x".  Please
> > > clarify what you wish to accomplish.
> > >
> > > As to displaying the *whiskers* as solid lines ... that is one hell of
> > > a good question!  I had to struggle:
> > >
> > > xxx <- trellis.par.get("box.umbrella") xxx$lty <- 1
> > > trellis.par.set(box.umbrella=xxx)
> > > junk <- rnorm(42)
> > > bwplot(junk)
> > >
> > > That gave me a boxplot "lying on its side"; I wanted one "standing up".
> > > After a great deal more struggle I did
> > >
> > > crud <- factor(rep(1,42),labels="")
> > > bwplot(junk ~ crud,ylab="")
> > >
> > > which gave me what I was after.
> > >
> > > Perhaps others who are more knowledgeable than I could chip in with
> > > better ideas.
> > >
> > > But first Luigi must clarify what he wants to obtain.
> > >
> > > cheers,
> > >
> > > Rolf Turner
> > >
> > > --
> > > Honorary Research Fellow
> > > Department of Statistics
> > > University of Auckland
> > > Phone: +64-9-373-7599 ext. 88276
> > >
> >
> >
> > --
> > 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.
> ______________________________________________
> 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.



More information about the R-help mailing list