Graphics Reference
In-Depth Information
ggplot(PlantGrowth, aes(x
=
group, y
=
weight, fill
=
group))
+
geom_boxplot()
+
scale_fill_manual(values
=
c(
"grey85"
,
"grey85"
,
"#FFDDCC"
), guide
=
FALSE
FALSE
)
See Also
See
Chapter 12
for more information about specifying colors.
For more information about removing the legend, see
Removing the Legend
.
Adding Error Bars
Problem
You want to add error bars to a graph.
Solution
Use
geom_errorbar
and map variables to the values for
ymin
and
ymax
. Adding the error bars
is done the same way for bar graphs and line graphs, as shown in
Figure 7-14
(notice that default
yrange is different for bars and lines, though):
library(gcookbook)
# For the data set
# Take a subset of the cabbage_exp data for this example
ce
<-
subset(cabbage_exp, Cultivar
==
"c39"
)
# With a bar graph
ggplot(ce, aes(x
=
Date, y
=
Weight))
+
geom_bar(fill
=
"white"
, colour
=
"black"
)
+
geom_errorbar(aes(ymin
=
Weight
-
se, ymax
=
Weight
+
se), width
=
.2
)
# With a line graph
ggplot(ce, aes(x
=
Date, y
=
Weight))
+
geom_line(aes(group
=
1
))
+
geom_point(size
=
4
)
+
geom_errorbar(aes(ymin
=
Weight
-
se, ymax
=
Weight
+
se), width
=
.2
)