Guest User

Untitled

a guest
Jan 24th, 2018
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.86 KB | None | 0 0
  1. library(directlabels)
  2. library(ggplot2)
  3. library(scales)
  4. library(tidyr)
  5.  
  6. df = data.frame(Canada = c(522, 609, 611),
  7. US = c(15872, 17793, 19362),
  8. Mexico = c(20010, 20762, 24559))
  9. df = data.frame(apply(df, 2, function(x) x = x/x[1]))
  10. df$year = 2014:2016
  11. df = tidyr::gather(df, country, percent, -year)
  12. p = ggplot(df, aes(year, percent, group = country, color = country)) +
  13. geom_line() +
  14. xlab("year") +
  15. ylab("homicide count as a percentage of 2014") +
  16. scale_x_continuous(breaks = (2014:2016), limits = c(2014,2016.3)) +
  17. ggtitle("Homicides in Mexico, the US, and Canada 2014-2016 (2014 = 100%)" ,
  18. subtitle = "Sources: Statistics Canada, CANSIM, table 253-0001 and Homicide Survey, Canadian Centre for Justice Statistics, INEGI, and CDC Wonder") +
  19. theme_bw() +
  20. scale_y_continuous(labels=percent)
  21. directlabels::direct.label(p, "last.bumpup")
Add Comment
Please, Sign In to add comment