# ggplot2: A little twist on back-to-back bar charts

**Anything but R-bitrary**, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)

Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

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 3__. Now add the balance trend line, remove the meaningless legend, and format the y-axis with commas.

__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)

*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

**leave a comment**for the author, please follow the link and comment on their blog:

**Anything but R-bitrary**.

R-bloggers.com offers

**daily e-mail updates**about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.

Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.