Search code examples
rleafletdata-visualization

Combing Leaflet Maps Together


I generated these 2 random dataset about geographical coordinates (e.g. each point represents an imaginary restaurant in France):

    id = 1:1000
    long = 2.2945 + rnorm( 1000, 0.1085246 , 0.1)
    lat = 48.8584 + rnorm( 1000, 0.009036273 , 0.1)
    
    my_data_1 = data.frame(id, lat, long)

id = 1:1000
    long = 2.2945 + rnorm( 1000, 0.1085246 , 0.1)
    lat = 48.8584 + rnorm( 1000, 0.009036273 , 0.1)
    

   my_data_2 = data.frame(id, lat, long)

I then made these 4 maps:

library(leaflet)
library(leaflet.extras)


map1 = my_data_1 %>%
    leaflet() %>%
    addTiles() %>%
    addHeatmap(lng=~long,lat=~lat,max=100,radius=20,blur=10)


map2 = my_data_2 %>%
    leaflet() %>%
    addTiles() %>%
    addHeatmap(lng=~long,lat=~lat,max=100,radius=20,blur=10)


map3 = my_data_1 %>% 
  leaflet() %>% 
  addTiles() %>% 
  addMarkers(clusterOption=markerClusterOptions())

map4 = my_data_2 %>% 
  leaflet() %>% 
  addTiles() %>% 
  addMarkers(clusterOption=markerClusterOptions())

I would like to combine all these 4 maps into a single map (i.e. a single html file). That is, I would like there to be a window on the right side with 4 toggle buttons :map1, map2, map3, map4. I would like that someone could click these 4 buttons and the corresponding map would load. (only one map can be loaded at a time)

I found this link over here that shows how to combine maps (https://rstudio.github.io/leaflet/showhide.html) - but I do not think that this can be directly used to solve the problem I am working on. This would have been useful if I had different "classes" of restaurants (e.g. cafes, vegan restaurant, fancy restaurant, etc.) and I wanted to toggle between the different restaurants on the same map - however, I want to toggle between 4 completely different maps.

  • I am wondering: is there a straightforward option within the "leaflet" library that can be used to do this? Or does a function need to be written to accomplish this?

For example: c(map1, map2, map3, map4)

Thank you!


Solution

  • Perhaps flexdashboard is what you are looking for leaving you with a html document.

    Using Rmd file:

    ---
    title: "maps"
    output: flexdashboard::flex_dashboard
    ---
        
    ```{r setup, include=FALSE}
    #library(flexdashboard)
    library(leaflet)
    library(leaflet.extras)
    id = 1:1000
    long = 2.2945 + rnorm( 1000, 0.1085246 , 0.1)
    lat = 48.8584 + rnorm( 1000, 0.009036273 , 0.1)
    my_data_1 = data.frame(id, lat, long)
    id = 1:1000
    long = 2.2945 + rnorm( 1000, 0.1085246 , 0.1)
    lat = 48.8584 + rnorm( 1000, 0.009036273 , 0.1)
    my_data_2 = data.frame(id, lat, long)
    
    ```    
      
       
    Column {.tabset}
    -------------------------------------
       
    ### map 1
    
    ```{r}
    map1 = my_data_1 %>%
        leaflet() %>%
        addTiles() %>%
        addHeatmap(lng=~long,lat=~lat,max=100,radius=20,blur=10)
    map1
    ```   
     
    ### map 2
        
    ```{r}
    map2 = my_data_2 %>%
        leaflet() %>%
        addTiles() %>%
        addHeatmap(lng=~long,lat=~lat,max=100,radius=20,blur=10)
    map2
    ```
    
    ### map 3
        
    ```{r}
    map3 = my_data_1 %>% 
      leaflet() %>% 
      addTiles() %>% 
      addMarkers(clusterOption=markerClusterOptions())
    map3
    
    ```
    
    ### map 4
        
    ```{r}
    map4 = my_data_2 %>% 
      leaflet() %>% 
      addTiles() %>% 
      addMarkers(clusterOption=markerClusterOptions())
    map4
    ```
    

    enter image description here