Created
June 30, 2017 11:46
-
-
Save vikjam/d6fc9dbb506901be5267ec0cde6e0ff2 to your computer and use it in GitHub Desktop.
Quick plot of prison rates versus murder rate.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| library(rvest) | |
| library(ggvis) | |
| wiki.prison <- html("http://en.wikipedia.org/wiki/List_of_countries_by_incarceration_rate") | |
| wiki.prison.data <- wiki.prison %>% | |
| html_nodes("table") %>% | |
| html_nodes("table") %>% | |
| .[[2]] %>% | |
| html_table(fill = TRUE) | |
| colnames(wiki.prison.data) <- c("country", "rate") | |
| wiki.prison.data$country[grep("United States of America", wiki.prison.data$country)] <- "United States" | |
| wiki.prison.data$rate[grep("707. See notes below.", wiki.prison.data$rate)] <- "707" | |
| wiki.prison.data$rate[grep("124 or 172. See notes below.", wiki.prison.data$rate)] <- "172" | |
| wiki.prison.data$rate <- as.numeric(wiki.prison.data$rate) | |
| wiki.gdp.capita <- html("http://en.wikipedia.org/wiki/List_of_countries_by_GDP_(PPP)_per_capita") | |
| wiki.gdp.capita.data <- wiki.gdp.capita %>% | |
| html_nodes("table") %>% | |
| html_nodes("table") %>% | |
| .[[1]] %>% | |
| html_table() | |
| wiki.gdp.capita.data <- wiki.gdp.capita.data[ , 2:3] | |
| colnames(wiki.gdp.capita.data) <- c("country", "gdp.per.capita") | |
| wiki.gdp.capita.data$gdp.per.capita <- as.numeric(gsub(",", "", wiki.gdp.capita.data$gdp.per.capita)) | |
| wiki.merge <- merge(wiki.prison.data, wiki.gdp.capita.data, by.x = "country", by.y = "country") | |
| p <- wiki.merge %>% | |
| ggvis( ~ log(gdp.per.capita), ~ rate, key := ~ country) %>% | |
| layer_points() %>% | |
| add_tooltip(function(df) df$country) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment