Graphics Reference
In-Depth Information
Grouping Bars Together
Problem
You want to group bars together by a second variable.
Solution
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, Cul-
tivar and Date , and one continuous variable, Weight :
library(gcookbook) # For the data set
cabbage_exp
Cultivar Date Weight
c39 d16
3.18
c39 d20
2.80
c39 d21
2.74
c52 d16
2.26
c52 d20
3.11
c52 d21
1.47
We'll map Date to the xposition and map Cultivar to the fill color ( Figure 3-4 ):
ggplot(cabbage_exp, aes(x = Date, y = Weight, fill = Cultivar)) +
geom_bar(position = "dodge" )
Search WWH ::




Custom Search