4.5 Changing the Appearance of Points
4.5.2 Solution
In geom_point()
, set the size
, shape
, colour
, and/or fill
outside of aes()
(the result is shown in Figure 4.14):
ggplot(BOD, aes(x = Time, y = demand)) +
geom_line() +
geom_point(size = 4, shape = 22, colour = "darkred", fill = "pink")
#> This is an untitled chart with no subtitle or caption.
#> It has x-axis 'Time' with labels 2, 4 and 6.
#> It has y-axis 'demand' with labels 10.0, 12.5, 15.0, 17.5 and 20.0.
#> It has 2 layers.
#> Layer 1 is a set of 1 line.
#> Line 1 connects 6 points, at (1, 8.3), (2, 10.3), (3, 19), (4, 16), (5, 15.6) and (7, 19.8).
#> Layer 2 is a set of 6 points.
#> The points are at:
#> (1, 8.3),
#> (2, 10.3),
#> (3, 19),
#> (4, 16),
#> (5, 15.6) and
#> (7, 19.8)
#> Layer 2 has size set to 4.
#> Layer 2 has shape set to fillable square.
#> Layer 2 has colour set to deep reddish orange.
#> Layer 2 has fill set to brilliant purplish pink.
4.5.3 Discussion
The default shape for points is a solid circle, the default size is 2, and the default colour is black. The fill color is relevant only for some point shapes (numbered 21–25), which have separate outline and fill colors (see Recipe ?? for a chart of shapes). The fill color is typically NA
, or empty; you can fill it with white to get hollow-looking circles, as shown in Figure 4.15:
ggplot(BOD, aes(x = Time, y = demand)) +
geom_line() +
geom_point(size = 4, shape = 21, fill = "white")
#> This is an untitled chart with no subtitle or caption.
#> It has x-axis 'Time' with labels 2, 4 and 6.
#> It has y-axis 'demand' with labels 10.0, 12.5, 15.0, 17.5 and 20.0.
#> It has 2 layers.
#> Layer 1 is a set of 1 line.
#> Line 1 connects 6 points, at (1, 8.3), (2, 10.3), (3, 19), (4, 16), (5, 15.6) and (7, 19.8).
#> Layer 2 is a set of 6 points.
#> The points are at:
#> (1, 8.3),
#> (2, 10.3),
#> (3, 19),
#> (4, 16),
#> (5, 15.6) and
#> (7, 19.8)
#> Layer 2 has size set to 4.
#> Layer 2 has shape set to fillable circle.
#> Layer 2 has fill set to white.
If the points and lines have different colors, you should specify the points after the lines, so that they are drawn on top. Otherwise, the lines will be drawn on top of the points.
For multiple lines, we saw in Recipe 4.3 how to draw differently colored points for each group by mapping variables to aesthetic properties of points, inside of aes()
. The default colors are not very appealing, so you may want to use a different palette, using scale_colour_brewer()
or scale_colour_manual()
. To set a single constant shape or size for all the points, as in Figure 4.16, specify shape or size outside of aes()
:
library(gcookbook) # Load gcookbook for the tg data set
# Save the position_dodge specification because we'll use it multiple times
<- position_dodge(0.2)
pd
ggplot(tg, aes(x = dose, y = length, fill = supp)) +
geom_line(position = pd) +
geom_point(shape = 21, size = 3, position = pd) +
scale_fill_manual(values = c("black","white"))
#> This is an untitled chart with no subtitle or caption.
#> It has x-axis 'dose' with labels 0.5, 1.0, 1.5 and 2.0.
#> It has y-axis 'length' with labels 10, 15, 20 and 25.
#> There is a legend indicating fill is used to show supp, with 2 levels:
#> OJ shown as black fill and
#> VC shown as white fill.
#> It has 2 layers.
#> Layer 1 is a set of 2 lines.
#> Line 1 connects 3 points, at (0.45, 13.23), (0.95, 22.7) and (1.95, 26.06).
#> Line 2 connects 3 points, at (0.55, 7.98), (1.05, 16.77) and (2.05, 26.14).
#> Layer 2 is a set of 6 points.
#> The points are at:
#> (0.45, 13.23) fill black which maps to supp = OJ,
#> (0.95, 22.7) fill black which maps to supp = OJ,
#> (1.95, 26.06) fill black which maps to supp = OJ,
#> (0.55, 7.98) fill white which maps to supp = VC,
#> (1.05, 16.77) fill white which maps to supp = VC and
#> (2.05, 26.14) fill white which maps to supp = VC
#> Layer 2 has shape set to fillable circle.
#> Layer 2 has size set to 3.