August 14, 2015

CRAN Task View on Spatial Data

Geographic Data Display

  • Points, Regions, Routes
  • Variables can be continuous and/or discrete data
  • Static or Interactive

Choropleths

library(choroplethr)
library(choroplethrMaps)
data(df_state_demographics)
names(df_state_demographics)
## [1] "region"            "total_population"  "percent_white"    
## [4] "percent_black"     "percent_asian"     "percent_hispanic" 
## [7] "per_capita_income" "median_rent"       "median_age"

Choropleths

kable(head(df_state_demographics))
region total_population percent_white percent_black percent_asian percent_hispanic per_capita_income median_rent median_age
alabama 4799277 67 26 1 4 23680 501 38.1
alaska 720316 63 3 5 6 32651 978 33.6
arizona 6479703 57 4 3 30 25358 747 36.3
arkansas 2933369 74 15 1 7 22170 480 37.5
california 37659181 40 6 13 38 29527 1119 35.4
colorado 5119329 70 4 3 21 31109 825 36.1

Median Rent: State-level

dfstatemedrent=df_state_demographics[,c(1,8)] # Median Rent
colnames(dfstatemedrent)=c("region","value")
state_choropleth(dfstatemedrent, title="Median Rent by State")

Median Rent: County-level

data("df_county_demographics")
#names(df_county_demographics)
# Federal Information Processing Standard (FIPS) https://en.wikipedia.org/wiki/FIPS_county_code
kable(head(df_county_demographics))
region total_population percent_white percent_black percent_asian percent_hispanic per_capita_income median_rent median_age
1001 54907 76 18 1 2 24571 668 37.5
1003 187114 83 9 1 4 26766 693 41.5
1005 27321 46 46 0 5 16829 382 38.3
1007 22754 75 22 0 2 17427 351 39.4
1009 57623 88 1 0 8 20730 403 39.6
1011 10746 22 71 0 6 18628 276 39.6

Median Rent: County-level

dfcountymedrent=df_county_demographics[,c(1,8)] 
colnames(dfcountymedrent)=c("region","value")
county_choropleth(dfcountymedrent, title="Median Rent by County")

Population: Specific County/ZCTA

library(choroplethrZip)#devtools::install_github("arilamstein/choroplethrZip")
#Zip Code Tabulation Areas are generalized area representations of the United States Postal Service (USPS) ZIP code service areas
library(ggplot2)
data("df_zip_demographics")
kable(head(df_zip_demographics))
region total_population percent_white percent_black percent_asian percent_hispanic per_capita_income median_rent median_age
00601 18450 1 0 0 99 7380 285 36.6
00602 41302 4 0 0 94 8463 319 38.6
00603 53683 2 0 0 96 9176 252 38.9
00606 6591 0 0 0 100 6383 230 37.3
00610 28963 1 0 0 99 7892 334 39.2
00612 68055 0 0 0 100 10188 315 38.5

Population Density Choropleths

Use population density (population per area) rather than raw count for a sound spatial analysis. A good explanation of the trouble with choropleth maps of raw count data is available in Monmonier, Mark, Mapping It Out: Expository Cartography for the Humanities and Social Sciences (1993: University of Chicago Press, Chicago). Pages 159-167.

library(dplyr)
data(zip.map)  #Pull ZCTA area from `choroplethrZip` package data sets to compute density
zarea <- zip.map %>% group_by(region) %>% 
  summarize(zland = first(ALAND10), zwater = first(AWATER10))
df_pop_zip <- df_pop_zip %>% merge(zarea, all.x = TRUE)
df_pop_zip$popdensity <- with(df_pop_zip, count / (zland + zwater) * 1000 ^ 2)
kable(head(df_pop_zip))
region zland zwater value count popdensity
01001 29635482 2229764 545.421805 17380 545.421805
01002 142559990 4276576 195.577987 28718 195.577987
01003 1842385 12788 6083.529676 11286 6083.529676
01005 114586901 667043 44.423642 5120 44.423642
01007 136346000 6943850 101.842524 14593 101.842524
01008 139331464 5086093 8.032264 1160 8.032264

Population Density: Washington

Use an equal area projection since the measure is normalized by area.

df_pop_zip$value <- df_pop_zip$popdensity
zip_choropleth(df_pop_zip, state_zoom = 'washington', legend = 'Count per km^2') + 
  coord_map(projection = 'albers', lat0 = 41, lat1 = 47)  # equal area projection for pacific NW  http://spatialreference.org/ref/sr-org/nad83-us-pacific-northwest-albers/html/

Population Density: Spokane County (FIPS 53063)

zip_choropleth(df_pop_zip, county_zoom = 53063,  legend = 'Count per km^2') + 
  coord_map(projection = 'albers', lat0 = 41, lat1 = 47)

Interactive map using leaflet, tigris and acs

Points

thingstodo=read.table(text="
                      Attraction  lat lon  Population
                      Jepson  47.667268 -117.405114 45
                      Starbucks  47.669088 -117.396847  50
                      WSU 47.660960 -117.405697 250
                      EWU 47.661061 -117.404044 400",header=TRUE)

Geocoding, if only address is available

library(ggmap)

whatislatlon=function(mydata,addressindata){
locs=geocode(as.character(unique(mydata[,addressindata])))
locs$address=unique(mydata[,addressindata])
mydata$latitude=locs$lat[ match(mydata[,addressindata],locs$address)]
mydata$longitude=locs$lon[ match(mydata[,addressindata],locs$address)]
return(mydata)
}

That function works

Address=c("502 E Boone Ave, Spokane, WA, 99258","502 E Boone Ave, Spokane, WA, 99258")
mydummydata=data.frame(Address=Address)
mysmartdata=whatislatlon(mydummydata,"Address")
kable(mysmartdata)
Address latitude longitude
502 E Boone Ave, Spokane, WA, 99258 NA NA
502 E Boone Ave, Spokane, WA, 99258 NA NA

A map of location of interest

location=c(-117.402209,47.665330)
map=get_map(location=location,maptype="roadmap",source="google",zoom=16)
spokanemap=ggmap(map)
print(spokanemap)

Add Attractions

spokanemap=spokanemap+geom_point(data=thingstodo,
            aes(lon,lat,color=Attraction),size=5)
print(spokanemap)

Some Cleaning

spokanemap+theme(panel.grid.major = element_blank(),
                 panel.grid.minor = element_blank(),
                axis.text = element_blank(),axis.title = element_blank(),
                axis.ticks = element_blank())

A traveling student's route

routes=data.frame(x=thingstodo$lon,y=thingstodo$lat)
newmap=get_googlemap(center=location,zoom=16,
                     markers=routes,
                     path = routes,scale=2,maptype = "satellite")
ggmap(newmap,darken=.3)+geom_text(data=thingstodo,aes(lon,lat,label=Attraction),
                                                  color="white",size=3)

R-Studio's leaflet package

  • Interface to leaflet JS
library(leaflet) #rstudio package
leaflet() %>% addTiles()

Add our points of attraction

leaflet() %>% addTiles()%>%addCircleMarkers(data=thingstodo)

Give more information

leaflet() %>% addTiles()%>%addCircleMarkers(data=thingstodo,popup=~Attraction,radius=~Population*.05)

Markers

leaflet() %>% addTiles()%>%addMarkers(data=thingstodo,popup=~Attraction)

Routes

leaflet() %>% addTiles()%>%addMarkers(data=thingstodo,popup=~Attraction)%>% 
  addPolylines(thingstodo$lon,thingstodo$lat)

Few Controls

Leaflet+Shiny+DataTable

Blog example 1: Air Pollution Levels

Blog example 2: Mortality Rates of Children under 5 per 1000 live births

Blog example 3: Animated Choropleths

Blog example 4: Great Circles

Code used is borrowed from many folks including: