Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Grouped horizontal boxplot with bwplot

Tags:

r

lattice

bwplot

I have the following code that allows grouped vertical box-plots via the bwplot function in lattice. Reproducible example..

data(mpg, package = "ggplot2") 

bwplot( hwy~class, data = mpg, groups = year,
    pch = "|", box.width = 1/3,
    auto.key = list(points = FALSE, rectangles = TRUE, space = "right"),
    panel = panel.superpose,
    panel.groups = function(x, y, ..., group.number) {
        panel.bwplot(x + (group.number-1.5)/3, y, ...)
     })

This works fine, but I would like the boxplots to be horizontal, so I changed the first line, keeping everything else equal:

bwplot( class~hwy, data = mpg, groups = year, ...

But the graph comes out like this Output. I have tried playing around with the code without success. I have 2 questions: Firstly, how can I, or is it possible to have the boxplots not superimposed on each other? And secondly and more generally, how can I set the color panel to grayscale, so the plot come out in shades of gray or just black and white?

like image 395
user2507608 Avatar asked Nov 24 '13 08:11

user2507608


People also ask

What is bwplot?

The function bwplot() makes box-and-whisker plots for numerical variables. It comes from the lattice package for statistical graphics, which is pre-installed with every distribution of R. Also, package tigerstats depends on lattice, so if you load tigerstats : require(tigerstats)

What is a grouped box plot?

Grouped Boxplots are used to visualize the data having multiple subgroups. Also, we can visualize three variables at a time with grouped boxplots where one variable is numerical and the other two are categorical variables. We can visualize the fourth variable by using the color property of ggplot in R.

How do I Group A boxplot in R?

In order to create a box plot by group in R you can pass a formula of the form y ~ x , being x a numerical variable and y a categoriacal variable to the boxplot function. You can pass the variables accessing the data from the data frame using the dollar sign or subsetting the data frame.

How do you make a horizontal boxplot in Ggplot?

Let us change the above boxplot to be a horizontal boxplot. By adding coord_flip() function to the ggplot2 object, we can swap the x and y-axis. Now we can easily read the labels (now on y-axis of the boxplot) on the horizontal boxplot.


2 Answers

  • You should also invert x and y roles in panel.groups function.
  • Use trellis.par.set to change fill color of superposed symbol

enter image description here

data(mpg, package = "ggplot2") 
library(latticeExtra)
mycolors <- grey.colors(5, start = 0.1, end = 0.9)
trellis.par.set(superpose.symbol = list(fill = mycolors,col=mycolors))
bwplot(class~hwy, data = mpg, groups = year,
                pch = "|", box.width = 1/3,
                panel = panel.superpose,
                panel.groups = function(x, y,..., group.number) 
                    panel.bwplot(x,y + (group.number-1.5)/3,...)
            ) 

But it is probably easier here to use the ggplot2 solution.

like image 112
agstudy Avatar answered Sep 21 '22 16:09

agstudy


If it's not critical that you use bwplot, you may try ggplot:

ggplot(data = mpg, aes(x = class, y = hwy, fill = factor(year))) +
  geom_boxplot() +
  coord_flip() +
  scale_fill_grey(start = 0.5, end = 0.8) +
  theme_classic()

enter image description here

like image 21
Henrik Avatar answered Sep 23 '22 16:09

Henrik