Last active
September 11, 2018 19:51
-
-
Save alanocallaghan/c6abb7be99dfff2a3d454fbc3c4b1589 to your computer and use it in GitHub Desktop.
Quick plot of cat and dog ratios
This file contains 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("ggplot2") | |
library("usmap") | |
library("httr") | |
library("readxl") | |
# Read data | |
GET("https://query.data.world/s/pth2w34nwc6uhg7mmvr7n56257y2gw", write_disk(tf <- tempfile(fileext = ".xlsx"))) | |
pets <- read_excel(tf) | |
pets <- pets[!colnames(pets) %in% "X"] | |
pets <- dplyr::rename_(pets, "state" = "Location") | |
pets[["catDogRatio"]] <- pets[["Dog Population (in 1000)"]] / (pets[["Dog Population (in 1000)"]] + pets[["Cat Population"]]) # catDogRatio: dog percentage from a total of dogs + cats | |
# Plot US map with catDogRatio per state | |
catdog_plot <- plot_usmap(data = pets, values = "catDogRatio", lines = "white") + | |
geom_polygon(aes(x = c(0), y = c(0))) + | |
scale_fill_distiller( | |
palette = "Spectral", | |
type = "div", | |
name = "Dog to Cat Ratio", | |
na.value = "#E8E8E8", | |
limits = c(1 / 2.8, 1.8 / 2.8), | |
breaks = c(1 / 2.75, 1 / 2.25, 0.5, 1.25 / 2.25, 1.75 / 2.75), | |
labels = c("1.75x more cats", "1.25x more cats", "1:1 cats and dogs", "1.25x more dogs", "1.75x more dogs") | |
) + | |
labs( | |
title = "Cats and dogs in the US", | |
subtitle = "More cats in northern states, more dogs in southern states", | |
caption = "Source: data.world, plot by @veerlevanson") + | |
theme_void(14) + | |
theme(legend.position = "right", | |
text = element_text(family="Roboto"), | |
plot.title = element_text(size = 18, hjust = 0.5), | |
plot.subtitle = element_text(hjust = 0.5), | |
plot.caption = element_text(size = 12, hjust = 1), | |
plot.margin = unit(c(0.5,0.5,0.5,0.5), "cm") | |
) | |
print(catdog_plot) | |
# Save plot | |
ggsave(catdog_plot, filename = "catdog_us.jpg", width=15, height=10, units="cm", scale=1.75) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment