Luigi Marongiu
2022-Jan-13 19:38 UTC
[R] how to plot numeric variable against several categories with lattice bwplot?
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
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