Using R ggplot2 Package for Elegant Graphics

I have been analysing results from a user study I conducted a couple of weeks back, and quite naturally, I have been experimenting with varying ways of presenting the results graphically. I have primarily been using R base graphics for constructing my plots, but recently came across ggplot2… incidentally though, I also briefly experimented with lattice.

I should mention here that R fits into my workflow on the post-processing side of statistical analysis; I basically do my pre-processing and computations using LibreOffice calc (I invested a lot of time mastering spreadsheet functions and letting go is clearly a problem here…) and then generate summaries of results that I later graph using R.
How to Use ggplot2

Installing the ggplot2 package is relatively easy right within the R console; a point to note, however, is that there is another package –reshap2– that you might want to install if you work with data in wide format. As it turns out, most of my data coming through from LibreOffice calc is wide and so reshap2’s melt() function comes in handy.

Installing ggplot2 and reshape2

> install.packages("ggplot2")
Installing package(s) into ‘/usr/local/lib/R/site-library’
> install.packages("reshape2")
Installing package(s) into ‘/usr/local/lib/R/site-library’

Load Libraries

> library(reshape2)
> library(ggplot2)

Sample Dataset Used

> dataset <- read.table('dataset.dat', header=TRUE, sep='\t')
> dataset
 Storage Ranking.1 Ranking.2 Ranking.3
1   Cloud   8   5   13
2   Database   12   10   4
3   File   6   11   9

Convert Data Frame to Long format

> storage <- melt(dataset, id.vars="Storage")
> storage
 Storage variable value
1   Cloud   Ranking.1   8
2   Database   Ranking.1   12
3   File   Ranking.1   6
4   Cloud   Ranking.2   5
5   Database   Ranking.2   10
6   File   Ranking.2   11
7   Cloud   Ranking.3   13
8   Database   Ranking.3   4
9   File   Ranking.3   9

Using qplot() Function for Simple Plots

> qplot(x=Storage, y=value, ylim=c(0, 15), data = storage, color = Storage, geom = "bar", facets = ~ variable, main = "A ggplot2 Stacked BarChart")
R ggplot qplot() Function Output

R ggplot qplot() Function Output

Using ggplot() Function for Flexibility

ggplot(storage, aes(x=Storage, y=value, fill=Storage)) + geom_bar() + facet_grid(~ variable) + theme_bw()
R ggplot ggplot() Function Output

R ggplot ggplot() Function Output



Categories: Research
Tags: , , ,