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

Displaying Two Different Datasets on One Map

提问人:Weiss 提问时间:11/15/2023 最后编辑:Weiss 更新时间:11/16/2023 访问量:95

问:

我想在公共地图上可视化两个不同的数据集以进行比较(因此它们应该具有不同的图标)。到目前为止,我一直在使用以下代码,但它只允许我流畅地显示一个数据集。 我的数据集在以下方面工作:![![enter image description here

当我运行时: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)

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

enter image description here

当我使用代码时:

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

我得到了以下结果:

enter image description here

控制台输出也不显示任何错误:

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

这个问题基于以下问题:元数据中的错误?

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

GIS R-宣传册

评论

0赞 Limey 11/15/2023
在最终的代码片段中,应该是?添加到同一代码段(如上一个代码段)是否能满足您的需求?#%>%print(map)
0赞 Weiss 11/15/2023
当我这样做时,我看不到任何图标:e

答:

1赞 Sys_Tem 11/16/2023 #1

如果您的目标是根据数据集为图标分配不同的颜色,您可能需要将两个数据集合并为一个数据集,创建图标列表以根据条件分配不同的颜色,并使用 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)

result

评论

0赞 Weiss 11/16/2023
这看起来不错,但我看不到data_b
0赞 Weiss 11/16/2023
当我使用你的应用程序时,我得到了一个 emty 地图:e
0赞 Sys_Tem 11/16/2023
我根据您帖子中的两段代码创建了data_a和data_b。
0赞 Weiss 11/16/2023
是的,我看到错误在我这边,你能看看我发布数据 b stackoverflow.com/questions/77486602/errors-in-the-metadata 可能是你发现错误
1赞 Sys_Tem 11/16/2023
@Weiss,我不确定,但就像其他人提到的那样,colorIcon 可能是导致您其他问题中出现错误的问题。R 文档中似乎没有引用它。在控制台中键入 ?colorIcon 会得到以下结果: > ?colorIcon 在指定的包和库中没有关于“colorIcon”的文档:您可以尝试“??colorIcon'