**Anything but R-bitrary**, and kindly contributed to R-bloggers)

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 on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...