传单渲染不正确的国家 [英] Leaflet rendering incorrect countries

查看:71
本文介绍了传单渲染不正确的国家的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 leaflet 绘制几个国家/地区,但显示的国家/地区不正确.以下是我的最小可重现示例:

 库(传单)图书馆(地图)df<-data.frame(name = c("Afghanistan","Albania","Algeria","Armenia"),代码= c("AFG","ALB","DZA","ARM"),val = c(5,10,15,20),stringsAsFactors = FALSE)pal<-colorNumeric(调色板=蓝调",域= as.numeric(df $ val))标签<-sprintf(< strong>国家/地区:%s</strong> br/>值:%g/",df $ name,df $ val)%>%lapply(htmltools :: HTML)国家=地图(世界",填充= TRUE,地块= FALSE,区域= iso.expand(df $ code,regex = TRUE))宣传单张(国家/地区)%>%addTiles()%&%;%addPolygons(fillOpacity = 0.6,smoothFactor = 0.5,笔画= TRUE,权重= 1,颜色=〜pal(as.numeric(df $ val)),标签=标签) 

我得到以下传单:

如您所见,阿尔及利亚显示为阿尔巴尼亚.如果我从数据中删除亚美尼亚,并绘制传单,我将获得正确的位置.以下是该代码和图像.

 库(传单)图书馆(地图)df<-data.frame(name = c("Afghanistan","Albania","Algeria"),代码= c("AFG","ALB","DZA"),val = c(5,10,15),stringsAsFactors = FALSE)pal<-colorNumeric(调色板=蓝调",域= as.numeric(df $ val))标签<-sprintf(< strong>国家/地区:%s</strong> br/>值:%g/",df $ name,df $ val)%>%lapply(htmltools :: HTML)国家=地图(世界",填充= TRUE,地块= FALSE,区域= iso.expand(df $ code,regex = TRUE))宣传单张(国家/地区)%>%addTiles()%&%;%addPolygons(fillOpacity = 0.6,smoothFactor = 0.5,笔画= TRUE,权重= 1,颜色=〜pal(as.numeric(df $ val)),标签=标签) 

我想念什么吗?

解决方案

我认为问题与 map 对象中的多边形顺序有关(如果您设置了 label = Country $ names ,标签现在正确).无论如何,您可以通过将 Country 转换为 SpatialPolygons 对象来解决该问题(请参见

I am trying to plot few countries using leaflet but the countries that it is showing is incorrect. Following is my minimal reproducible example:

library(leaflet)
library(maps)


  df <- data.frame(name = c("Afghanistan", "Albania" , "Algeria" , "Armenia"),
                   code = c("AFG", "ALB", "DZA", "ARM"),
                    val = c(5, 10, 15, 20), stringsAsFactors = FALSE)

  pal <- colorNumeric(
    palette = "Blues",
    domain = as.numeric(df$val))

  labels <- sprintf(
    "<strong>Country:%s</strong><br/>Value:%g /",
    df$name, df$val)%>% lapply(htmltools::HTML) 

  Country = map("world", fill = TRUE, plot = FALSE, regions=iso.expand(df$code,regex = TRUE))

  leaflet(Country) %>% addTiles() %>%
    addPolygons(fillOpacity = 0.6,  smoothFactor = 0.5, stroke = TRUE, weight = 1, 
                color = ~pal(as.numeric(df$val)),
                label = labels)

I get the following leaflet with this:

As you can see Algeria is shown as Albania. If I remove the Armenia from my data and plot the leaflet I get correct location. Following is the code and image for that.

library(leaflet)
  library(maps)

  df <- data.frame(name = c("Afghanistan", "Albania" , "Algeria" ),
                   code = c("AFG", "ALB", "DZA"),
                    val = c(5, 10, 15), stringsAsFactors = FALSE)

  pal <- colorNumeric(
    palette = "Blues",
    domain = as.numeric(df$val))

  labels <- sprintf(
    "<strong>Country:%s</strong><br/>Value:%g /",
    df$name, df$val)%>% lapply(htmltools::HTML) 

  Country = map("world", fill = TRUE, plot = FALSE, regions=iso.expand(df$code,regex = TRUE))

  leaflet(Country) %>% addTiles() %>%
    addPolygons(fillOpacity = 0.6,  smoothFactor = 0.5, stroke = TRUE, weight = 1, 
                color = ~pal(as.numeric(df$val)),
                label = labels)

Am I missing something?

解决方案

I think the issue is related to the polygons order in map object (if you set label = Country$names, the labels are now correct). Anyway, you can solve the problem by converting Country into a SpatialPolygons object (see here).

library(maptools)
IDs <- sapply(strsplit(Country$names, ":"), function(x) x[1])
Country <- map2SpatialPolygons(Country, 
                               IDs=IDs, 
                               proj4string=CRS("+proj=longlat +datum=WGS84"))

leaflet(Country) %>% addTiles() %>%
  addPolygons(fillOpacity = 0.6,  smoothFactor = 0.5, stroke = TRUE, weight = 1, 
              color = pal(as.numeric(df$val)),
              label = labels)

这篇关于传单渲染不正确的国家的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆