I\'m making a boxplot in which x
and fill
are mapped to different variables, a bit like this:
ggplot(mpg, aes(x=as.factor(cyl), y=cty,
Just use the facet_grid()
function, makes things a lot easier to visualize:
ggplot(mpg, aes(x=as.factor(drv), y=cty, fill=as.factor(drv))) +
geom_boxplot() +
facet_grid(.~cyl)
See how I switch from x=as.factor(cyl)
to x=as.factor(drv)
.
Once you have done this you can always change the way you want the strips to be displayed and remove margins between the panels... it can easily look like your expected display.
By the way, you don't even need to use the as.factor()
before specifying the columns to be used by ggplot()
. this again improve the readability of your code.