Graphics Reference
In-Depth Information
useful when using a color scale with light colors, as in Figure 5-10 , because the outline sets them
off from the background. In this example, we also set the fill gradient to go from black to white
and make the points larger so that the fill is easier to see:
ggplot(heightweight, aes(x = weightLb, y = heightIn, fill = ageYear)) +
geom_point(shape = 21 , size = 2.5 ) +
scale_fill_gradient(low = "black" , high = "white" )
# Using guide_legend() will result in a discrete legend instead of a colorbar
ggplot(heightweight, aes(x = weightLb, y = heightIn, fill = ageYear)) +
geom_point(shape = 21 , size = 2.5 ) +
scale_fill_gradient(low = "black" , high = "white" , breaks = 12 : 17 ,
guide = guide_legend())
Figure 5-10. Left: outlined points with a continuous variable mapped to fill; right: with a discrete
legend instead of continuous colorbar
When we map a continuous variable to an aesthetic, that doesn't prevent us from mapping a cat-
egorical variable to other aesthetics. In Figure 5-11 , we'll map weightLb to size , and also map
sex to colour . Because there is a fair amount of overplotting, we'll make the points 50% trans-
parent by setting alpha=.5 . We'll also use scale_size_area() to make the area of the points
proportional to the value (see Creating a Balloon Plot ), and change the color palette to one that
is a little more appealing:
ggplot(heightweight, aes(x = ageYear, y = heightIn, size = weightLb, colour = sex)) +
geom_point(alpha = .5 ) +
scale_size_area() + # Make area proportional to numeric value
scale_colour_brewer(palette = "Set1" )
Search WWH ::




Custom Search