【问题标题】:Plotting World chloropeth map with leaflet用传单绘制世界等值线图
【发布时间】:2021-09-03 03:14:34
【问题描述】:

我正在尝试绘制世界地图 chloropeth,但标签显示不正确。我不确定问题可能是什么。请参阅下面的可重现代码。一旦我将光标放在美国上,它就会显示另一个标签。

## Source of shape file
# http://thematicmapping.org/downloads/world_borders.php

## Set working directory ## 
## Download the shape files to working directory ##
download.file("http://thematicmapping.org/downloads/TM_WORLD_BORDERS_SIMPL-0.3.zip" , destfile="TM_WORLD_BORDERS_SIMPL-0.3.zip")
## Unzip them ##
unzip("TM_WORLD_BORDERS_SIMPL-0.3.zip")

## OR ## You can directly connect to download link and download to a temp folder as well ##

## Load Required Packages## 
library(leaflet)
library(rgdal) # R 'Geospatial' Data Abstraction Library. Install package if not already installed.


## Load the shape file to a Spatial Polygon Data Frame (SPDF) using the readOGR() function
myspdf = readOGR(dsn=getwd(), layer="TM_WORLD_BORDERS_SIMPL-0.3")
head(myspdf)
summary(myspdf)

# using the slot data
head(myspdf@data)
head(myspdf$NAME, n=220)


#Get my variable
newdff<-c("Ghana", "Grenada", "Guyana", "India", "Jamaica", "Kenya", "United States","Canada")
val<-c(1,2,4,5,5,1000,20000, 100)

df<-data.frame(newdff,val)
df

summary(df)

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

pal <- colorNumeric("OrRd", df$val)
#
total<-subset(myspdf, myspdf$NAME %in% df$newdff)
head(total@data)
## Create map object and add tiles and polygon layers to it
leaflet(data=total) %>% 
  addTiles() %>% 
  addPolygons(fillColor = "green",
              highlight = highlightOptions(weight = 1,
                                           color = "red",
                                           fillOpacity = 0.1,
                                           bringToFront = TRUE),
              label=labels)

【问题讨论】:

    标签: r r-leaflet


    【解决方案1】:

    虽然我不完全理解“val”向量的手动创建,

    在“标签”创建中引用“总”数据框似乎可行。

    ## Source of shape file
    # http://thematicmapping.org/downloads/world_borders.php
    
    ## Set working directory ## 
    ## Download the shape files to working directory ##
    download.file("http://thematicmapping.org/downloads/TM_WORLD_BORDERS_SIMPL-0.3.zip" , destfile="TM_WORLD_BORDERS_SIMPL-0.3.zip")
    ## Unzip them ##
    unzip("TM_WORLD_BORDERS_SIMPL-0.3.zip")
    
    ## OR ## You can directly connect to download link and download to a temp folder as well ##
    
    ## Load Required Packages## 
    library(leaflet)
    library(rgdal) # R 'Geospatial' Data Abstraction Library. Install package if not already installed.
    
    
    ## Load the shape file to a Spatial Polygon Data Frame (SPDF) using the readOGR() function
    myspdf = readOGR(dsn=getwd(), layer="TM_WORLD_BORDERS_SIMPL-0.3")
    head(myspdf)
    summary(myspdf)
    
    # using the slot data
    head(myspdf@data)
    head(myspdf$NAME, n=220)
    
    
    #Get my variable
    newdff<-c("Ghana", "Grenada", "Guyana", "India", "Jamaica", "Kenya", "United States","Canada")
    val<-c(1,2,4,5,5,1000,20000, 100)
    
    df<-data.frame(newdff,val)
    df
    
    summary(df)
    
    labels <- sprintf(
      "<strong>Country:%s</strong><br/>Population:%g",
      df$newdff, df$val)%>% lapply(htmltools::HTML) 
    
    pal <- colorNumeric("OrRd", df$val)
    #
    total<-subset(myspdf, myspdf@data$NAME %in% df$newdff)
    
    labels <- sprintf(
      "<strong>Country:%s</strong><br/>Population:%g",
      total$NAME, df$val)%>% lapply(htmltools::HTML) 
    
    head(total@data)
    ## Create map object and add tiles and polygon layers to it
    leaflet(data=total) %>% 
      addTiles() %>% 
      addPolygons(fillColor = "green",
                  highlight = highlightOptions(weight = 1,
                                               color = "red",
                                               fillOpacity = 0.1,
                                               bringToFront = TRUE),
                  label=labels)
    

    【讨论】:

    • 非常感谢,苏珊!那行得通!看来 val 向量值也没有正确显示在标签中。我不确定可能出了什么问题。
    • 而不是将值放在独立向量中。我喜欢将值添加到数据框中包含名称的数据框中,并使用 sp 包中的 merge() 将 df 加入到 spatialdf
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-14
    相关资源
    最近更新 更多