Create leaflet map with mainland country and overseas territories grouped together - r

I was wondering whether it would possible to easily group mainland France with its overseas territories on a leaflet map, similarly to what we often see for the USA.
Something like the map below.
Ideally, I could still use regular coordinates to place points on the map, but that may not be possible. I've spent a few hours to try and figure it out, but lamentably failed...
I guess one solution would be to modify the original shapefile to move the polygons of interest and expand them proportionally, but that would not be very straightforward (note that that kind of shapefile may already exist, but if it does, I haven't used the right keywords)
Any idea?
Thanks for your help :)

Using the first link provided by #Will Hore Lacy Multiple leaflets in a grid, you can use htmltools to create the desired view.
library(htmltool)
library(leaflet)
First, create all maps, providing different heights for each map.
# main map
# indicate height (should be related to the number of other maps : 800px = 4 maps * 200px)
metropole <- leaflet(height = "800px") %>%
addTiles() %>%
setView(lng = 2.966, lat = 46.86, zoom = 6) %>%
addControl("Métropole", position = "bottomleft")
# smaller maps :
# height is identical (200px)
reunion <- leaflet(height = "200px") %>%
addTiles() %>%
setView(lng = 55.53251, lat = -21.133165, zoom = 8) %>%
addControl("La Réunion", position = "bottomleft")
martinique <- leaflet(height = "200px") %>%
addTiles() %>%
setView(lng = -61.01893, lat = 14.654532, zoom = 8) %>%
addControl("Martinique", position = "bottomleft")
guadeloupe <- leaflet(height = "200px") %>%
addTiles() %>%
setView(lng = -61.53982, lat = 16.197587, zoom = 8) %>%
addControl("Guadeloupe", position = "bottomleft")
guyane <- leaflet(height = "200px") %>%
addTiles() %>%
setView(lng = -53.23917, lat = 3.922325, zoom = 6) %>%
addControl("Guyane", position = "bottomleft")
Create the HTML grid.
leaflet_grid <-
tagList(tags$table(width = "100%", border = "1px",
tags$tr(
tags$td(reunion, width = "30%"), # reduce first column width
tags$td(metropole, rowspan = 4) # span across the four other maps
),
tags$tr(
tags$td(martinique)
),
tags$tr(
tags$td(guadeloupe)
),
tags$tr(
tags$td(guyane)
)
)
)
browsable(leaflet_grid)
This should give something like this :

Related

How can I control the zoom speed when selecting a point on a leaflet map (with Shiny) when the user presses the button to zoom in to the point?

I am developing a dashboard in Rmarkdown, Flexdashboard and Shiny. At the moment I am using Leaflet to map several points and I have a shiny input, which when pressing a button selects a certain point and zooms it.
I would like to know if there is a way to control the zoom speed.
An example in pictures:
Initial state of the map, the points are in blue.
Panel where the user selects the point and the button executes the filtering and zooming.
In this image I show the zoom to the selected point, which is very fast.
I leave some example code and I remain attentive to your comments. Thank you.
{r chunk, echo = false}
leafletOutput(outputId = "mapa1")
# DATA FILTERED BY USER INPUT (1 point)
rbdFiltered <- reactive({
misDatos()[misDatos()$rdb == input$rbd, ]
})
# LEAFLET OBJECT WITH ALL POINTS
myMap <- reactive({
input$params
leaflet() %>%
addTiles() %>%
addCircleMarkers(data = misDatos(), lng = ~ longitud, lat = ~ latitud, radius = 3, group = "myMarkers") %>%
setView(lng = -70.560946, lat = -33.592340, zoom = 13) # ZOOM SET HERE
})
# OBSERVE EVENT (Select a point)
observeEvent(input$BT_select_rbd, {
leafletProxy(mapId = "mapa1") %>% clearGroup(group = "myMarkers") %>%
addCircleMarkers(data = rbdFiltered(), lng = ~ longitud, lat = ~ latitud, radius = 3, group = "myMarkers") %>%
# HERE A ZOOM TO THE SELECTED POINT IS PROCESSED, IS IT POSSIBLE TO CONTROL THE SPEED?
setView(lng = rbdFiltered()$longitud, lat = rbdFiltered()$latitud, zoom = 17)
})
# OBSERVE EVENT (Show all points)
observeEvent(input$BT_todo_rbd, {
leafletProxy(mapId = "mapa1") %>% clearGroup(group = "myMarkers") %>%
addCircleMarkers(data = misDatos(), lng = ~ longitud, lat = ~ latitud, radius = 3, group = "myMarkers") %>%
setView(lng = -70.560946, lat = -33.592340, zoom = 13) # WE RETURN TO ORIGINAL ZOOM
})
output$mapa1 <- renderLeaflet({myMap()})
Regards
EDITED: Images
Instead of using setView() in your Shiny events, try using leaflet::flyTo().

How to resize large images in R Leaflet marker popups?

My code:
library(leaflet)
df <- as.data.frame(read.csv("arts.csv"))
file <- as.character(df$url)
leaflet() %>% addTiles()
%>% addMarkers(data = df, lng = ~lon, lat = ~lat,
popup = paste0("<img src = ", file, ">"))
%>% popupOptions(maxWidth = "auto")
I used some code snippet from here: Image in R Leaflet marker popups
My question is: how can I easily resize large images appearing in popups?
The illustartion of the problem:
For Example, I would like to make this photo of Cave Rapa Nui smaller to fit the pop-up.
Thank you in advance!
You can adjust the size by using the HTML width attribute.
Edit your code from above to e.g.
map <- leaflet() %>%
addTiles() %>%
addMarkers(data = df,
lng = ~lon,
lat = ~lat,
popup = paste0("<img src = ", file, " width = 300>"))
map # print the map

leaflet.extras: measure distance in metres

I would like to create a map where I can interactively measure the distance between 2 points. Luckily, leaflet.extras has exactly what I need, however, I'm struggling to get it to produce the outputs in metres (or kilometres) as opposed to feet.
Consider the below piece of code:
library(leaflet)
library(leaflet.extras)
leaflet() %>%
addTiles() %>%
addDrawToolbar(
editOptions=editToolbarOptions(selectedPathOptions=selectedPathOptions())
)
It creates the following map:
However, this example (chunk 3) effectively the same code to create the same measuring tool (polyline), except it works in KM, whereas my example works in feet.
If you have any tips that can help me switch to metres as opposed to feet, I would really appreciate it.
The drawPolylineOptions function does not allow to set the option feet=FALSE.
Hence, I suggest to modify drawPolylineOptions as follows:
library(leaflet)
library(leaflet.extras)
mydrawPolylineOptions <- function (allowIntersection = TRUE,
drawError = list(color = "#b00b00", timeout = 2500),
guidelineDistance = 20, metric = TRUE, feet = FALSE, zIndexOffset = 2000,
shapeOptions = drawShapeOptions(fill = FALSE), repeatMode = FALSE) {
leaflet::filterNULL(list(allowIntersection = allowIntersection,
drawError = drawError, guidelineDistance = guidelineDistance,
metric = metric, feet = feet, zIndexOffset = zIndexOffset,
shapeOptions = shapeOptions, repeatMode = repeatMode)) }
leaflet() %>% setView(10.975342,45.421588,9) %>%
addTiles() %>%
addProviderTiles(providers$OpenStreetMap.Mapnik) %>%
addDrawToolbar(
polylineOptions = mydrawPolylineOptions(metric=TRUE, feet=FALSE),
editOptions=editToolbarOptions(selectedPathOptions=selectedPathOptions())
)
Otherwise, using addMeasures you can add to your map a useful tool to measure distances (see the icon at the top right corner of the map).
It is possibile to specify units used to display length results by the primaryLengthUnit option.
leaflet() %>% setView(10.975342,45.421588,9) %>%
addTiles() %>%
addProviderTiles(providers$CartoDB.Positron) %>%
addDrawToolbar(
editOptions=editToolbarOptions(selectedPathOptions=selectedPathOptions())
) %>%
addMeasure(primaryLengthUnit="kilometers", secondaryLengthUnit="kilometers")

How to "addTiles" on top of "addPolygons" in R's Leaflet?

How do you set the layer order in R's leaflet package so that tiles show up on top of polygons filled with color?
Here's what I've got so far:
require(leaflet)
require(acs)
require(tigris)
require(rgdal)
census.income.end.year = 2015
county = 17
nd.counties=acs.fetch(geography=geo.make(state="ND", county=county),
table.number="B01003", endyear = 2015)
tracts <- tigris::tracts(state = 'ND', county = county, cb=FALSE, year = 2015)
# create a geographic set to grab tabular data (acs)
geo<-geo.make(state=c("ND"),
county = county,
tract="*")
# add in median income
median.income <- acs.fetch(endyear = census.income.end.year,
geography = geo,
variable = c("B19013_001"))
income_df <- data.frame(paste0(as.character(median.income#geography$state),
str_pad(as.character(median.income#geography$county), 3, 'left', '0'),
str_pad(as.character(median.income#geography$tract), 5, 'left', '0')),
median.income#estimate)
rownames(income_df)<-1:nrow(income_df)
names(income_df)<-c("GEOID", "hhincome")
income_merged <- geo_join(tracts, income_df, "GEOID", "GEOID")
income_merged <- spTransform(income_merged, CRS("+init=epsg:4326"))
qpal <- colorQuantile("plasma", income_df$hhincome, n = 4)
leaflet() %>%
setView( -96.7898, 46.8772, zoom=11) %>%
addPolygons(data = income_merged,
fillColor = qpal(income_merged$hhincome),
fillOpacity = 1,
weight = 0.3) %>%
addProviderTiles(providers$Hydda.RoadsAndLabels)
Ultimately, I'ld like to do this with addTiles (instead of addProviderTiles as in the above code) using a custom MapBox, but I can't figure out how to make that reproducible for this example... given that you need a key to access custom MapBox tiles (BTW, I've created a custom MapBox tile that should be transparent except for roads and labels, so the underlying polygons should "show thru.")
Here is one way to do add a tile on top of a circle with the non-R version of leaflet: http://jsfiddle.net/dcu9pz2w/, but I don't see how to make that work in my context. I think adding "panes" may be the way to go, but I don't see that functionality in R leaflet. Also, I explored z-index values, but that seemed to be a dead end.
Any help is much appreciated!
R leaflet now includes addMapPane function. The solution to this problem is to first set up pane order and then add tiles/polygons. Reproducible example:
library(leaflet)
library(geojsonio)
# get polygon data
# https://github.com/simonepri/geo-maps/blob/master/info/countries-land.md
world <- geojson_read(
"https://github.com/simonepri/geo-maps/releases/download/v0.6.0/countries-land-10km.geo.json",
what = "sp"
)
# generate random values
world#data$value <- runif(nrow(world#data))
# get color palette
color_pal <- colorNumeric(palette = "YlOrRd", domain = NULL)
# get leaflet map
leaflet() %>%
setView(lat = 50, lng = 15, zoom = 4) %>%
addMapPane("background_map", zIndex = 410) %>% # Level 1: bottom
addMapPane("polygons", zIndex = 420) %>% # Level 2: middle
addMapPane("labels", zIndex = 430) %>% # Level 3: top
addProviderTiles(
providers$Esri.WorldTerrain,
options = pathOptions(pane = "background_map")
) %>%
addPolygons(
data = world, stroke = FALSE, smoothFactor = 0.2,
fillOpacity = 0.6, fillColor = ~color_pal(value),
options = pathOptions(pane = "polygons")
) %>%
addProviderTiles(
providers$Stamen.TonerLabels,
options = pathOptions(pane = "labels")
)

Change color of leaflet marker

Is there anyway to change the color of leaflet marker base on the value of some variable. In the following map, for example, I wish to assign marker color based on mag variable:
library(leaflet)
data(quakes)
# Show first 20 rows from the `quakes` dataset
leaflet(data = quakes[1:20,]) %>% addTiles() %>%
addMarkers(~long, ~lat, popup = ~as.character(mag))
I often use the circle markers because you can change both the size and color based on other variables. For example, I have created a binned variable from a continuous using the following code:
# first cut the continuous variable into bins
# these bins are now factors
last$BeatHomeLvl <- cut(last$BeatHome,
c(0,.5,1,2,3,5,100), include.lowest = T,
labels = c('<.5x', '.5-1x', '1-2x', '2-3x', '3-5x','5x+'))
# then assign a palette to this using colorFactor
# in this case it goes from red for the smaller values to yellow and green
# standard stoplight for bad, good, and best
beatCol <- colorFactor(palette = 'RdYlGn', last$BeatHomeLvl)
When you plot it, I use the code for circle markers. The radius/area of the circle is based on the actual value of the factor and then color is assigned according to the bins.
m1 <- leaflet() %>%
addTiles() %>%
addProviderTiles(providers$OpenStreetMap, group = 'Open SM') %>%
addProviderTiles(providers$Stamen.Toner, group = 'Toner') %>%
addProviderTiles(providers$Esri.NatGeoWorldMap, group = 'NG World') %>%
setView(lng = -72, lat = 41, zoom = 8) %>%
addCircleMarkers(data = Jun, lat = ~Lat, lng = ~Lon,
color = ~beatCol(BeatHomeLvl), popup = Jun$Popup,
radius = ~sqrt(BeatHome*50), group = 'Home - Jun') %>%
At the end of your code add a legend. I added some formatting.
addLegend('bottomright', pal = beatCol, values = last$BeatHomeLvl,
title = 'Compare Home<br>Quote Count to<br>3Mos State Avg',
opacity = 1)
This gives you color-coded and sized circles based on a variable and a nice legend.
As far as I know, you need to assign an image file to one level of icon. For instance, if you have three levels in magnitude in the earthquake data, you need to create an icon list with three image paths. Then, you can have three different colors in markers. At least, the following example is getting closer to what you want. I edited a png file and created three png files. You need to specify the paths of the file when you make an icon list.
library(dplyr)
library(leaflet)
mutate(quakes, group = cut(mag, breaks = c(0, 5, 6, Inf), labels = c("blue", "green", "orange"))) -> mydf
### I edit this png file and created my own marker.
### https://raw.githubusercontent.com/lvoogdt/Leaflet.awesome-markers/master/dist/images/markers-soft.png
quakeIcons <- iconList(blue = makeIcon("/Users/jazzurro/Documents/Stack Overflow/blue.png", iconWidth = 24, iconHeight =32),
green = makeIcon("/Users/jazzurro/Documents/Stack Overflow/green.png", iconWidth = 24, iconHeight =32),
orange = makeIcon("/Users/jazzurro/Documents/Stack Overflow/orange.png", iconWidth = 24, iconHeight =32))
leaflet(data = mydf[1:100,]) %>%
addTiles() %>%
addMarkers(icon = ~quakeIcons[group])
This one worked for me:
Source: https://github.com/bhaskarvk/leaflet/blob/master/inst/examples/awesomeMarkers.R
library(leaflet)
icon.glyphicon <- makeAwesomeIcon(icon= 'flag', markerColor = 'blue', iconColor = 'black')
icon.fa <- makeAwesomeIcon(icon = 'flag', markerColor = 'red', library='fa', iconColor = 'black')
icon.ion <- makeAwesomeIcon(icon = 'home', markerColor = 'green', library='ion')
# Marker + Label
leaflet() %>% addTiles() %>%
addAwesomeMarkers(
lng=-118.456554, lat=34.078039,
label='This is a label',
icon = icon.glyphicon)
leaflet() %>% addTiles() %>%
addAwesomeMarkers(
lng=-118.456554, lat=34.078039,
label='This is a label',
icon = icon.fa)
leaflet() %>% addTiles() %>%
addAwesomeMarkers(
lng=-118.456554, lat=34.078039,
label='This is a label',
icon = icon.ion)
# Marker + Static Label using custom label options
leaflet() %>% addTiles() %>%
addAwesomeMarkers(
lng=-118.456554, lat=34.078039,
label='This is a static label',
labelOptions = labelOptions(noHide = T),
icon = icon.fa)
Why not use vector markers based on svg (here is one example implementation - https://github.com/hiasinho/Leaflet.vector-markers) that you can apply any fill color you want to? Instead of having to create a large amount of static image files. Some code involved, yes, but a lot more flexible.
L.Marker uses images (one for the marker, one for the shadow) so that's not possible. You can however use your own images, there's a good write up on the topic among the tutorials on the Leaflet site:
http://leafletjs.com/examples/custom-icons.html

Resources