Graphics Reference
In-Depth Information
m
13.92
62.0
m
12.58
59.3
We can group points on the variable sex , by mapping sex to one of the aesthetics colour or
shape ( Figure 5-4 ):
ggplot(heightweight, aes(x = ageYear, y = heightIn, colour = sex)) + geom_point()
ggplot(heightweight, aes(x = ageYear, y = heightIn, shape = sex)) + geom_point()
Figure 5-4. Grouping points by a variable mapped to colour (left), and to shape (right)
Discussion
The grouping variable must be categorical—in other words, a factor or character vector. If it is
stored as a vector of numeric values, it should be converted to a factor before it is used as a
grouping variable.
It is possible to map a variable to both shape and colour , or, if you have multiple grouping vari-
ables, to map different variables to them. Here, we'll map sex to shape and colour ( Figure 5-5 ,
left):
ggplot(heightweight, aes(x = ageYear, y = heightIn, shape = sex, colour = sex)) +
geom_point()
The default shapes and colors may not be very appealing. Other shapes can be used with
scale_shape_manual() , and other colors can be used with scale_colour_brewer() or
scale_colour_manual() .
Search WWH ::




Custom Search