Skip to content

Instantly share code, notes, and snippets.

@yisongtao
Created October 31, 2016 00:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yisongtao/18400826add3b7d86661d20a67fb4087 to your computer and use it in GitHub Desktop.
Save yisongtao/18400826add3b7d86661d20a67fb4087 to your computer and use it in GitHub Desktop.
Map sample code
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