Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.
Map a variable to fill, and
use geom_bar(position="dodge").
In this example we’ll use the cabbage_exp data set, which has two
categorical variables, Cultivar and
Date, and one continuous variable,
Weight:
library(gcookbook)# For the data setcabbage_expCultivar Date Weight c39 d163.18c39 d202.80c39 d212.74c52 d162.26c52 d203.11c52 d211.47
We’ll map Date to the
x position and map Cultivar to the fill color (Figure 3-4):
ggplot(cabbage_exp,aes(x=Date,y=Weight,fill=Cultivar))+geom_bar(position="dodge")