最近有一个朋友给我出了一个难题,说他有一份数据,是美国 CDCs National Center for Health Statistics上面搞来的关于美国毒品死亡率的数据。想要我可视化一下,问我能不能做到。
当然能做到就是一下:
CU <- read.csv('/Users/milin/Downloads/NCHS_-_Drug_Poisoning_Mortality_by_County__United_States.csv',stringsAsFactors = FALSE)
# library the packages
library(maps)
# load data from maps library
county <- county.fips
library(tidyverse)
# merge the data
CU.1 <- CU %>% left_join(county,by = c('FIPS'='fips'))
CU.1 <- CU.1 %>% filter(Year=='2004')
colors = c("#F1EEF6", "#D4B9DA", "#C994C7", "#DF65B0", "#DD1C77", "#980043",'#FFFFD4','#FED98E','#FE9929','#D95F0E','#993404','#00007F','#007FFF','#7FFF7F','#FF7F00','#7F0000')
# unemp$colorBuckets <- as.numeric(cut(unemp$unemp, c(0, 2, 4, 6, 8, 10, 100)))
leg.txt <- c("<2",'2-3.9','4-5.9','6-7.9','8-9.9',"10-11.9", "12-13.9", "14-15.9", "16-17.9", "18-19.9",'20-21.9','22-23.9','24-25.9','26-27.9','28-29.9','30+')
CU.1 <- CU.1 %>% mutate(EAC=case_when(
Estimated.Age.adjusted.Death.Rate..16.Categories..in.ranges.==leg.txt[1]~1,
Estimated.Age.adjusted.Death.Rate..16.Categories..in.ranges.==leg.txt[2]~2,
Estimated.Age.adjusted.Death.Rate..16.Categories..in.ranges.==leg.txt[3]~3,
Estimated.Age.adjusted.Death.Rate..16.Categories..in.ranges.==leg.txt[4]~4,
Estimated.Age.adjusted.Death.Rate..16.Categories..in.ranges.==leg.txt[5]~5,
Estimated.Age.adjusted.Death.Rate..16.Categories..in.ranges.==leg.txt[6]~6,
Estimated.Age.adjusted.Death.Rate..16.Categories..in.ranges.==leg.txt[7]~7,
Estimated.Age.adjusted.Death.Rate..16.Categories..in.ranges.==leg.txt[8]~8,
Estimated.Age.adjusted.Death.Rate..16.Categories..in.ranges.==leg.txt[9]~9,
Estimated.Age.adjusted.Death.Rate..16.Categories..in.ranges.==leg.txt[10]~10,
Estimated.Age.adjusted.Death.Rate..16.Categories..in.ranges.==leg.txt[11]~11,
Estimated.Age.adjusted.Death.Rate..16.Categories..in.ranges.==leg.txt[12]~12,
Estimated.Age.adjusted.Death.Rate..16.Categories..in.ranges.==leg.txt[13]~13,
Estimated.Age.adjusted.Death.Rate..16.Categories..in.ranges.==leg.txt[14]~14,
Estimated.Age.adjusted.Death.Rate..16.Categories..in.ranges.==leg.txt[15]~15,
Estimated.Age.adjusted.Death.Rate..16.Categories..in.ranges.==leg.txt[16]~16
))
# align data with map definitions by (partial) matching state,county
# names, which include multiple polygons for some counties
# cnty.fips <- county.fips$fips[match(map("county", plot=FALSE)$names,
# county.fips$polyname)]
cnty.fips <- CU.1$FIPS[match(maps::map("county", plot=FALSE)$names,
CU.1$polyname)]
# colorsmatched <- unemp$colorBuckets [match(cnty.fips, unemp$fips)]
colorsmatched <- CU.1$EAC[match(cnty.fips, CU.1$FIPS)]
# draw map
maps::map("county", col = colors[colorsmatched], fill = TRUE, resolution = 0,
lty = 0, projection = "polyconic")
maps::map("state", col = "white", fill = FALSE, add = TRUE, lty = 1, lwd = 0.2,
projection="polyconic")
title("unemployment by county, 2009")
legend("topright", leg.txt, horiz = F, fill = colors,ncol = 2)