Skip to content

Instantly share code, notes, and snippets.

@mkulakowski2
Created December 15, 2012 00:11
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 mkulakowski2/4289817 to your computer and use it in GitHub Desktop.
Save mkulakowski2/4289817 to your computer and use it in GitHub Desktop.
library("ggplot2")
crime <- read.csv("/Users/marcin/R/crime/crime.csv", header=TRUE)
mayors <- read.csv("/Users/marcin/R/crime/mayors.csv", header=TRUE)
(crimeplot <- qplot(Year, Murder, data=crime, geom="line",xlab = "", ylab = "No. Murders"))
crimeplot + geom_vline(aes(xintercept = start), data = mayors)
library(scales)
yrng <- range(crime$Murder)
xrng <- range(crime$Year)
crimeplot + geom_rect(aes(NULL, NULL, xmin = start, xmax = end,
fill = party), ymin = yrng[1], ymax = yrng[2],
data = mayors) + scale_fill_manual(values =
alpha(c("blue", "purple", "red"), 0.2))
last_plot() + geom_text(aes(x = start, y = yrng[1], label = name),data = mayors, size = 3, hjust = 0, vjust = 0)
caption <- paste(strwrap("Number of crime rates in New York have varied a lot over the years", 25), collapse="\n")
crimeplot + geom_text(aes(x, y, label = caption),data = data.frame(x = xrng[2], y = yrng[2]),hjust = 1, vjust = 1, size = 4)
crimeplot + geom_rect(aes(NULL, NULL, xmin = start, xmax = end,
fill = party), ymin = yrng[1], ymax = yrng[2],
data = mayors,size = 3, hjust = 0, vjust = 0) + scale_fill_manual(values =
alpha(c("blue", "purple", "red"), 0.2))
last_plot() + geom_text(aes(x, y, label = caption),data = data.frame(x = xrng[2], y = yrng[2]),hjust = 1, vjust = 1, size = 4)
(a <- qplot(Year, Murder, data = crime, geom = "line"))
(b <- qplot(Population, Murder, data = crime) + geom_smooth(se = F))
(c <- qplot(Population, Murder, data = crime, geom="path"))
library("reshape2")
cri <- melt(crime, id = "Year", measure = c("Murder", "Rape", "Robbery", "Assault", "Burglary", "CarTheft"))
qplot(Year, value, data = cri, geom = "line", colour = variable)
qplot(Year, value, data = cri, geom = "line") + facet_grid(variable ~ ., scales = "free_y")
qplot(Year, value, data = cri, colour = variable)
cri2 <- melt(crime, id = "Year", measure = c("Murder", "Rape", "Robbery", "Assault"))
qplot(Year, value, data = cri2, geom = "line") + facet_grid(variable ~ ., scales = "free_y")
qplot(Year, value, data = cri2, colour = variable)
cplot <- qplot(Year, Murder, data = crime, geom = "line") + ylab("No. of Murders") + geom_hline(xintercept = 0, colour = "grey50")
cplot
cri3 <- melt(crime, id = "Year")
qplot(Year, value, data = cri3, geom = "line", group = variable) + facet_grid(variable ~ ., scale = "free_y")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment