4.7 Making a Stacked Area Graph
4.7.2 Solution
Use geom_area()
andmap a factor to fill (Figure 4.20):
library(gcookbook) # Load gcookbook for the uspopage data set
ggplot(uspopage, aes(x = Year, y = Thousands, fill = AgeGroup)) +
geom_area()
#> This is an untitled chart with no subtitle or caption.
#> It has x-axis 'Year' with labels 1900, 1925, 1950, 1975 and 2000.
#> It has y-axis 'Thousands' with labels 0e+00, 1e+05, 2e+05 and 3e+05.
#> There is a legend indicating fill is used to show AgeGroup, with 8 levels:
#> <5 shown as strong reddish orange fill,
#> 5-14 shown as deep orange yellow fill,
#> 15-24 shown as vivid yellow green fill,
#> 25-34 shown as vivid yellowish green fill,
#> 35-44 shown as brilliant bluish green fill,
#> 45-54 shown as brilliant blue fill,
#> 55-64 shown as vivid violet fill and
#> >64 shown as deep purplish pink fill.
#> The chart is an area graph that VI can not process.
#> These are stacked, as sorted by AgeGroup.
4.7.3 Discussion
The sort of data that is plotted with a stacked area chart is often provided in a wide format, but ggplot requires data to be in long format. To convert it, see Recipe 7.6.
In the example here, we used the uspopage
data set:
uspopage#> Year AgeGroup Thousands
#> 1 1900 <5 9181
#> 2 1900 5-14 16966
#> 3 1900 15-24 14951
#> ...<818 more rows>...
#> 822 2002 45-54 40084
#> 823 2002 55-64 26602
#> 824 2002 >64 35602
This version of the chart (Figure 4.21) changes the palette to a range of blues and adds thin (size = .2
) lines between each area. It also makes the filled areas semitransparent (alpha = .4
), so that it is possible to see the grid lines through them:
ggplot(uspopage, aes(x = Year, y = Thousands, fill = AgeGroup)) +
geom_area(colour = "black", size = .2, alpha = .4) +
scale_fill_brewer(palette = "Blues")
#> This is an untitled chart with no subtitle or caption.
#> It has x-axis 'Year' with labels 1900, 1925, 1950, 1975 and 2000.
#> It has y-axis 'Thousands' with labels 0e+00, 1e+05, 2e+05 and 3e+05.
#> There is a legend indicating fill is used to show AgeGroup, with 8 levels:
#> <5 shown as white fill,
#> 5-14 shown as bluish white fill,
#> 15-24 shown as very pale blue fill,
#> 25-34 shown as very light greenish blue fill,
#> 35-44 shown as light blue fill,
#> 45-54 shown as brilliant blue fill,
#> 55-64 shown as strong blue fill and
#> >64 shown as strong purplish blue fill.
#> The chart is an area graph that VI can not process.
#> These are stacked, as sorted by AgeGroup.
#> It has colour set to black.
#> It has size set to 0.2.
#> It has alpha set to 0.4.
Since each filled area is drawn with a polygon, the outline includes the left and right sides. This might be distracting or misleading. To get rid of it (Figure 4.22), first draw the stacked areas without an outline (by leaving colour
as the default NA
value), and then add a geom_line()
on top:
ggplot(uspopage, aes(x = Year, y = Thousands, fill = AgeGroup, order = dplyr::desc(AgeGroup))) +
geom_area(colour = NA, alpha = .4) +
scale_fill_brewer(palette = "Blues") +
geom_line(position = "stack", size = .2)
#> This is an untitled chart with no subtitle or caption.
#> It has x-axis 'Year' with labels 1900, 1925, 1950, 1975 and 2000.
#> It has y-axis 'Thousands' with labels 0e+00, 1e+05, 2e+05 and 3e+05.
#> There is a legend indicating fill is used to show AgeGroup, with 8 levels:
#> <5 shown as white fill,
#> 5-14 shown as bluish white fill,
#> 15-24 shown as very pale blue fill,
#> 25-34 shown as very light greenish blue fill,
#> 35-44 shown as light blue fill,
#> 45-54 shown as brilliant blue fill,
#> 55-64 shown as strong blue fill and
#> >64 shown as strong purplish blue fill.
#> It has 2 layers.
#> Layer 1 is an area graph that VI can not process.
#> These are stacked, as sorted by AgeGroup.
#> Layer 1 has colour set to white.
#> Layer 1 has alpha set to 0.4.
#> Layer 2 is a set of 8 lines.
#> Line 1 connects 103 points.
#> Line 2 connects 103 points.
#> Line 3 connects 103 points.
#> Line 4 connects 103 points.
#> Line 5 connects 103 points.
#> Line 6 connects 103 points.
#> Line 7 connects 103 points.
#> Line 8 connects 103 points.
#> These are stacked, as sorted by AgeGroup.
#> Layer 2 has size set to 0.2.