Created
April 25, 2017 16:21
-
-
Save xiaohk/3d7a9eb05a50f2838726e7efa2732dfb to your computer and use it in GitHub Desktop.
Matching continuous data to the United States map, and Wisconsin county map
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('ggplot2') | |
library('maps') | |
# Plot the United State map with AQL data | |
head(state_aql) | |
# name AQL state | |
# 1 Alabama 34.83090 alabama | |
# 2 Alaska 21.12207 alaska | |
# 3 Arizona 28.49161 arizona | |
# 4 Arkansas 34.61867 arkansas | |
# 5 California 32.34555 california | |
# 6 Colorado 26.22654 colorado | |
states_map = map_data("state") | |
p1 = ggplot(state_aql, aes(map_id = state)) + | |
geom_map(aes(fill = AQL), map = states_map) + | |
expand_limits(x = states_map$long, y = states_map$lat) + | |
scale_fill_gradient(low = "#ff7b75", high = "#5b0300", space = "Lab") + | |
labs(title = "American Air Quality in 2016") | |
p1 | |
# Plot the Wisconsin County map with AQL data | |
# Since there is no full county data, add a column to the county_map and give | |
# unknown data as `NA` | |
print(county_aql) | |
# name AQL county subregion | |
# 1 Ashland 22.39474 ashland ashland | |
# 2 Brown 32.77049 brown brown | |
# 3 Dane 36.56000 dane dane | |
# 4 Dodge 34.44000 dodge dodge | |
# 5 Eau Claire 33.00000 eau claire eau claire | |
# 6 Forest 19.94118 forest forest | |
# 7 Grant 32.43182 grant grant | |
# 8 Kenosha 33.70588 kenosha kenosha | |
# 9 La Crosse 29.81818 la crosse la crosse | |
# 10 Milwaukee 37.14054 milwaukee milwaukee | |
# 11 Outagamie 31.35000 outagamie outagamie | |
# 12 Ozaukee 36.62500 ozaukee ozaukee | |
# 13 Sauk 35.94872 sauk sauk | |
# 14 Taylor 29.24000 taylor taylor | |
# 15 Vilas 22.72727 vilas vilas | |
# 16 Waukesha 35.58824 waukesha waukesha | |
# Load county_map data | |
county_map = map_data('county', region = 'Wisconsin') | |
# Build a list | |
county_list = as.list(query4$AQL) | |
names(county_list) = query4$subregion | |
# Given unknow data NA | |
values = c() | |
for (sub in county_map$subregion){ | |
if (sub %in% names(county_list)){ | |
values[length(values) + 1] = county_list[[sub]] | |
} else { | |
values[length(values) + 1] = NA | |
} | |
} | |
county_map$aql = values | |
head(county_map) | |
# long lat group order region subregion aql | |
# -89.78249 43.64219 1 1 wisconsin adams NA | |
# -89.77676 43.66511 1 2 wisconsin adams NA | |
# -89.79395 43.67657 1 3 wisconsin adams NA | |
# -89.81686 43.68803 1 4 wisconsin adams NA | |
# -89.83405 43.71668 1 5 wisconsin adams NA | |
# -89.83405 43.75679 1 6 wisconsin adams NA | |
p2 = ggplot(county_map, aes(x = long, y = lat, group = group)) + | |
geom_polygon(colour = "grey", aes(fill = aql)) + | |
scale_fill_gradient(low = "#ff7b75", high = "#5b0300", space = "Lab", na.value="white") + | |
labs(title = "AQL in 16 counties of Wisconsin in 2016") + | |
coord_map() | |
p2 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment