Created
March 26, 2015 20:17
-
-
Save EHDEV/c250eb4b860966572899 to your computer and use it in GitHub Desktop.
R code to generate some ggplot2 charts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--- | |
title: "rail_blog" | |
output: html_document | |
--- | |
```{r echo=FALSE} | |
library(ggplot2) | |
library(gridExtra) | |
setwd('~/code/expdataviz/chart_blog/') | |
new_df = read.csv('./pass_injury_train.csv') | |
names(new_df) <- c("year", "country", "passanger.miles.injury") | |
new_df$year = as.factor(new_df$year) | |
``` | |
```{r} | |
cp <- c("#9B6279", | |
"#5E9740", | |
"#A47A32", | |
"#558584", | |
"#777BC3", | |
"#CB5EB3", | |
"#CB495C") | |
cp2 = c('US' = cp[1], | |
'Latvia' = cp[2], | |
'Poland'=cp[3], | |
'Turkey' = cp[4], | |
'Lithuania' = cp[5], | |
'Romania' = cp[6], | |
'Greece' = cp[7]) | |
# png("plots.png") | |
plots <- list() | |
countries <- unique(new_df$country) | |
for (i in 1:(length(countries))){ | |
if (countries[i] != 'US'){ | |
u <- new_df$country == 'US' | new_df$country == countries[i] | |
df <- new_df[u, ] | |
plots[[i]] <- ggplot(data = df, aes(x=year, y=passanger.miles.injury, fill=country)) + | |
geom_area(aes(fill=country, group=country)) + | |
scale_fill_manual(values=cp2) + | |
ggtitle(paste0('US and ', countries[i])) + | |
ylab('Thousands of Passenger Miles per Reported Injury') + | |
theme(legend.position="right", legend.text=element_text(size=6), legend.key.size=unit(.3, 'cm')) + | |
theme(axis.title.y = element_text(size = rel(0.4), angle = 90)) + | |
theme(legend.title=element_blank()) + | |
theme(axis.title.x = element_text(size = rel(0.4))) + | |
theme(axis.text.x = element_text(size=rel(0.6))) + | |
theme(plot.title=element_text(face="bold", size=11)) + | |
theme(axis.text.y = element_text(size=rel(0.6))); | |
} | |
} | |
plots[[1]] = NULL | |
multiplot(plotlist = plots, cols=2) | |
# dev.off() | |
``` | |
```{r} | |
ggplot(data = new_df, aes(x=year, y=passanger.miles.injury, fill=country)) + | |
geom_bar(position=position_dodge(width=rel(.8)), stat = 'identity') + scale_fill_manual(values=cp2) + | |
# theme(panel.background = element_rect(fill = "black")) + | |
# geom_bar(width=rel(.7), position=position_dodge(width=rel(.7)), stat = 'identity') + | |
ylab('Thousands of Passenger Miles per Reported Injury') + | |
theme(legend.position="bottom", legend.text=element_text(size=8), legend.key.size=unit(.5, 'cm')) + | |
theme(axis.title.y = element_text(size = rel(1), angle = 90)) + | |
theme(axis.title.x = element_text(size = rel(1))) + | |
theme(axis.text.x = element_text(size=rel(1))) + | |
theme(axis.text.y = element_text(size=rel(1))) + | |
ggtitle('US and European Train Safety') | |
# dff <- with(new_df, new_df[order(-as.numeric(passanger.miles.injury)), ]) | |
``` | |
Plotting multiple charts in one grid layout | |
```{r} | |
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)) | |
} | |
} | |
} | |
``` |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment