Skip to content

Instantly share code, notes, and snippets.

@briatte
Created May 16, 2013 18:43
Show Gist options
  • Save briatte/5594039 to your computer and use it in GitHub Desktop.
Save briatte/5594039 to your computer and use it in GitHub Desktop.
quick replication of an Excel graph that was recoded in SPSS in 21 lines (excluding data preparation)
require(ggplot2)
require(plyr)
require(xlsx)
# http://www.calculatedriskblog.com/2013/05/april-employment-report-165000-jobs-75.html
# https://andrewpwheeler.wordpress.com/2013/03/18/the-junk-charts-challenge-remaking-a-great-line-chart-in-spss/
# http://www.minneapolisfed.org/publications_papers/studies/recession_perspective/
link = "http://www.minneapolisfed.org/publications_papers/studies/recession_perspective/data/historical_recessions_recoveries_data_05_03_2013.xls"
file = "data/us.recessions.4807.xls"
data = "data/us.recessions.4807.txt"
if(!file.exists(data)) {
if(!file.exists(file)) download(link, file, mode = "wb")
file <- read.xlsx(file, 1, rowIndex = c(6, 8:80), colIndex = 1:12)
write.csv(file, data, row.names = FALSE)
}
data <- read.csv(data, stringsAsFactors = FALSE)
str(data)
names(data) <- c("t", gsub("X", "", names(data)[-1]))
data <- melt(data, id = "t", variable = "recession.year")
head(data)
notes <- ddply(na.omit(data), .(recession.year), summarise,
x = max(t) + 1,
y = value[max(t) + 1])
qplot(data = data, x = t, y = value, group = recession.year, geom = "line") +
geom_line(data = subset(data, recession.year == 2007),
color = "red", size = 1) +
geom_text(data = notes, aes(x = x, y = y, label = recession.year), hjust = 0,
color = ifelse(notes$recession.year == 2007, "red", "black")) +
geom_hline(y = 0, linetype = "dashed") +
scale_x_continuous("Months after peak", lim = c(0, 75)) +
labs(y = "Cumulative decline from NBER peak")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment