Skip to content

Instantly share code, notes, and snippets.

@briatte
Last active August 29, 2015 14:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save briatte/ce44d856d5a2eba51874 to your computer and use it in GitHub Desktop.
Save briatte/ce44d856d5a2eba51874 to your computer and use it in GitHub Desktop.
A demo of R + ggplot2, using Guardian/ICM polling data.
# A demo of R + ggplot2, using Guardian/ICM polling data.
# 2014-10-02
# Load packages.
pkgs = c("httr", "ggplot2", "lubridate", "RColorBrewer", "reshape2")
pkgs = lapply(pkgs, FUN = function(x) {
if(!require(x, character.only = TRUE)) {
install.packages(x, quiet = TRUE)
library(x, character.only = TRUE)
}
})
# Target data file.
file = "icm.polls.8414.csv"
# Download dataset.
if (!file.exists(file)) {
message("Dowloading the data...")
# Read CSV spreadsheet.
data = GET("http://goo.gl/1tB4eB")
# Write CSV spreadsheet.
write(content(data), file)
}
# Open file.
icm = read.csv(file, stringsAsFactors = FALSE)
# Check result.
str(icm)
# Clean percentages.
icm[, 2:6] = as.numeric(gsub("%", "", as.matrix(icm[, 2:6])))
# Check result.
str(icm)
# Mark general elections.
icm$GE = grepl("RESULT", icm$Sample)
# Check result.
icm[ icm$GE, 2:6 ]
# Convert dates.
icm$Date = dmy(icm$End.of.fieldwork..election.date)
# Check result.
str(icm)
# List polling years.
table(year(icm$Date))
# List general election years.
table(year(icm$Date[ icm$GE ]))
# Subset data.
icm = icm[, c("Date", "GE", "CON", "LAB", "LIB.DEM", "OTHER")]
# Drop missing data.
icm = na.omit(icm)
# Reshape dataset.
icm = melt(icm, id = c("Date", "GE"), variable.name = "Party")
# Check result.
head(icm)
# Check party name order.
levels(icm$Party)
# View Set1 from ColorBrewer.
display.brewer.pal(7, "Set1")
# View selected color codes.
brewer.pal(7,"Set1")[c(2, 1, 5, 4)]
# ggplot2 manual color palette.
colors = scale_colour_manual(values = brewer.pal(7,"Set1")[c(2, 1, 5, 4)])
# ggplot2 manual fill color palette.
fcolors = scale_fill_manual(values = brewer.pal(7,"Set1")[c(2, 1, 5, 4)])
# ggplot2 option to set titles.
titles = labs(title= "Guardian/ICM voting intentions\n", y = NULL, x = NULL)
# Time series.
qplot(data = icm, y = value, x = Date, color = Party, geom = "line") +
colors + titles
# Smoothed lines.
qplot(data = icm, y = value, x = Date,
color = Party, size = I(.75), geom = "point") +
geom_smooth(se = FALSE) +
colors + titles
# Plotting only general elections.
qplot(data = icm[icm$GE, ], y = value, x = Date,
color = Party, size = I(.75), geom = "line") +
geom_point(size = 12, color = "white") +
geom_text(aes(label = year(Date)), size = 4) +
colors + titles +
theme_minimal(12)
# kthxbye
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment