Background
While thinking about ways to represent incoming and outgoing flows in a business process, I thought about using export-import charts like the one shown here in the Learning R blog. However, as the author acknowledges, it is difficult to compare individual values using these charts. Regardless, I still wanted to have this graph for an at-a-glance view before breaking it into facets and comparing individual values.
My Solution
In the Learning R article, the author chooses to show multiple categories of import and export using stacked bars. Instead of representing multiple categories, I decided to use the color intensity on the bars’ fill as visual reinforcement of information the graph already contains. Import and export are represented by red and blue, respectively, and the transparency facilitates the visual comparison the reader must make between bars that are not side by side.
In the below example, I use the same subset of data as in the motivating post. Please refer to the linked article for the data used in this example. Make sure to click on the link that says "Access the subset used in this post in here." rather than going to the Eurostat website. Save the file as "trade.csv" in the working directory. These are monthly trade data for the 27 European Union countries by broad economic categories (BEC) in millions of Euros.
First, load the necessary packages.
For convenient and powerful data manipulation, plyr and reshape provide functions like ddply and melt. A relatively new package, scales is required for scale functions to format the numbers to specific scales within ggplot2.
Next, import the data, calculate the trade balance (export - import), and melt the data for ggplot2.
After the melt step, add another line to aggregate over BEC. This will further simplify the structure.
At this point, the data will look like this:
We step through one layer at a time.
Layer 1: Start with export bars. We will add import data on the bottom of this graph.
Layer 2: Add the import data and attach it back-to-back to the export data. Label the x-axis and the y-axis accordingly.
Layer 4: Finally, change the x-axis to make it easy for viewers to read. The following result is my final product.
labels <- gsub("20([0-9]{2})M([0-9]{2})", "\\2\n\\1",trade.m$Time) last_plot() + scale_x_discrete(labels = labels)
The resulting plot shows the overall export and import trend, with different color intensities to reinforce the size of each bar. This eases the cognitive burden placed on readers when they visually compare export versus import.
While the overall trend shows that there are more exports than imports, the story might be more complicated when there are subcategories. An example is the United States economy: an aggregated USA import-export chart will show significantly larger import bars than exports bars, but when it is broken into different categories, especially in agricultural goods, the graph will show a different story from the overall trend.
In the meantime, this graph provides a quick at-a-glance look at exports and imports before digging deeper into various categories for further analysis.
All highlighted R-code segments were created by Pretty R at inside-R.org. Keep up with ours and other great articles relating to R on R-bloggers.
References
- ggplot2: Back-to-back Bar Charts. Learning R. URL http://learnr.wordpress.com/2009/09/24/ggplot2-back-to-back-bar-charts(accessed July 23, 2012).
- Pretty R Syntax Highlighter. inside-R.org. URL http://www.inside-r.org/pretty-r (accessed July 27, 2012).
- R Development Core Team (2012). R: A language and environment for statistical computing. R Foundation for Statistical Computing, Vienna, Austria. ISBN 3-900051-07-0, URL http://www.R-project.org
- Wickham, H. (2007). Reshaping data with the reshape package. Journal of Statistical Software, 21(12).
- Wickham, H. (2009). ggplot2: elegant graphics for data analysis. Springer New York, USA.
- Wickham, H. (2011). The Split-Apply-Combine Strategy for Data Analysis. Journal of Statistical Software, 40(1), 1-29. URL http://www.jstatsoft.org/v40/i01/
- Wickham, H. (2012). scales: Scale functions for graphics. R package version 0.2.1. URL http://CRAN.R-project.org/package=scales
Great post :) Thanks
ReplyDeleteThanks, your example came just I was trying to figure out how to construct an age-sex pyramid to display age distribution by sex. Build a back-to-back chart, flip the coordinates, and voila, a population pyramid!
ReplyDelete