How to easily make stacked area chart with ezplot - Part 9
Stacked area chart isn’t something I use often. However, I see Economists use it a lot. For example, if you open the book Capital in the Twenty-First Century, you’ll find many stacked area charts. By the way, it’s a thought-provoking book. A stacked area chart is the continuous analog of a stacked bar chart. We can use it to show how different components of the whole vary over time. The reason I don’t like it is because as you move up the stacks, it becomes increasingly hard to see the patterns. Therefore, it’s very important
to choose the order in which the different components are stacked. Nonetheless, I added a function mk_areaplot() to the ezplot package. This function can save you a lot of time in case you ever need to make a stacked area chart. Let’s see how to use it.
On Linux, install the R development package, usually called r-devel or r-base-dev.
Install devtools by running install.packages("devtools") in R.
Install and Load ezplot
The ezplot package includes a dataset of annual online advertising revenues between 2000 and 2014, broken down by 13 different revenue channels. We’ll make a stacked area chart to show how each type of revenue progressed from 2000 to 2014.
First, we need to change the data to long format.
Next, we can easily draw a stacked area chart using mk_areaplot().
We can also visualize the percentage of each revenue channel over the years.
First, we calculate the percentages of the revenue channels for each year.
Next, we run the same code, replacing “rev” by “pct”.
I created ezplot because there are too many detailed commands to remember when making and customizing a ggplot. I hope ezplot can improve your productivity. If you enjoyed reading this post, please share it with your friends. I’m writing a book called ezplot: How to Easily Make ggplot2 Graphics for Data Analysis, and it is 20% complete. Read the sample chapters for FREE and get notified when the book is published.