Graphics Reference
In-Depth Information
When a discrete variable is mappedto an aesthetic like
colour
or
fill
(as in the case of the
bars), that variable is used for grouping the data. But by settingthe
colour
of the error bars, we
made it so that the variable for
colour
was not used for grouping, and we needed some other
way to inform
ggplot()
that the two data entries at each xwere in different groups so that they
would be dodged.
See Also
See
Grouping Bars Together
for more about creating grouped bar graphs, and
Making a Line
Graph with Multiple Lines
for more about creating line graphs with multiple lines.
See
Summarizing Data with Standard Errors and Conidence Intervals
for calculating summaries
with means, standard deviations, standard errors, and confidence intervals.
See
Adding a Conidence Region
for adding a confidence region when the data has a higher
density along the x-axis.
Adding Annotations to Individual Facets
Problem
You want to add annotations to each facet in a plot.
Solution
Create a new data frame with the faceting variable(s), and a value to use in each facet. Then use
geom_text()
with the new data frame (
Figure 7-17
):
# The base plot
p
<-
ggplot(mpg, aes(x
=
displ, y
=
hwy))
+
geom_point()
+
facet_grid(
.
~ drv)
# A data frame with labels for each facet
f_labels
<-
data.frame(drv
=
c(
"4"
,
"f"
,
"r"
), label
=
c(
"4wd"
,
"Front"
,
"Rear"
))
p
+
geom_text(x
=
6
, y
=
40
, aes(label
=
label), data
=
f_labels)
# If you use annotate(), the label will appear in all facets
p
+
annotate(
"text"
, x
=
6
, y
=
42
, label
=
"label text"
)