Graphics Reference
In-Depth Information
Figure 3-4. Graph with grouped bars
Discussion
The most basic bar graphs have one categorical variable on the x-axis and one continuous vari-
able on the y-axis. Sometimes you'll want to use another categorical variable to divide up the
data, in addition to the variable on the x-axis. You can produce a grouped bar plot by map-
ping that variable to
fill
, which represents the fill color of the bars. You must also use
posi-
tion="dodge"
, which tells the bars to “dodge” each other horizontally; if you don't, you'll end
up with a stacked bar plot (
Making a Stacked Bar Graph
).
As with variables mapped to the x-axis of a bar graph, variables that are mapped to the fill color
of bars must be categorical rather than continuous variables.
To add a black outline, use
colour="black"
inside
geom_bar()
. To set the colors, you can use
from
RColorBrewer
:
ggplot(cabbage_exp, aes(x
=
Date, y
=
Weight, fill
=
Cultivar))
+
geom_bar(position
=
"dodge"
, colour
=
"black"
)
+
scale_fill_brewer(palette
=
"Pastel1"
)
Other aesthetics, such as
colour
(the color of the outlines of the bars) or
linestyle
, can also
be used for grouping variables, but
fill
is probably what you'll want to use.