ggplot: arranging boxplots of multiple y-variables for each group of a continuous x

sina picture sina · Jan 27, 2014 · Viewed 46.6k times · Source

I would like to create boxplots of multiple variables for groups of a continuous x-variable. The boxplots should be arranged next to each other for each group of x.

The data looks like this:

require (ggplot2)
require (plyr)
library(reshape2)

set.seed(1234)
x   <- rnorm(100)
y.1 <- rnorm(100)
y.2 <- rnorm(100)
y.3 <- rnorm(100)
y.4 <- rnorm(100)

df <- as.data.frame(cbind(x,y.1,y.2,y.3,y.4))

which I then melted

dfmelt <- melt(df, measure.vars=2:5)    

The facet_wrap as shown in this solution ( Multiple plots by factor in ggplot (facets)) gives me out each variable in an individual plot, but I would like to have the boxplots of each variable next to each other for each bin of x in one diagram.

ggplot(dfmelt, aes(value, x, group = round_any(x, 0.5), fill=variable))+
geom_boxplot() + 
geom_jitter() + 
facet_wrap(~variable)

fig1

This shows the y-variables next to each other but does not bin x.

ggplot(dfmelt) +
geom_boxplot(aes(x=x,y=value,fill=variable))+
facet_grid(~variable)

fig2

Now I would like to produce such a plot for each bin of x.

What has to be changed or added?

Answer

jlhoward picture jlhoward · Jan 27, 2014

Not exactly sure what you're looking for. Is this close?

enter image description here

library(ggplot2)
library(plyr)
ggplot(dfmelt, aes(x=factor(round_any(x,0.5)), y=value,fill=variable))+
  geom_boxplot()+
  facet_grid(.~variable)+
  labs(x="X (binned)")+
  theme(axis.text.x=element_text(angle=-90, vjust=0.4,hjust=1))

EDIT (response to OP's comment)

You can put the Y's next to each other in each bin by just taking out the facet_grid(...) call, but I don't recommend it.

ggplot(dfmelt, aes(x=factor(round_any(x,0.5)), y=value, fill=variable))+
  geom_boxplot()+
  labs(x="X (binned)")+
  theme(axis.text.x=element_text(angle=-90, vjust=0.4,hjust=1))

If you have to do it this way, it's still clearer using facets:

dfmelt$bin <- factor(round_any(dfmelt$x,0.5))
ggplot(dfmelt, aes(x=bin, y=value, fill=variable))+
  geom_boxplot()+
  facet_grid(.~bin, scales="free")+
  labs(x="X (binned)")+
  theme(axis.text.x=element_blank())

Note the addition of a bin column to dfmelt. This is because using factor(round_any(x,0.5)) in the facet_grid(...) formula doesn't work.