Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
library(directlabels)
library(ggplot2)
library(scales)
library(tidyr)
df = data.frame(Canada = c(522, 609, 611),
US = c(15872, 17793, 19362),
Mexico = c(20010, 20762, 24559))
df = data.frame(apply(df, 2, function(x) x = x/x[1]))
df$year = 2014:2016
df = tidyr::gather(df, country, percent, -year)
p = ggplot(df, aes(year, percent, group = country, color = country)) +
geom_line() +
xlab("year") +
ylab("homicide count as a percentage of 2014") +
scale_x_continuous(breaks = (2014:2016), limits = c(2014,2016.3)) +
ggtitle("Homicides in Mexico, the US, and Canada 2014-2016 (2014 = 100%)" ,
subtitle = "Sources: Statistics Canada, CANSIM, table 253-0001 and Homicide Survey, Canadian Centre for Justice Statistics, INEGI, and CDC Wonder") +
theme_bw() +
scale_y_continuous(labels=percent)
directlabels::direct.label(p, "last.bumpup")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.