rgisr-leaflet

Displaying two different datasets on one map


I want to visualize two distinct datasets on a common map for comparison purposes (so they should have different Icons). So far, I've been using the following code, but it only allows me to display one dataset smoothly. My datasets work like this on:

![![enter image description here

When I run: dput(head(data_a)) dput(head(data_b)) the outputs are:

The output for data_a:

structure(list(Nr = c(1, 2, 3, 4, 5, 6), Name = c("MD95-2006", 
"IODP 302", "IODP 302", "IODP 302", "IODP 302", "IODP 302"), 
    Lat = c(57.083333, 87.89, 87.9036, 87.92118, 87.93333, 87.86658
    ), Long = c(-8.05, 137.65, 138.46065, 139.365501, 139.535, 
    136.17735), `18O` = c(0.69, NA, NA, NA, NA, NA), Info = c(NA_character_, 
    NA_character_, NA_character_, NA_character_, NA_character_, 
    NA_character_), Source = c("MD95-2006 planktic foraminifera ?13C and ?18O", 
    "https://www.ecord.org/expedition302/", "https://www.ecord.org/expedition302/", 
    "https://www.ecord.org/expedition302/", "https://www.ecord.org/expedition302/", 
    "https://www.ecord.org/expedition302/")), row.names = c(NA, 
-6L), class = c("tbl_df", "tbl", "data.frame"))

The output for data_b:

structure(list(Nr = c(1, 2, 3, 4, 5, 6), Name = c("Simstich", 
"Schiebel", "Schiebel", "Stangeew", "Stangeew", "Stangeew"), 
    Lat = c(75.003333, 62.50275, 67.225033, 56.2747, 53.4347, 
    52.874), Long = c(-7.313333, -13.99235, 2.920317, -48.6992, 
    -50.0673, -51.5128), `18O` = c(NA, NA, NA, NA, NA, NA), Info = c("data for different depths", 
    NA, NA, NA, NA, NA), Source = c("https://doi.pangaea.de/10.1594/PANGAEA.82001?format=html#download", 
    "https://doi.pangaea.de/10.1594/PANGAEA.75647?format=html#download", 
    "https://doi.pangaea.de/10.1594/PANGAEA.75719", "https://doi.pangaea.de/10.1594/PANGAEA.706908", 
    "https://doi.pangaea.de/10.1594/PANGAEA.706908", "https://doi.pangaea.de/10.1594/PANGAEA.706908"
    )), row.names = c(NA, -6L), class = c("tbl_df", "tbl", "data.frame"
))

This Is the code I do have right now and which works for one dataset:

install.packages('readxl')
install.packages('leaflet')
library(leaflet)
library(readxl)

data_a <- read_excel('C:/Users/Location_map.xlsx', sheet = 'Core_Tops')
data_b <- read_excel('C:/Users/Location_map.xlsx', sheet = 'Tows')
dput(head(data_a))
dput(head(data_b))

lat_a <- data_a$Lat
lon_a <- data_a$Long
lat_b <- data_b$Lat
lon_b <- data_b$Long


map <- leaflet() %>%
  addTiles() %>%
  addMarkers(lng = lon_a, lat = lat_a) 

print(map)

The outcome (as I want but only for one dataset):

enter image description here

When I use the code:

map <- leaflet() %>%
  addTiles() %>%
  addMarkers(lng = lon_a, lat = lat_a) %>%

I got following result:

enter image description here

the console output also displays no error:

> map <- leaflet() %>%
+   addTiles() %>%
+   addMarkers(lng = lon_a, lat = lat_a)%>%
+   addMarkers(lng = lon_b, lat = lat_b)
> print(map)

This question based on the question: https://stackoverflow.com/questions/77486602/errors-in-the-metadata


Solution

  • If your objective is to assign different colors to the icons based on the datasets, you may need to merge the two datasets into one, create icons list to assign different colors based on condition and use addAwesomeMarkers instead of addMarkers . Here is how I would do it:

    library(leaflet)
    library(readxl)
    library(dplyr)
    
        data_a <- structure(list(Nr = c(1, 2, 3, 4, 5, 6), Name = c("MD95-2006", 
    "IODP 302", "IODP 302", "IODP 302", "IODP 302", "IODP 302"), 
        Lat = c(57.083333, 87.89, 87.9036, 87.92118, 87.93333, 87.86658
        ), Long = c(-8.05, 137.65, 138.46065, 139.365501, 139.535, 
        136.17735), `18O` = c(0.69, NA, NA, NA, NA, NA), Info = c(NA_character_, 
        NA_character_, NA_character_, NA_character_, NA_character_, 
        NA_character_), Source = c("MD95-2006 planktic foraminifera ?13C and ?18O", 
        "https://www.ecord.org/expedition302/", "https://www.ecord.org/expedition302/", 
        "https://www.ecord.org/expedition302/", "https://www.ecord.org/expedition302/", 
        "https://www.ecord.org/expedition302/")), row.names = c(NA, 
    -6L), class = c("tbl_df", "tbl", "data.frame"))
    
    
    data_b <- structure(list(Nr = c(1, 2, 3, 4, 5, 6), Name = c("Simstich", 
    "Schiebel", "Schiebel", "Stangeew", "Stangeew", "Stangeew"), 
        Lat = c(75.003333, 62.50275, 67.225033, 56.2747, 53.4347, 
        52.874), Long = c(-7.313333, -13.99235, 2.920317, -48.6992, 
        -50.0673, -51.5128), `18O` = c(NA, NA, NA, NA, NA, NA), Info = c("data for different depths", 
        NA, NA, NA, NA, NA), Source = c("https://doi.pangaea.de/10.1594/PANGAEA.82001?format=html#download", 
        "https://doi.pangaea.de/10.1594/PANGAEA.75647?format=html#download", 
        "https://doi.pangaea.de/10.1594/PANGAEA.75719", "https://doi.pangaea.de/10.1594/PANGAEA.706908", 
        "https://doi.pangaea.de/10.1594/PANGAEA.706908", "https://doi.pangaea.de/10.1594/PANGAEA.706908"
        )), row.names = c(NA, -6L), class = c("tbl_df", "tbl", "data.frame"
    ))
    
    data <- bind_rows(list(data_a,data_b), .id="data_source")
    
    icons <- awesomeIcons(
      icon = 'ios-close',
      iconColor = 'black',
      library = 'ion',
      markerColor = ifelse(data$data_source == 1, "green","red")
    )
    
    map <- leaflet(data) %>%
      addTiles() %>%
      addAwesomeMarkers(lng = ~Long, lat = ~Lat, icon = icons) 
    
    print(map)
    

    result