Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.
Use geom_area() and
map a factor to fill
(Figure 4-20):
library(gcookbook)# For the data setggplot(uspopage,aes(x=Year,y=Thousands,fill=AgeGroup))+geom_area()
The sort of data that is plotted with a stacked area chart is
often provided in a wide format, but ggplot2() requires data to be in long format. To convert it, see
Converting Data from Wide to Long.
In the example here, we used the uspopage data set:
uspopage
Year AgeGroup Thousands
1900 <5 9181
1900 5-14 16966
1900 15-24 14951
1900 25-34 12161
1900 35-44 9273
1900 45-54 6437
1900 55-64 4026
1900 >64 3099
1901 <5 9336
1901 5-14 17158
...