Stacked Bar Plots with GGPlot2

When plotting contribution shares, pie charts seem to be logic. But pie charts look ugly and do not add much more information than presenting the data in a table. But there is an alterative: stacker bar charts. In R you can use the ggplot2 package to plot them easily. Below, you find the very same data plotted using two different layout. Left the stacked bar chart. Right you see the stacked bar chart using polar coordinates.

To reproduce both charts, copy and paste the following code to your R-Shell.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
library(RColorBrewer)
library(ggplot2)

project <- c('ArgoUML','ArgoUML','ArgoUML','ArgoUML','ArgoUML','GWT','GWT','GWT','GWT','GWT','Jaxen','Jaxen','Jaxen','Jaxen','Jaxen','JRuby','JRuby','JRuby','JRuby','JRuby')

component <- c('FileDistance','PackageDistance','DataDependency','CallGraphDistance','ChangeCouplings','FileDistance','PackageDistance','DataDependency','CallGraphDistance','ChangeCouplings','FileDistance','PackageDistance','DataDependency','CallGraphDistance','ChangeCouplings','FileDistance','PackageDistance','DataDependency','CallGraphDistance','ChangeCouplings')

numbers <- c(16,22,46,10,6,46,22,10,12,10,20,20,20,20,20,0,36,33,20,11)

df <- data.frame(project,component,numbers)

#plot the stacked bar plot
ggplot(df, aes(x = project)) + geom_bar(aes(weight=numbers, fill = component), position = 'fill') + scale_y_continuous("", breaks=NA) + scale_fill_manual(values = rev(brewer.pal(6, "Purples")))

#plot the stacked bar plot with polar coordinates
ggplot(df, aes(x = project)) + geom_bar(aes(weight=numbers, fill = component), position = 'fill') + scale_y_continuous("", breaks=NA) + scale_fill_manual(values = rev(brewer.pal(6, "Purples"))) + coord_polar()

More in the coolest plotting framework ever on http://had.co.nz/ggplot2/

Comments on this entry are closed.