Graphics Reference
In-Depth Information
Making a Line Graph with Multiple Lines
Problem
You want to make a line graph with more than one line.
Solution
In addition to the variables mapped to the x- and y-axes, map another (discrete) variable to
col-
# Load plyr so we can use ddply() to create the example data set
library(plyr)
# Summarize the ToothGrowth data
tg
<-
ddply(ToothGrowth, c(
"supp"
,
"dose"
), summarise, length
=
mean(len))
# Map supp to colour
ggplot(tg, aes(x
=
dose, y
=
length, colour
=
supp))
+
geom_line()
# Map supp to linetype
ggplot(tg, aes(x
=
dose, y
=
length, linetype
=
supp))
+
geom_line()
Figure 4-6. Left: a variable mapped to colour; right: a variable mapped to linetype
Discussion
The
tg
data has three columns, including the factor
supp
, which we mapped to
colour
and
linetype
: