TopoJSON choropleth in R/Leaflet? - r

Is it possible to style a TopoJSON file from its features for a choropleth using R/leaflet? Tried a few things, and I'm not sure if this is impossible with the leaflet package or if I just don't have the syntax right, especially accessing the properties to enter in the pal() function. Here's what I have:
pal<-colorNumeric(palette ="YlOrRd",domain = USAdata$GINI) #USAdata data frame I merged with the spdf before converting it to shp/topojson
map<-leaflet() %>%
addTiles(options=tileOptions(minZoom = 3)) %>%
setMaxBounds(-167.276413,5.499550,-52.233040, 83.162102) %>%
setView(93.85,37.45,zoom =3) %>%
#addGeoJSON(geojson = jso5)
addTopoJSON(topojson=jso, fillColor = ~pal("GINI"))
#addPolygons(data=poly)
this throws up an error:
"Error in UseMethod("doResolveFormula") :
no applicable method for 'doResolveFormula' applied to an object of class "NULL""
I also tried converting it to an R object the topojson with fromJSON() and adding style elements, but this won't load after I try send it back with toJSON().
Not sure if relevant, but the topojson was created from a shapefile made following the instructions here:
with cl:
topojson -o 'USApuma.json' --shapefile-encoding utf8 --id-property=+GEOID10 -p GINI,+STATEFP10,+GEOID10 -- 'usaetest.shp'
then read in with readLines().
Eventually trying to throw this into a shiny app. Here's some examples I've been following.

Do you need to use TopoJSON? If not consider using the tigris package (disclosure: I created and maintain the package). It'll get you access to just about any Census geographic dataset you need, and plays nicely with leaflet. Here's a brief example in line with what you are doing. For example, you can get all PUMAs in the continental US with the following code:
library(readr)
library(tigris)
library(leaflet)
us_states <- unique(fips_codes$state)[1:51]
continental_states <- us_states[!us_states %in% c("AK", "HI")]
pumas_list <- lapply(continental_states, function(x) {
pumas(state = x, cb = TRUE)
})
us_pumas <- rbind_tigris(pumas_list)
I've generated a sample dataset that measures PUMA median household income for this example; the geo_join function from the tigris package can merge the dataset to the spatial data frame us_pumas:
puma_income <- read_csv('http://personal.tcu.edu/kylewalker/data/puma_income.csv')
joined_pumas <- geo_join(us_pumas, puma_income, 'GEOID10', 'GEOID')
We can then plot with Leaflet:
pal <- colorQuantile(palette = 'YlOrRd', domain = joined_pumas$hhincome, n = 7)
leaflet(joined_pumas) %>%
addProviderTiles('CartoDB.Positron') %>%
addPolygons(weight = 0.5, fillColor = ~pal(hhincome),
color = 'lightgrey', fillOpacity = 0.75,
smoothFactor = 0.2) %>%
addLegend(pal = pal,
values = joined_pumas$hhincome)
If you are planning to build a Shiny app, I'd recommend saving out the PUMAs you obtain from tigris first as a .rda file and reading it in with your Shiny script so you don't have to rbind_tigris every time.

Related

simplify my us state shapefile with rmapshaper::ms_simplify gives error

I'm making chloropleth us state map on my shiny app using leaflet package. I found rendering the map is very slow. After googling , it seems like maybe the shapefile is too complex and simplifying that might make it a lot faster. According to this post, simplifying the shapefile might be the answer.
Reading shape file works OK. I was able to render my leaflet map.
states_shape <- tigris::states(cb = TRUE, resolution='500k')
leaflet(states_shape) %>%
addProviderTiles("CartoDB.Positron") %>%
addPolygons(fillColor = "white",
color = "black",
weight = 0.5) %>%
setView(-98.5795, 39.8282, zoom=3)
I tried to simplify my shapefile with rmapshaper::ms_simplify
states_shape_simple <- rmapshaper::ms_simplify(states_shape, keep = 0.05, keep_shapes = TRUE)
I got error like below:
Error in FUN(X[[i]], ...) : isTRUE(gpclibPermitStatus()) is not TRUE
I have no idea what that mean and what to do. Does anyone know why that happened and how to make it work? Thanks a lot!
The following should work:
# packages
library(sf)
#> Linking to GEOS 3.8.0, GDAL 3.0.4, PROJ 6.3.1
library(leaflet)
# data
states_shape <- tigris::states(cb = TRUE, resolution='500k', class = "sf")
# simplify
states_shape_simple <- rmapshaper::ms_simplify(states_shape, keep = 0.05, keep_shapes = TRUE)
states_shape_simple <- st_transform(states_shape_simple, 4326)
# plot
leaflet(states_shape_simple) %>%
addProviderTiles("CartoDB.Positron") %>%
addPolygons(fillColor = "white",
color = "black",
weight = 0.5) %>%
setView(-98.5795, 39.8282, zoom = 3)
Created on 2020-05-27 by the reprex package (v0.3.0)
I added states_shape_simple <- st_transform(states_shape_simple, 4326) since I received a warning message by leaflet saying that the object states_shape_simple had an invalid datum. I don't know if you face the same warning message.
Anyway if you want to read something about the differences between sf and sp check Chapter 1 of Geoocomputation with R (and maybe Chapter 6 about reprojections, such as st_transform). I don't know why it fails with sp, maybe you can ask to the package mantainer.

Exporting a contoured Kernel density estimation plot to raster or shapefile format

I'm trying to perform Kernel density estimation in R using some GPS data that I have. My aim is to create a contoured output with each line representing 10% of the KDE. From here i want to import the output (as a shapefile or raster) into either QGIS or arcmap so I can overlay the output on top of existing environmental layers.
So far i have used AdehabitatHR to create the following output using the below code:
kud<-kernelUD(locs1[,1], h="href")
vud<-getvolumeUD(kud)
vud <- estUDm2spixdf(vud)
xyzv <- as.image.SpatialGridDataFrame(vud)
contoured<-contour(xyzv, add=TRUE)
Aside from being able to remove the colour, this is how i wish the output to appear (or near to). However i am struggling to figure out how i can export this as either a shapefile or raster? Any suggestions would be gratefully received.
With the amt package this should be relatively straightforward:
library(adehabitatHR)
library(sf)
library(amt)
data("puechabonsp")
relocs <- puechabonsp$relocs
hr <- as.data.frame(relocs) %>% make_track(X, Y, name = Name) %>%
hr_kde(trast = raster(amt::bbox(., buffer = 2000), res = 50)) %>%
hr_isopleths(level = seq(0.05, 0.95, 0.1))
# Use the sf package to write a shape file, or any other supported format
st_write(hr, "~/tmp/home_ranges.shp")
Note, it is also relatively easy to plot
library(ggplot2)
ggplot(hr) + geom_sf(fill = NA, aes(col = level))

Make a choropleth from a non-highmap-collection map

I've been trying to make a choropleth map with hcmap from highcharter package; I obtained the polygons from my own shapefile because it's a map that is not on the list of highmap's collection.
To do so, first I managed to transform my shapefile to a GeoJson file, as described here:
https://blog.exploratory.io/creating-geojson-out-of-shapefile-in-r-40bc0005857d
Later I managed to draw the map using the package geosonio as described here:
http://jkunst.com/highcharter/highmaps.html#geojsonio-package
However, I can't figure out how to merge a dataframe with values into the polygons drawn in my map. All the examples availables are merging to mapdata that is in a data.frame format, which I lose when transforming to GeoJson.
Here's my code so far:
library(rgdal)
library(geojsonio)
library(highcharter)
#Get map from shapefile
Mymap <- readOGR(dsn="Mymap", "Mymap", verbose = FALSE) %>%
spTransform(CRS("+proj=longlat +ellps=GRS80 +datum=WGS84"))
#Transform to geoJson
MymapJSON <- geojson_json(Mymap)
#Use geojsonio to make data compatible with hcmap
Myhcmap <- jsonlite::fromJSON(MymapJSON, simplifyVector = FALSE)
Myhcmap<- geojsonio::as.json(Myhcmap)
#Draw map:
highchart(type = "map") %>%
hc_add_series(mapData = Myhcmap, showInLegend = T)
Result:
¿How can I put additional data into the GeoJson so I can draw a choropleth?
I finally got to a solution by myself some time ago, it's was fairly simple but since it's not well documented how to add data to the GeoJSON, I will show it here:
#Work with the map until this step:
Myhcmap <- jsonlite::fromJSON(MymapJSON, simplifyVector = FALSE)
#This part was unnecessary:
#Myhcmap<- geojsonio::as.json(Myhcmap)
#Then, write your map like this:
highchart() %>%
hc_add_series_map(Myhcmap, df, value = "value", joinBy = "ID")
Where:
dfis the dataframe you want to append
value is the column name of the data you want to color your map by
joinBy is the joining key variable

Crosstalk: filter Polylines in Leaflet

I can't get crosstalk to work with leaflet and Polylines - here is an MWE:
library(crosstalk)
library(leaflet)
theta <- seq(0, 2*pi, len = 100)
dat <- data.frame(
lon = cos(theta),
lat = sin(theta),
t = 1:100
)
sd <- SharedData$new(dat)
map <- leaflet() %>%
addTiles() %>%
addCircleMarkers(data = sd, lat = ~lat, lng = ~lon, color = "blue") %>%
addPolylines(data = sd, lat = ~lat, lng = ~lon, color = "blue")
bscols(
filter_slider("t", "Time", sd, column = ~t),
map
)
The time filter_slider applies to the circle markers but not the polylines.
Happy to having a go at fixing this in the R leaflet package if someone can point me in the right direction. I.e. what would be required to change / implement? I assume the support is missing on the javascript side as of now?
UPDATE: Good News!
#dmurdoch has submitted a pull request to add support for polylines and polygons.
Using his version of crosstalk, you can now filter leaflet lines/polygons if they're sp objects (note, it doesn't seem to work with sf yet).
First you will need to install this version of crosstalk:
devtools::install_github("dmurdoch/leaflet#crosstalk4")
Then you will need to make sure your features are Spatial objects, easy using rgdal or raster:
shapes_to_filter <- raster::shapefile("data/features.shp") # raster import to 'Spatial Object'
shapes_to_filter <- rgdal::readOGR("data/features.shp") # rgdal import to 'Spatial Object'
Or, if you use sf and dplyr for most spatial tasks (like me) convert an sf object to Spatial:
library(dplyr)
library(sf)
shapes_to_filter <- st_read("data/features.shp") %>% as('Spatial') # sf import to 'Spatial Object'
Then create an sd object for leaflet, and a data frame copy for the filters (IMPORTANT: note how the group for sd_df is set using the group names from the sd_map) :
library(crosstalk)
sd_map <- SharedData$new(shapes_to_filter)
sd_df <- SharedData$new(as.data.frame(shapes_to_filter#data), group = sd_map $groupName())
Create crosstalk filters using sd_df:
filter_select("filterid", "Select Filter Label", sd_df, ~SomeColumn)
Create the map using the sd_map object:
library(leaflet)
leaflet() %>%
addProviderTiles("OpenStreetMap") %>%
addPolygons(data = sd_map)
And any linked tables/charts need to also use the sd_df object:
library(DT)
datatable(sd_df)
Here's all of the sources for the solution:
GitHub Issue
Github pull request from dmurdoch to add support for polygons/lines
Original solution - with outdated method "sd$transform"
Updated example - with the new "group" method, but I couldnt get their RMD to work
As previously mentioned by Bhaskar Karambelkar:
"crosstalk for now works only with markers and not polylines/polygons"
I hope this changes soon.

Remove unused GEOID in geo_join

I am attempting to plot profitability on top of counties in Minnesota, Iowa, and Nebraska. Using leaflet and tigris, I have been able to plot ALL counties, whether or not I have data for it. This leaves me with a few counties with colors and the rest labeled as NA. Is there a way for me to remove all NA's from my geo_join data so that it just isn't used ala unused Wisconsin areas? I have tried using fortify, but I can't figure out how to determine what county boundaries I'm looking at when I merge the TIGER boundary lines with my County FIPS file in order to remove them.
Here is what my leaflet currently looks like:
My code to get the map is this:
library(tigris)
library(leaflet)
pal <- colorNumeric(c("yellow","dark red"),county$Construction.Cost,na.color="white")
IA_counties <- counties(state="IA", cb=TRUE, resolution ="20m")
MN_counties <- counties(state="MN",cb=TRUE,resolution="20m")
NE_counties <- counties(state="NE",cb=TRUE,resolution="20m")
IA_merged <- geo_join(IA_counties,county,"GEOID", "GEOID")
MN_merged <- geo_join(MN_counties,county,"GEOID","GEOID")
NE_merged <- geo_join(NE_counties,county,"GEOID","GEOID")
popupIA <- paste0("County Projects: ", as.character(paste('$',formatC(format(round(IA_merged$Construction.Cost, 0), big.mark=',', format = 'f')))))
popupMN <- paste0("County Projects: ", as.character(paste('$',formatC(format(round(MN_merged$Construction.Cost, 0), big.mark=',', format = 'f')))))
popupNE <- paste0("County Projects: ", as.character(paste('$',formatC(format(round(NE_merged$Construction.Cost, 0), big.mark=',', format = 'f')))))
leaflet() %>%
addProviderTiles("MapQuestOpen.OSM") %>%
addLegend(pal = pal,
values = IA_merged$Construction.Cost,
position = "bottomright",
title = "County Projects",
labFormat=labelFormat(prefix="$")) %>%
addCircles(lng=yup2$lon, lat=yup2$lat,weight=.75,fillOpacity=0.01,color="red",
radius = 96560) %>%
addCircles(lng=yup2$lon, lat=yup2$lat,weight=.75,fillOpacity=0.01,color="blue",
radius = 193121) %>%
addPolygons(data = IA_counties,
fillColor = ~pal(IA_merged$Construction.Cost),
layerId=1,
fillOpacity = .25,
weight = 0.05,
popup = popupIA)%>%
addPolygons(data=MN_counties,
fillColor=~pal(MN_merged$Construction.Cost),
fillOpacity=0.25,
weight=0.05,
popup = popupMN) %>%
addPolygons(data=NE_counties,
fillColor=~pal(NE_merged$Construction.Cost),
fillOpacity=0.25,
weight=0.05,
popup = popupNE)
I apologize for not including reproducible data, but if needed, please ask. I'm hoping that this is more of a simple na.color= formula solution. The map looks "okay" as of now, but I'd like it if it's possible to not have to make the fillOpacity so light so the NA counties don't stand out.
Thanks for any and all help and please, let me know if you have any questions!
I'm the creator of the tigris package. Thanks so much for using it! In the development version of tigris on GitHub (https://github.com/walkerke/tigris), I've added an option to geo_join to accommodate inner joins, which would remove the unmatched data entirely from the resultant spatial data frame (if this is what you are looking for). You can also supply a common merge column name as a named argument to the new by parameter if you want. For example:
IA_merged <- geo_join(IA_counties, county, by = "GEOID", how = "inner")
should work. I'm still testing but I'll probably submit this update to CRAN in January.
So, embarrassingly, the answer to this question was as simple as I had hoped. I tweaked the following na.color code and it worked exactly as I wanted.
pal <- colorNumeric(c("yellow","dark red"),county$Construction.Cost,na.color="transparent")

Resources