如何在与国家/地区R传单对应的地图多边形上指定弹出窗口

时间:2018-03-27 11:56:01

标签: r leaflet maps r-leaflet

我正在尝试编写一个允许人们点击某个国家/地区的脚本,然后会出现一个弹出窗口并显示该国家/地区的名称和相应的值。我已成功创建了地图和每个国家的边界​​,但是当您点击时,例如俄罗斯,弹出窗口将显示印度。以下是该脚本的简化版本:

library(leaflet)
library(maps)

countries <- c("Australia", "South Africa", "India", "Mexico", "USA", "Russia")

values <- c(1,2,3,4,5,6)

bounds <- map("world", countries, fill = TRUE, plot = FALSE)


map <- leaflet(data) %>%
       addTiles() %>%
       addPolygons(data = bounds, group = "Countries", 
                   color = "red", 
                    weight = 2,
                    popup = paste("Country: ", countries, "<br>", "Value: ", values, "<br>"),
                    fillOpacity = 0.1,
                    highlightOptions = highlightOptions(color = "black", 
                                                        weight = 2,
                                                        bringToFront = TRUE))

map

地图是使用R中的leaflet包生成的。任何解决方案或建议都是受欢迎的,我确信这是一个简单的错误,我正在某处。

1 个答案:

答案 0 :(得分:0)

我为你留下两个选择。如果您只想显示bounds中的国家/地区名称(例如澳大利亚:梅尔维尔岛),则需要将popup = countries更改为popup = ~names

map1 <- leaflet() %>%
        addProviderTiles("OpenStreetMap.Mapnik") %>%
        addPolygons(data = bounds, group = "Countries", 
                    color = "red", 
                    weight = 2,
                    popup = ~names,
                    fillOpacity = 0.1,
                    highlightOptions = highlightOptions(color = "black", 
                                                        weight = 2,
                                                        bringToFront = TRUE))

enter image description here

如果您想拥有countries中指定的国家/地区名称,则需要使用names来操纵gsub()。在这里,我删除了每个名称从:到结尾的所有字符。

map2 <- leaflet() %>%
        addProviderTiles("OpenStreetMap.Mapnik") %>%
        addPolygons(data = bounds, group = "Countries", 
                    color = "red", 
                    weight = 2,
                    popup = ~gsub(x = names, pattern = ":.*$", replacement = ""),
                    fillOpacity = 0.1,
                    highlightOptions = highlightOptions(color = "black", 
                                                        weight = 2,
                                                        bringToFront = TRUE))

enter image description here

<强> EXTRA

OP在他的问题上又增加了一件事。这是我的想法。如果要在弹出窗口中显示两项内容,则可以执行以下操作。您将value添加到bounds并创建弹出窗口。

# Add values to bounds. 

set.seed(111)
bounds$value <- sample.int(n = 1000, size = 301, replace = TRUE)

map3 <- leaflet() %>%
        addProviderTiles("OpenStreetMap.Mapnik") %>%
        addPolygons(data = bounds, group = "Countries", 
                    color = "red", 
                    weight = 2,
                    popup = paste("Country: ", bounds$names, "<br>",
                                  "Value: ", bounds$value, "<br>"),
                    fillOpacity = 0.1,
                    highlightOptions = highlightOptions(color = "black", 
                                                        weight = 2,
                                                        bringToFront = TRUE))

enter image description here