3

Leaflet在 R 中为一些数据实现包取得了一些不错的成功。

我还设法将两张地图并排放置为格子和同步。

我有两个问题:

1) 同步...根本不同步地图

我正在使用MapviewRaster尝试同步,但并排的两张地图不会一起放大/缩小。

请看下面的代码:

library(leaflet)
library(ggmap)
library(mapview)
library(raster)
library(magrittr)
UK <- ggmap::geocode("United Kingdom")

#FILE1 <- read.csv("DATASET1.csv")
#FILE2 <- read.csv("DATASET2.csv")
FILE1 <- data.frame('lat' = c(51.31, 51.52, 51.53), 'lon' = c(0.06, 0.11, 0.09))
FILE2 <- data.frame('lat' = c(52.20, 52.25, 52.21), 'lon' = c(0.12, 0.12, 0.12))

map1 <- leaflet(FILE1)%>%
  addTiles()%>%
  addMarkers(clusterOptions = markerClusterOptions())

map2 <- leaflet(FILE2)%>%
  addTiles()%>%
  addMarkers(clusterOptions = markerClusterOptions())

sync(map1,map2)

我确定我在这里遗漏了一些简单的东西,但希望有人可以帮助我看到:)

2) 理想情况下,我希望一张传单地图与另一张重叠

我想通过为每张地图使用不同的市场来区分它们。我试图在互联网上研究这个并通过阅读传单手册但没有成功

非常感谢任何指导!

4

1 回答 1

4

1) 同步两张地图

安装开发版本为我解决了这个问题

# Dependencies
# If your devtools is not the latest version
# then you might have to install "units" manually
install.packages('units') 
install.packages('devtools')
library(devtools)

devtools::install_github("environmentalinformatics-marburg/mapview", ref = "develop")

我使用的代码:

library(leaflet)
library(ggmap)
library(mapview)
library(raster)
library(magrittr)
UK <- ggmap::geocode("United Kingdom")

#FILE1 <- read.csv("DATASET1.csv")
#FILE2 <- read.csv("DATASET2.csv")
FILE1 <- data.frame('lat' = c(51.31, 51.52, 51.53), 'lon' = c(0.06, 0.11, 0.09))
FILE2 <- data.frame('lat' = c(52.20, 52.25, 52.21), 'lon' = c(0.12, 0.12, 0.12))

map1 <- leaflet(FILE1)%>%
  addTiles()%>%
  addMarkers(clusterOptions = markerClusterOptions())

map2 <- leaflet(FILE2)%>%
  addTiles()%>%
  addMarkers(clusterOptions = markerClusterOptions())

mapview::latticeView(map1, map2, ncol = 2, sync = list(c(1, 2)), sync.cursor = FALSE, no.initial.sync = FALSE)
# Or:
sync(map1, map2)

2) 叠加两张地图

您可以使用两个单独的数据框作为数据源,并将它们分别添加到同一个地图中。更改符号样式以区分它们。

map3 <- leaflet(FILE2)%>%
  addTiles() %>%
  addCircleMarkers(data = FILE1) %>%
  addCircleMarkers(data = FILE2,
                   color = '#0FF')
map3

如果你想对集群标记做类似的事情,这里这里有一些很好的文档。基于这些帖子中的一些代码,我在下面创建了一个建议,我使用预先存在的样式来区分不同类型的集群:

FILE1 <- data.frame('lat' = rnorm(n = 1000, mean = 51.4, sd = 0.5), 
                    'lon' = rnorm(n = 1000, mean = 0.8, sd = 0.5))
FILE2 <- data.frame('lat' = rnorm(n = 1000, mean = 53, sd = 0.5), 
                    'lon' = rnorm(n = 1000, mean = -0.5, sd = 0.5))

map3 <- leaflet(rbind(FILE1, FILE2)) %>%
  addTiles() %>%
  addCircleMarkers(data = FILE1,
    color = '#FA5',
    opacity = 1,
    clusterOptions = markerClusterOptions(iconCreateFunction = JS("function (cluster) {    
      var childCount = cluster.getChildCount(); 
      var c = ' marker-cluster-';  
      if (childCount < 3) {  
        c += 'large';  
      } else if (childCount < 5) {  
        c += 'large';  
      } else { 
        c += 'large';  
      }    
      return new L.DivIcon({ html: '<div><span>' + childCount + '</span></div>', 
      className: 'marker-cluster' + c, iconSize: new L.Point(40, 40) });
  
    }"))) %>%
  addCircleMarkers(data = FILE2,
    color = '#9D7',
    opacity = 1,
    clusterOptions = markerClusterOptions(iconCreateFunction = JS("function (cluster) {    
      var childCount = cluster.getChildCount(); 
      var c = ' marker-cluster-';  
      if (childCount < 3) {  
        c += 'small';  
      } else if (childCount < 5) {  
        c += 'small';  
      } else { 
        c += 'small';  
      }    
      return new L.DivIcon({ html: '<div><span>' + childCount + '</span></div>', 
      className: 'marker-cluster' + c, iconSize: new L.Point(40, 40) });
  
    }")))

示例地图

于 2017-03-29T11:14:08.273 回答