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)
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)
Now I would like to produce such a plot for each bin of x.
What has to be changed or added?
Not exactly sure what you're looking for. Is this close?
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.If you have to do it this way, it's still clearer using facets:
Note the addition of a
bin
column todfmelt
. This is because usingfactor(round_any(x,0.5))
in thefacet_grid(...)
formula doesn't work.