Skip to content

Instantly share code, notes, and snippets.

@kanishkamisra
Created April 29, 2018 16:30
Show Gist options
  • Save kanishkamisra/06d112db3a767f70de55ef2f5f7db15a to your computer and use it in GitHub Desktop.
Save kanishkamisra/06d112db3a767f70de55ef2f5f7db15a to your computer and use it in GitHub Desktop.
library(tidyverse)
#> ── Attaching packages ─────────────────────────────────────────────────────────── tidyverse 1.2.1 ──
#> ✔ ggplot2 2.2.1 ✔ purrr 0.2.4
#> ✔ tibble 1.4.2 ✔ dplyr 0.7.4
#> ✔ tidyr 0.8.0 ✔ stringr 1.3.0
#> ✔ readr 1.1.1 ✔ forcats 0.2.0
#> ── Conflicts ────────────────────────────────────────────────────────────── tidyverse_conflicts() ──
#> ✖ dplyr::filter() masks stats::filter()
#> ✖ dplyr::lag() masks stats::lag()
options(scipen = 99)
fatal <- data_frame(
year = 2000:2015,
fatal_encounters = c(576, 667, 693, 801, 741, 829, 930, 917, 903, 964, 1052, 1151, 1215, 1502, 1493, 1298),
shr = c(308, 371, 341, 368, 365, 345, 384, 392, 374, 411, 393, 399, 426, 467, 450, 452)
)
fatal_long <- fatal %>%
gather(fatal_encounters, shr, key = "key", value = "value")
fatal_long
#> # A tibble: 32 x 3
#> year key value
#> <int> <chr> <dbl>
#> 1 2000 fatal_encounters 576
#> 2 2001 fatal_encounters 667
#> 3 2002 fatal_encounters 693
#> 4 2003 fatal_encounters 801
#> 5 2004 fatal_encounters 741
#> 6 2005 fatal_encounters 829
#> 7 2006 fatal_encounters 930
#> 8 2007 fatal_encounters 917
#> 9 2008 fatal_encounters 903
#> 10 2009 fatal_encounters 964
#> # ... with 22 more rows
fatal_long %>%
ggplot(aes(year, value, color = key)) +
geom_line() +
labs(
title = "Number of police homicides per year",
y = "Police Homicides",
color = "legend_title"
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment