Data

The data for tidy tuesday week 5 is US census data. The data contains information at a county level for a number of different variables/factors, such as population, income, and employment. More information on the dat acan be found here.

Map plot

The maps library can be used to create plots of geographical data.

I decided to create a function which would plot the data on a map. The function takes any of the numerical variables in the data and will output a heatmap of that variable.

Code

library(dplyr)
library(ggplot2)
library(maps)
library(scales)

tidy_tuesday5 <- readr::read_csv("https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2018-04-30/week5_acs2015_county_data.csv")

 # County map data
counties <- map_data("county")

 # Combined county data
all_county <- tidy_tuesday5 %>% 
  mutate(County=tolower(County), State=tolower(State)) %>%
  inner_join(counties, by=c("County" = "subregion", "State" = "region"))

county_plot <- function(x, legend_format=number){
  ggplot(data = all_county, mapping = aes(x = long, y = lat, group = group)) + 
    coord_fixed(1.3) + 
    geom_polygon(aes_string(fill = x), color = "grey", size = 0.05) +
    scale_fill_distiller(palette = "Spectral", labels = legend_format) +
    theme_void() +
    theme(plot.margin = unit(c(0,0,0,0), "cm"))
}

county_plot("Income", dollar)
county_plot("MeanCommute", number_format(suffix = " mins"))




Twitter



Extra Code

State version

I also created a version to plot data on a state level

 # State map data
states <- map_data("state")

 # Aggregated state data
all_state <- tidy_tuesday5 %>%
  group_by(State = State) %>%
  summarise(Pop = sum(TotalPop),
            Income = mean(Income, na.rm = T),
            Men = sum(Men), Women = sum(Women),
            Unemployment_tot = sum(Unemployment * 0.01 * TotalPop)) %>%
  mutate(Ratio_men = Men / (Men + Women), 
         Unemployment = Unemployment_tot/Pop, 
         State = tolower(State)) %>%
  inner_join(states, by = c("State" = "region"))


 # Function to plot variable
state_plot <- function(x, legend_format=number){
  ggplot(data = states, mapping = aes(x = long, y = lat, group = group)) +
    coord_fixed(1.3) +
    geom_polygon(data = all_state, aes_string(fill = x), color = "white") +
    geom_polygon(color = "black", fill = NA) +
    scale_fill_distiller(palette = "Spectral", labels = legend_format) +
    theme_void()
}


state_plot("Pop", comma)

state_plot("Ratio_men", percent_format(accuracy=1))

state_plot("Income", dollar)

state_plot("Unemployment", percent_format(accuracy=1))