Skip to content

Instantly share code, notes, and snippets.

@cigrainger
Created June 14, 2016 15:01
Show Gist options
  • Save cigrainger/77aa6cc65ce2faf087becc371f65a3f2 to your computer and use it in GitHub Desktop.
Save cigrainger/77aa6cc65ce2faf087becc371f65a3f2 to your computer and use it in GitHub Desktop.
library(readr)
library(dplyr)
df <- read_csv('~/desktop/guns.csv')
library(ggplot2)
library(ggthemes)
df %>%
arrange(desc(guns_per_100_people)) %>%
top_n(n = 20, wt = guns_per_100_people) %>%
ggplot(aes(x = country,
y = guns_per_100_people)) +
geom_bar(stat = 'identity') +
xlab('') +
ylab('Guns per 100 people') +
coord_flip() +
theme_tufte()
us_guns <- df$guns_per_100_people[df$country == 'United States'])
df %>%
select(guns_per_100_people,
country) %>%
na.omit() %>%
ggplot(aes(x = guns_per_100_people)) +
geom_histogram(aes(y = ..density..),
binwidth = 2.5,
colour = 'black',
fill= 'white') +
geom_density(alpha = .2,
fill= '#FF6666') +
geom_vline(aes(xintercept = mean(guns_per_100_people),
color= 'red',
linetype= 'dashed',
size = 0.5) +
geom_vline(aes(xintercept = us_guns,
color= 'blue',
linetype= 'dashed',
size = 0.5) +
scale_x_continuous() +
expand_limits(x = c(0,100)) +
xlab('Guns per 100 people') +
ylab('') +
theme_tufte()
US <- df$guns_per_100_people[df$country == 'United States']
mu <- mean(df$guns_per_100_people, na.rm = T)
s <- sd(df$guns_per_100_people, na.rm = T)
(US - mu)/s
df %>%
select(homicides_by_guns_per_100k_people,
country) %>%
na.omit() %>%
arrange(desc(homicides_by_guns_per_100k_people)) %>%
top_n(n = 20,
wt = homicides_by_guns_per_100k_people) %>%
ggplot(aes(x = country,
y = homicides_by_guns_per_100k_people)) +
geom_bar(stat = 'identity') +
xlab('') +
ylab('Homicides by guns per 100k people') +
coord_flip() +
theme_tufte()
oecd_list <- data.frame(country = c(
'Australia', 'Austria', 'Belgium', 'Canada',
'Chile', 'Czech Republic', 'Denmark', 'Estonia',
'Finland', 'France', 'Germany', 'Greece',
'Hungary', 'Iceland', 'Ireland', 'Israel',
'Italy', 'Japan', 'South Korea', 'Luxembourg', 'Mexico',
'Netherlands', 'New Zealand', 'Norway', 'Poland',
'Portugal', 'Slovakia', 'Slovenia',
'Spain', 'Sweden', 'Switzerland', 'Turkey',
'England and Wales', 'Scotland', 'United States'))
df %>%
inner_join(oecd_list) %>%
select(homicides_by_guns_per_100k_people,
country) %>%
na.omit() %>%
arrange(desc(homicides_by_guns_per_100k_people)) %>%
top_n(n = 20,
wt = homicides_by_guns_per_100k_people) %>%
ggplot(aes(x = country,
y = homicides_by_guns_per_100k_people)) +
geom_bar(stat = 'identity') +
xlab('') +
ylab('Homicides by guns per 100k people') +
coord_flip() +
theme_tufte()
oecd_list <- filter(oecd_list, country != 'Mexico')
us_homicides <- df$homicides_by_guns_per_100k_people[df$country == 'United States']
df %>%
inner_join(oecd_list) %>%
select(homicides_by_guns_per_100k_people,
country) %>%
na.omit() %>%
ggplot(aes(x = homicides_by_guns_per_100k_people)) +
geom_histogram(aes(y = ..density..),
binwidth = 0.2,
colour = 'black',
fill= 'white') +
geom_density(alpha = .2,
fill= '#FF6666') +
geom_vline(aes(xintercept = mean(homicides_by_guns_per_100k_people,
na.rm = T)),
color = 'red',
linetype = 'dashed',
size = 0.5) +
geom_vline(aes(xintercept=us_homicides,
color = 'blue',
linetype = 'dashed',
size = 0.5) +
scale_x_continuous() +
ylab('') +
expand_limits(x = c(0,4)) +
xlab('Homicides by guns per 100k people') +
theme_tufte()
df %>%
select(guns_per_100_people, homicides_by_guns_per_100k_people,
country) %>%
na.omit() %>%
mutate(us = ifelse(country == 'United States', 1, 0)) %>%
ggplot(aes(x = guns_per_100_people,
y = homicides_by_guns_per_100k_people,
colour = factor(us))) +
geom_point() +
xlab('Guns per 100 people') +
ylab('Homicides by guns per 100k people') +
scale_color_manual(values = c('black', 'red'),
guide = FALSE) +
theme_tufte()
df %>%
inner_join(oecd_list) %>%
select(guns_per_100_people,
homicides_by_guns_per_100k_people,
country) %>%
na.omit() %>%
mutate(us = ifelse(country == 'United States', 1, 0)) %>%
ggplot(aes(x = guns_per_100_people,
y = homicides_by_guns_per_100k_people,
colour = factor(us))) +
geom_point() +
xlab('Guns per 100 people') +
ylab('Homicides by guns per 100k people') +
scale_color_manual(values = c('black', 'red'),guide=FALSE) +
expand_limits(x = c(0,100), y = c(0,4)) +
theme_tufte()
df %>%
select(guns_per_100_people,
percent_homicides_by_guns,
country) %>%
na.omit() %>%
mutate(us = ifelse(country == 'United States', 1, 0)) %>%
ggplot(aes(x = guns_per_100_people,
y = percent_homicides_by_guns,
colour = factor(us))) +
geom_point() +
xlab('Guns per 100 people') +
ylab('% of total homicides by guns') +
scale_color_manual(values = c('black', 'red'),
guide = FALSE) +
theme_tufte()
df %>%
inner_join(oecd_list) %>%
select(guns_per_100_people,
percent_homicides_by_guns,
country) %>%
na.omit() %>%
mutate(us = ifelse(country == 'United States', 1, 0)) %>%
ggplot(aes(x = guns_per_100_people,
y = percent_homicides_by_guns,
colour = factor(us))) +
geom_point() +
xlab('Guns per 100 people') +
ylab('% of total homicides by guns') +
scale_color_manual(values = c('black', 'red'),guide=FALSE) +
expand_limits(x = c(0,100), y = c(0,4)) +
theme_tufte()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment