Forbes Graph Makeover Challenge ggplot2 style

I recently came across the Forbes Graph Makeover Challenge (http://www.forbes.com/sites/naomirobbins/2012/11/27/graph-makeover-contest/), and decided to give it a shot.  My goal was to create an entry using nothing but R code (that is, no post image processing at all), which to be totally honest, due to my lack of any experience at all with post image processing software/applications is the only way I could ever go about something like this.  The great thing about this challenge is that it really pushed me to get out of my ggplot2 shell and explore facets of ggplot2 that I never did have a handle on.

I haven’t yet had any time to annotate and clean up the code; however, to make things easier to digest–if you are so inclined to work your way through it–I cut the code into layers that build successively on p so you can see what happens as you go along.  Anyhow, here is the code (quick note–something happens when wordpress converts my R-code to HTML and I need to go back in and escape all of my “<-" in the code… for now just realize that until I fix them, these are getting converted to $lt; in "HTML-ese"):


forbes1 <- structure(list(TrafficSource = structure(c(3L, 1L, 2L, 3L, 1L, 2L), .Label = c("Twitter", "LinkedIn", "Facebook"), class = "factor"), Type = structure(c(1L, 1L, 1L, 2L, 2L, 2L), .Label = c("B2B", "B2C"), class = "factor"), Value = c(72L, 12L,16L, -84L, -15L, -1L)), .Names = c("TrafficSource", "Type", "Value"), row.names = c(NA, -6L), class = "data.frame")

cbPalette <- c("dodgerblue4", "gray35")

p <- ggplot(forbes1, aes(x= TrafficSource, y=Value, fill= Type, width = 0.7)) + geom_bar(color = "black", stat="identity", data=subset(forbes1, Type == "B2B")) + geom_bar(color = "black", stat="identity", data=subset(forbes1, Type == "B2C")) + scale_fill_manual(values = cbPalette)+ guides(fill=FALSE)

p <- p + ggtitle("Social Traffic Sources for\n B2B and B2C Companies") + theme(plot.title = element_text(lineheight=.8, face="bold", size=18), axis.text.x = element_text(vjust=0.2, size=18), axis.text.y = element_text(vjust=0.1, size=13), axis.title.y = element_text(face="bold", size=18))+ scale_x_discrete(name="")

abs_format <- function(){
function(x){
abs(x)
}
}
p <- p + scale_y_continuous(breaks=seq(-100, 100, 25), labels = abs_format(), limits=c(-100, 100), name= "% of Referral Traffic") + geom_hline(aes(yintercept= 0))

p <- p + geom_text(aes(TrafficSource,Value,Type,label = texthere), data.frame(TrafficSource="Twitter", Type="B2B" ,Value=95, texthere="B2B"), size = 12, fontface=2, color="dodgerblue4") + geom_text(aes(TrafficSource,Value,Type,label = texthere), data.frame(TrafficSource="Twitter", Type="B2B" ,Value=-93, texthere="B2C"), size = 12, fontface=2, color= "gray35")

p<- p + geom_text(aes(TrafficSource,Value,Type,label = texthere), data.frame(TrafficSource="Twitter", Type="B2B" ,Value=30, texthere="12%") , size = 8, fontface=2) + geom_text(aes(TrafficSource,Value,Type,label = texthere), data.frame(TrafficSource="Twitter", Type="B2B" ,Value=-30, texthere="15%"), size = 8, fontface=2)

p <- p + geom_text(aes(TrafficSource,Value,Type,label = texthere), data.frame(TrafficSource="LinkedIn", Type="B2B" ,Value=30, texthere="16%"), size = 8, fontface=2) + geom_text(aes(TrafficSource,Value,Type,label = texthere), data.frame(TrafficSource="LinkedIn", Type="B2B" ,Value=-30, texthere="1%"), size = 8, fontface=2)

p <- p + geom_text(aes(TrafficSource,Value,Type,label = texthere), data.frame(TrafficSource="Facebook", Type="B2B" ,Value=30, texthere="72%"), size = 8, fontface=2) + geom_text(aes(TrafficSource,Value,Type,label = texthere), data.frame(TrafficSource="Facebook", Type="B2B" ,Value=-30, texthere="84%"), size = 8, fontface=2)

p

Here is the Example Image:

ggplot2 output for Forbes Graph Makeover Challenge