Created
October 31, 2016 00:58
-
-
Save yisongtao/18400826add3b7d86661d20a67fb4087 to your computer and use it in GitHub Desktop.
Map sample code
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(leaflet) | |
library(tmap) | |
## load NY state population data by zip code from 2010 census | |
nyc_pop <- read.csv("aff_download/DEC_10_SF1_P1_with_ann.csv", skip = 1, | |
header = T, stringsAsFactors = F) | |
nyc_pop$Zip <- as.factor(nyc_pop$Id2) | |
nyczipgeo <- readRDS("nyczipgeo.RDS") ## load NYC zip code shape map | |
noise_sum_zipcode <- as.data.frame(table(noise_complaints$`Incident Zip`)) | |
noise_sum_zipcode$Zip <- noise_sum_zipcode$Var1 | |
noise_sum_zipcode <- left_join(noise_sum_zipcode, nyc_pop) | |
noise_sum_zipcode <- noise_sum_zipcode %>% select(Zip, Freq, Total) %>% | |
mutate( Count = Freq/Total) | |
noise_sum_zipcode$Count[noise_sum_zipcode$Count == Inf] <- NA | |
noise_sum_zipcode$Count[which.max(noise_sum_zipcode$Count)] <- NA | |
nycmap <- append_data(nyczipgeo, noise_sum_zipcode, key.shp = "ZCTA5CE10", key.data = "Zip") | |
nyc_map<- tm_shape(nycmap) + | |
tm_fill("Count", title = "All Noise Complaints per Capita", palette = "YlOrRd", | |
showNA = F, n = 6, id = "Zip") + | |
tm_borders(alpha = 0.5) + | |
tm_style_white(legend.frame = F, legend.bg.color = NA) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment