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: Washington

dfzipmedrent=df_zip_demographics[,c(1,8)] 
colnames(dfzipmedrent)=c("region","value")
zip_choropleth(dfzipmedrent,state_zoom="washington")+coord_map() #adds mercator projection

Population: Spokane County (FIPS 53063)

zip_choropleth(dfzipmedrent, county_zoom=53063) + coord_map()

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 47.6683 -117.403
502 E Boone Ave, Spokane, WA, 99258 47.6683 -117.403

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: