Skip to content

Instantly share code, notes, and snippets.

@amrit
Last active August 29, 2015 14:25
Show Gist options
  • Save amrit/3cf14a2a95d50c520e9e to your computer and use it in GitHub Desktop.
Save amrit/3cf14a2a95d50c520e9e to your computer and use it in GitHub Desktop.
DeAndre Jordan Medium Post
library(ggplot2)
library(scales)
library(reshape2)
boozer <- read.csv('boozer.csv')
hedo <- read.csv('hedo.csv')
brand <- read.csv('brand.csv')
deandre <- read.csv('deandre.csv')
boozer$Season <- sapply(boozer$Season, as.character)
hedo$Season <- sapply(hedo$Season, as.character)
brand$Season <- sapply(brand$Season, as.character)
deandre$Season <- sapply(deandre$Season, as.character)
boozer <- boozer[-nrow(boozer),]
hedo <- hedo[-nrow(hedo),]
brand <- brand[-nrow(brand),]
deandre <- deandre[-nrow(deandre),]
boozer = boozer[ which(boozer$Season == "2003-04" | boozer$Season == "2004-05"),]
hedo = hedo[ which(hedo$Season == "2008-09" | hedo$Season == "2009-10"),]
brand = brand[ which(brand$Season == "2007-08" | brand$Season == "2008-09"),]
deandre = deandre[ which(deandre$Season == "2014-15"),]
boozer$Name = "Carlos Boozer"
hedo$Name = "Hedo Turkoglu"
brand$Name = "Elton Brand"
deandre$Name = "DeAndre Jordan"
boozer$Season <- c("1", "2")
hedo$Season <- c("1", "2")
brand$Season <- c("1", "2")
boozerFg <- ggplot(data=boozer, aes(x=Season, y=FG.,)) + ggtitle("Carlos Boozer FG%") +
geom_bar(stat="identity", fill="#FF9999") + scale_y_continuous(limit = c(.5, .525), oob = rescale_none)
hedoFg <- ggplot(data=hedo, aes(x=Season, y=FG.)) + ggtitle("Hedo Turkoglu FG%") +
geom_bar(stat="identity", fill="#003399") + scale_y_continuous(limit = c(.4, .425), oob = rescale_none)
brandFg <- ggplot(data=brand, aes(x=Season, y=FG.)) + ggtitle("Elton Brand FG%") +
geom_bar(stat="identity", fill="#00CC99") + scale_y_continuous(limit = c(.435, .46), oob = rescale_none)
boozerFt <- ggplot(data=boozer, aes(x=Season, y=FT.)) + ggtitle("Carlos Boozer FT%") +
geom_bar(stat="identity", fill="#FF9999") + scale_y_continuous(limit = c(.65, .8), oob = rescale_none)
hedoFt <- ggplot(data=hedo, aes(x=Season, y=FT.)) + ggtitle("Hedo Turkoglu FT%") +
geom_bar(stat="identity", fill="#003399") + scale_y_continuous(limit = c(.7, .85), oob = rescale_none)
brandFt <- ggplot(data=brand, aes(x=Season, y=FT.)) + ggtitle("Elton Brand FT%") +
geom_bar(stat="identity", fill="#00CC99") + scale_y_continuous(limit = c(.625, .8), oob = rescale_none)
# Multiple plot function
#
# ggplot objects can be passed in ..., or to plotlist (as a list of ggplot objects)
# - cols: Number of columns in layout
# - layout: A matrix specifying the layout. If present, 'cols' is ignored.
#
# If the layout is something like matrix(c(1,2,3,3), nrow=2, byrow=TRUE),
# then plot 1 will go in the upper left, 2 will go in the upper right, and
# 3 will go all the way across the bottom.
#
multiplot <- function(..., plotlist=NULL, file, cols=1, layout=NULL) {
library(grid)
# Make a list from the ... arguments and plotlist
plots <- c(list(...), plotlist)
numPlots = length(plots)
# If layout is NULL, then use 'cols' to determine layout
if (is.null(layout)) {
# Make the panel
# ncol: Number of columns of plots
# nrow: Number of rows needed, calculated from # of cols
layout <- matrix(seq(1, cols * ceiling(numPlots/cols)),
ncol = cols, nrow = ceiling(numPlots/cols))
}
if (numPlots==1) {
print(plots[[1]])
} else {
# Set up the page
grid.newpage()
pushViewport(viewport(layout = grid.layout(nrow(layout), ncol(layout))))
# Make each plot, in the correct location
for (i in 1:numPlots) {
# Get the i,j matrix positions of the regions that contain this subplot
matchidx <- as.data.frame(which(layout == i, arr.ind = TRUE))
print(plots[[i]], vp = viewport(layout.pos.row = matchidx$row,
layout.pos.col = matchidx$col))
}
}
}
multiplot(boozerFg, brandFg, hedoFg, cols=2)
multiplot(boozerFt, brandFt, hedoFt, cols=2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment