Bar Charts

Graph 1: Simple Bar Chart

This is a replication of a graph Seth used in a Washington Post article.

Load up our libraries...

library(ggplot2)
library(grid)
library(nyloncalc)
library(extrafont)

The data is in aldridge.csv, so first load it up and take a look at how it’s formatted:

Again, one row equals one data point or in this case, one bar. You can also see that I have a grouping variable for the bars here, the "With Aldridge" and "Without Aldridge" column. Always make the labels in your grouping column exactly what you want to see in the key (you can change it in ggplot but it's much easier to just have it ready to go). First to set up the plot region:

attach(data)
dev.new(width=8,height=6)

The geom for a bar chart is geom_bar. We are also going to specify in the aesthetic that bars should be filled with different colors depending on the value of the Condition column. The x-axis value in this case is the Player's name. You can experiment with swapping Condition and Player in this statement, in which case you will get Conditions on the x-axis, with bar colors according to player names.

plot <- ggplot(data,aes(x=Player,y=TS.,fill=Condition))
plot <- plot + geom_bar(stat="identity",alpha=.8, position="dodge")

We have to pass a couple of important arguments to geom_bar() too. First, stat="identity" just tells ggplot that rather than giving it all the data, as we did in the histogram case, and asking it to figure out what the bars should look like, we are going to give it summary data (the average fg% for each player) and we want it to just plot those numbers. Second, position="dodge" tells ggplot that we want to have side-by-side grouped bars. If you were not grouping on Condition, you could leave this part out.

Now I'll apply the NC theme and label my axes. I went ahead and looked at the plot and based on that, I'm also going to change the limits of my graph a little bit.

plot <- plot + theme_nyloncalc()
plot <- plot + ggtitle("Aldridge's Effect on Teammates")
plot <- plot + ylab("True Shooting %")
plot <- plot + scale_y_continuous(breaks=seq(0,60,12),limits=c(0,62))

You might ask why I bothered to set the y-axis lower limit to 0. By default, ggplot will actually set it to a small negative value, because ggplot likes to have a little overhang on either end of the graph. I think it looks a little nicer with 0 set, but you can try deleting the limits argument and comparing for yourself.

Finally for my badge code, which I am going to modify slightly to reflect the y-axis limits I set above:

x_min=ggplot_build(plot)$panel$ranges[[1]]$x.range[1]
x_max=ggplot_build(plot)$panel$ranges[[1]]$x.range[2]
y_min=0
y_max=62

domain=x_max-x_min
range=y_max-y_min

plot<-plot+annotate("rect",xmin=x_max-.32*(domain),xmax=x_max,ymin=y_min,ymax=y_min+(range*.07),alpha=.8)
plot<-plot+annotate("text",x=((x_max-.32*(domain))+x_max)/2,y=((y_min+(range*.08))+y_min)/2,label="Nylon Calculus",colour="#ffffff",family="Chalk Line Outline",size=4.3,vjust=.5,hjust=.5)

Should look pretty good if you plot it now. I'm ready to save this output:

ggsave(filename="/Users/austinc/Desktop/aldridgeeffect.png")