Graphics Reference
In-Depth Information
See Also
For more on using different colors, see
Chapter 12
.
For more information about recoding a continuous variable to a categorical one, see
Recoding a
Mapping a Continuous Variable to Color or Size
Problem
You want to represent a third continuous variable using color or size.
Solution
Map the continuous variable to
size
or
colour
. In the
heightweight
data set, there are many
columns, but we'll only use four of them in this example:
library(gcookbook)
# For the data set
# List the four columns we'll use
heightweight[, c(
"sex"
,
"ageYear"
,
"heightIn"
,
"weightLb"
)]
sex ageYear heightIn weightLb
f
11.92
56.3
85.0
f
12.92
62.3
105.0
f
12.75
63.3
108.0
...
m
13.92
62.0
107.5
m
12.58
59.3
87.0
The basic scatter plot in
Making a Basic Scatter Plot
shows the relationship between the con-
tinuous variables
ageYear
and
heightIn
. To represent a third continuous variable,
weightLb
,
we must map it to another aesthetic property. We can map it to
colour
or
size
, as shown in
ggplot(heightweight, aes(x
=
ageYear, y
=
heightIn, colour
=
weightLb))
+
geom_point()
ggplot(heightweight, aes(x
=
ageYear, y
=
heightIn, size
=
weightLb))
+
geom_point()