Rolf Turner
2022-Jan-17 03:22 UTC
[R] how to plot numeric variable against several categories with lattice bwplot?
On Thu, 13 Jan 2022 20:38:04 +0100 Luigi Marongiu <marongiu.luigi at 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 youIt 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
Richard M. Heiberger
2022-Jan-17 05:25 UTC
[R] [External] how to plot numeric variable against several categories with lattice bwplot?
My guess, continuing from Rolf, is that you should be using conditioning bwplot(~ y | f2, df, layout=c(3,1))> On Jan 16, 2022, at 22:22, Rolf Turner <r.turner at auckland.ac.nz> wrote: > > xxx <- trellis.par.get("box.umbrella") > xxx$lty <- 1 > trellis.par.set(box.umbrella=xxx) > junk <- rnorm(42) > bwplot(junk)
Luigi Marongiu
2022-Jan-17 12:31 UTC
[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 at auckland.ac.nz> wrote:> > > On Thu, 13 Jan 2022 20:38:04 +0100 > Luigi Marongiu <marongiu.luigi at 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