Make a choropleth from a non-highmap-collection map - r

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

Related

r plotly combined bubble and choropleth map using lat lon instead of fips code

I have a dataset with columns "state_name", "county_name", "value1", "value2", "lat", and "lon".
I've tried to apply the examples from the Plotly website and other resources with my data but I'm not getting any luck. They only seem to have an example with the fipscode, and I'm not sure how to use "lat" and "lon" information on my code.
This is the example code I found from the plotly website. Is there a way to plot the map with only the "lat" and "lon" information without having to use counties JSON and the fips code?
library(rjson)
library(plotly)
url <- 'https://raw.githubusercontent.com/plotly/datasets/master/geojson-counties-fips.json'
counties <- rjson::fromJSON(file=url)
url2<- "https://raw.githubusercontent.com/plotly/datasets/master/fips-unemp-16.csv"
df <- read.csv(url2, colClasses=c(fips="character"))
fig <- plot_ly()
fig <- fig %>% add_trace(
type="choroplethmapbox",
geojson=counties,
locations=df$fips,
z=df$unemp,
colorscale="Viridis",
zmin=0,
zmax=12,
marker=list(line=list(
width=0),
opacity=0.5
)
)
fig <- fig %>% layout(
mapbox=list(
style="carto-positron",
zoom =2,
center=list(lon= -95.71, lat=37.09))
)
fig
Given the example you provided, I assume you want to color the counties by either value1 or value2. In this case, you believe you need the geojson as these draw the county's boundaries so that the color gradient can be applied. Depending on what you are looking for:
If you are looking for the fill counties style (strictly choropleth map), you can potentially convert the fips code from the state_name and county_name using this dataset (Counties and FIPS Dataset); you can merge by the county and state to obtain the FIPS codes.
If you are not looking for a choropleth map, you might not need the geojson object; a bubble map might be more suitable (Plotly bubble Maps) as you only need the lat and long to plot this.

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.

R / leaflet - drawing numerous polygons

I am trying to draw numerous polygons with the leaflet package but I can't understand what's going wrong.
The shapefile I use can be found here: https://www.data.gouv.fr/en/datasets/fond-de-carte-des-codes-postaux/
library(leaflet)
library(rgdal)
df <- readOGR("C:/Users/me/codes_postaux","codes_postaux_region")
plot(df)
The shapefile seems ok to me and the code I use is rather simple. However I only get the map as an output and no Polygons. I've been struggling with this issue for quite a long time, I would really appreciate if someone could help me here.
map <- leaflet(df) %>%
addProviderTiles("CartoDB.Positron")%>%
fitBounds(10,38,10,55) %>%
addPolygons(fillOpacity = 0.8, color = "Blue", weight = 1)
map
Look at df#proj4string and the output of plot(df); axis(1); axis(2). Your shapefile uses a specific CRS. You need to transform your SpatialPolygonsDataFrame with a common CRSobj (I got the CRS code from here: Leaflet for R: Raster Images).
library(sp)
pj <- CRS("+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs")
df2 <- spTransform(df, pj)
map2 <- leaflet(df2) %>%
addProviderTiles("CartoDB.Positron")%>%
fitBounds(10,38,10,55) %>%
addPolygons(fillOpacity = 0.8, color = "Blue", weight = 1)
map2

TopoJSON choropleth in R/Leaflet?

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.

Plotting regions and adding color in leaflet R

I need to plot a map of Denmark divided into regions (there are 5: Region Nordjylland, Midtjylland, Sydjylland, Sjælland and Hovedstaden) and then color the regions such that the different regions stand out clearly. I have to use leaflet, since it has other features, that I will use later. I found a map on naturalearthdata.com, that I think I can use, but I can't figure out how to color (or even indicate) the regions. The code I tried is below
library(rgdal)
library(leaflet)
download.file(file.path('http://www.naturalearthdata.com/http/',
'www.naturalearthdata.com/download/50m/cultural',
'ne_50m_admin_1_states_provinces_lakes.zip'),
f <- tempfile())
unzip(f, exdir=tempdir())
world <- readOGR(tempdir(), 'ne_50m_admin_1_states_provinces_lakes', encoding='UTF-8')
DK <- subset(world, name=="Denmark")
leaflet() %>% addTiles() %>% addTopoJSON(DK, weight = 1, color = "#444444", fill = TRUE)
How does one use the naturalearthdata.com data to plot regions/states/provinces of different countries? I have seen a very nice example at
http://www.56n.dk/kort/dk2050kort_age.html
but there is no sample code available.
I have also found a very nice example here: https://rpubs.com/walkerke/leaflet_choropleth - but I need a map of Denmark.
UPDATE: I have found a shapefile at http://www.kortforsyningen.dk which does the trick. So now my question is how do I combine my own data with a shapefile and plot it in leaflet? If I just put
DK <- readOGR(".../shape", layer="REGION")
leaflet(data=DK)
I get a blank screen...

Resources