Guest User

Untitled

a guest
Apr 22nd, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.59 KB | None | 0 0
  1. library(ggplot2)
  2. library(maps)
  3.  
  4. # First (and most annoying) task - get matching state and county variables
  5. # for both datasets. And unfortauntely it's not quite right, as you can
  6. # see from the finish product - some counties are missing.
  7. unemp <- read.csv("unemployment09.csv", header = F, stringsAsFactors = F)
  8. names(unemp) <- c("id", "state_fips", "county_fips", "name", "year",
  9. "?", "?", "?", "rate")
  10. unemp$county <- tolower(gsub(" County, [A-Z]{2}", "", unemp$name))
  11. unemp$state <- gsub("^.*([A-Z]{2}).*$", "\\1", unemp$name)
  12.  
  13. county_df <- map_data("county")
  14. names(county_df) <- c("long", "lat", "group", "order", "state_name", "county")
  15. county_df$state <- state.abb[match(county_df$state_name, tolower(state.name))]
  16. county_df$state_name <- NULL
  17.  
  18. state_df <- map_data("state")
  19.  
  20. # Combine together
  21. choropleth <- merge(county_df, unemp, by = c("state", "county"))
  22. choropleth <- choropleth[order(choropleth$order), ]
  23. # Discretise rate to use with Brewer colour scheme - many options here
  24. # choropleth$rate_d <- cut_number(choropleth$rate, 5)
  25. # choropleth$rate_d <- cut_interval(choropleth$rate, 5)
  26. # Nathan's choice is a little odd:
  27. choropleth$rate_d <- cut(choropleth$rate, breaks = c(seq(0, 10, by = 2), 35))
  28.  
  29. # Once you have the data in the right format, recreating the plot is straight
  30. # forward.
  31.  
  32. ggplot(choropleth, aes(long, lat, group = group)) +
  33. geom_polygon(aes(fill = rate_d), colour = alpha("white", 1/2), size = 0.2) +
  34. geom_polygon(data = state_df, colour = "white", fill = NA) +
  35. scale_fill_brewer(pal = "PuRd")
  36.  
  37. # Takes a while to draw because ggplot2 not very efficient with large numbers
  38. # of polygons :(
Add Comment
Please, Sign In to add comment