简体   繁体   English

R Leaflet 中的嵌套标记

[英]Nested Markers in R Leaflet

I have a dataframe containing nested ecological survey data.我有一个包含嵌套生态调查数据的dataframe A conceptual description of the data - at the 'top' level is a survey site;数据的概念性描述——“顶层”是调查站点; within sites are n traps;站点内有n 个陷阱; within traps there can be n animals caught.在陷阱内可以捕获n只动物。

I would like to display a Leaflet map showing markers which correspond to the data's top level (ie survey sites).我想显示一个Leaflet map 显示对应于数据顶层(即调查站点)的标记。 When you click on a survey site, it should fan out and show markers of each trap within a survey site.当您单击调查站点时,它应该呈扇形散开并显示调查站点每个陷阱的标记。 When clicking a trap, it should then display a list of the animals caught within the trap (a regular Leaflet pop up would be fine at this point).当点击一个陷阱时,它应该会显示在陷阱中捕获的动物的列表(此时常规的Leaflet弹出窗口就可以了)。

I've no idea whether this is even possible within R Leaflet , despite many searches of Google and/or SO.我不知道这在R Leaflet中是否可行,尽管对 Google 和/或 SO 进行了多次搜索。 I did find one potential Javascript solution but I don't know whether it would translate exactly to resolve the issue I'm facing.我确实找到了一种潜在的 Javascript 解决方案,但我不知道它是否会完全转化为解决我面临的问题。 At the very least, however, the act of hovering on a marker and it displaying nested markers is the functionality that I'm after when viewing traps within sites .然而,至少,悬停在标记上并显示嵌套标记的行为是我在查看站点内的陷阱时所追求的功能。

I have included below an example nested dataframe of some dummy data, plus a barebones Leaflet reprex.我在下面包含了一些虚拟数据的嵌套 dataframe 示例,以及准系统Leaflet表示。 In the dummy data, and for simplicity, I have assigned each site the same lat/lon.在虚拟数据中,为简单起见,我为每个site分配了相同的纬度/经度。 When clicking on any site (001, 002, 003), the traps will fan out from that site.单击任何site (001、002、003)时,陷阱将从该站点散开。 At Site 001 there is only 1 unique trap (ID = 001-001) but Site 2 has two unique traps (002-001 and 002-002).在站点 001 只有 1 个独特的陷阱(ID = 001-001),但站点 2 有两个独特的陷阱(002-001 和 002-002)。 Site 003 has only one trap. Site 003 只有一个陷阱。

library(leaflet)
library(tidyverse)

x<-as_tibble(data.frame(site = c("001", "001", "001", "002", "002", "002", "003"), 
              trap = c("001-001", "001-001", "001-001", "002-001", "002-001", "002-002", "003-001"),
              animal = c("001-001-001", "001-001-002", "001-001-003", "002-001-001", "002-001-002", "002-002-003", "003-001-001"),
              lat = c(51.1, 51.1, 51.1, 52.4, 52.4, 52.4, 51.5),
              lon = c(-1.1, -1.1, -1.1, -1.7, -1.7, -1.7, -1.2)))


leaflet() %>%
  addProviderTiles(providers$OpenStreetMap, options = providerTileOptions(noWrap = TRUE), group = "Open Street Map") %>%
  setView(lng = -1.900796, lat = 52.479380, zoom = 7) %>% 
  addLayersControl(baseGroups = c("Open Street Map")))

Hoping a solution is possible - any help would be very much appreciated.希望解决方案是可能的 - 任何帮助将不胜感激。

There is an option to create clusters ( clusterOptions = TRUE ) within the addCircleMarkers function.addCircleMarkers function 中有一个创建集群的选项 ( clusterOptions = TRUE )。

I am not sure if the code below generates what you want, but it might be able to help you to the next step.我不确定下面的代码是否会生成您想要的内容,但它可能会帮助您进行下一步。

leaflet() %>%
  addProviderTiles(providers$OpenStreetMap, options = providerTileOptions(noWrap = TRUE), group = "Open Street Map") %>%
  setView(lng = -1.900796, lat = 52.479380, zoom = 7) %>% 
  addLayersControl(baseGroups = c("Open Street Map")) %>% 
  addCircleMarkers(data = x, lng = ~lon, lat = ~lat, clusterOptions = TRUE, 
                   popup = paste("<b>Site:</b>", x$site, "<br>",
                                 "<b>lon:</b>", x$lon, "<br>",
                                 "<b>lat:</b>", x$lat, "<br>",
                                 "<b>trap:</b>", x$trap, "<br>",
                                 "<b>animal:</b>", x$animal))

I added the popup to keep track of which marker was which, but you can do something with colors or shapes if that is more visually attractive.我添加了弹出窗口以跟踪哪个标记是哪个,但是如果在视觉上更具吸引力,您可以使用 colors 或形状做一些事情。

An additional note : As you can see I have created the popup with some html script for the formatting ( <b></b> ) and the linebreaks ( <br> ).附加说明:如您所见,我使用一些 html 脚本创建了用于格式化( <b></b> )和换行符( <br> )的弹出窗口。 You can use other html tricks in these popups if you want, it usually works and it's highly customizable.如果需要,您可以在这些弹出窗口中使用其他 html 技巧,它通常有效并且高度可定制。 However, the leafpop package might be a faster way to add tables, images and graphs in popups.但是, leafpop package 可能是在弹出窗口中添加表格、图像和图形的更快方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM