在一张地图上显示两个不同的数据集

问题描述 投票:0回答:1

我想在一个公共地图上可视化两个不同的数据集以进行比较(因此它们应该具有不同的图标)。到目前为止,我一直在使用以下代码,但它只能让我顺利地显示一个数据集。 我的数据集的工作方式如下:

当我运行时: dput(head(data_a)) dput(head(data_b)) 输出是:

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"
))

这是我现在拥有的代码,适用于一个数据集:D

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)

结果(如我所愿,但仅适用于一个数据集:)

当我使用代码时:

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

我得到以下结果:

控制台输出也没有显示错误:

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

此问题基于以下问题:元数据中存在错误?

(一位 stackoverlow 成员告诉我我应该问一个新问题;)

r leaflet gis r-leaflet
1个回答
0
投票

如果您的目标是根据数据集为图标分配不同的颜色,您可能需要将两个数据集合并为一个,创建图标列表以根据条件分配不同的颜色,并使用 addAwesomeMarkers 而不是 addMarkers 。我会这样做:

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)

© www.soinside.com 2019 - 2024. All rights reserved.