【问题标题】:Global Leaflet Map in R - issues adding data to spatial objectR中的全局传单地图-向空间对象添加数据的问题
【发布时间】:2022-11-13 03:17:57
【问题描述】:

我正在尝试复制这种视觉效果,但使用我自己的数据。这是我正在使用的模板 - https://r-graph-gallery.com/183-choropleth-map-with-leaflet.html

我的意图是用相同的颜色突出显示每个国家/地区的价值。我可能会把它做成热图或其他东西——但现在添加多边形会出错,所以我根本无法尝试任何颜色选项。

# Setup
library(leaflet)
library(rgdal)
library(here)
library(tidyverse)

# Basically copy pasted from the template, but the download did not work. I manually went to the website, downloaded the file, manually un-zipped, and manually dropped it in my working directory
# download.file("http://thematicmapping.org/downloads/TM_WORLD_BORDERS_SIMPL-0.3.zip" , destfile="DATA/world_shape_file.zip")
# system("unzip DATA/world_shape_file.zip")
world_spdf <- readOGR( 
  dsn= here() , 
  layer="TM_WORLD_BORDERS_SIMPL-0.3",
  verbose=FALSE
)

world_spdf@data$POP2005[ which(world_spdf@data$POP2005 == 0)] = NA
world_spdf@data$POP2005 <- as.numeric(as.character(world_spdf@data$POP2005)) / 1000000 %>% round(2)

# Example of my data - I have countries and numbers associated with them, although not every country has a number

country <- c("Algeria", "Argentina", "Australia")
values <- c(1,4,4)

my_df <- dataframe(country, values)

# This is how I am trying to add MY values to the map. I have to convert the map to a tibble, add my data, then convert it back to a map. Perhaps this is the problem? 
interactive_data_attempt <- world_spdf %>% 
  as.tibble() %>% 
  left_join(my_df , by = c("NAME" = "country")) %>% 
  mutate(texts = replace_na(texts, 0),
         exists = texts > 1) %>% 
  st_as_sf(coords = c("LON","LAT"))

# This is the method I used to do the exact same thing in a domestic US map
bins <- c(seq(0,1,1), Inf)
pal <- colorBin(c("white","#C14A36"), domain = interactive_data_attempt$exists, bins = bins, reverse = FALSE)

# This gives an error: Error in to_ring.default(x) : Don't know how to get polygon data from object of class XY,POINT,sfg
leaflet(interactive_data_attempt) %>% 
  addTiles() %>% 
  setView(lat=10, lng=0 , zoom=2) %>%
  addPolygons(fillColor = ~pal(interactive_data_attempt$exists))

【问题讨论】:

    标签: r leaflet


    【解决方案1】:

    您使用readOGR 来获取一个 sp 对象,但有一次您将其转换为 tibble 然后再转换为 sf?不确定 sp,但在大多数情况下,您可以将 sf 作为常规 tibble / dataframe 处理,即 left_jointo 它。您可以使用st_read 直接将 shapefile 读取到 sf。

    然后你的变量有一些东西,我猜是复制粘贴的混合:在my_df中你有values但你从来没有用它做任何事情,在你的mutate中你使用texts但不清楚它来自哪里从。

    二进制调色板是从 exists 构建的,这是一个布尔值,应该指示实际值是否存在,尽管我假设您希望使用 my_df$values 中的值。

    原样保留 NA 值,更改 bin(仅 2 个)并调整一些颜色。

    library(leaflet)
    library(sf)
    library(dplyr)
    library(tidyr)
    
    # download.file("http://thematicmapping.org/downloads/TM_WORLD_BORDERS_SIMPL-0.3.zip" , destfile="world_shape_file.zip")
    # unzip("world_shape_file.zip",exdir = "world_shape_file")
    world_sf <- st_read("world_shape_file")
    
    world_sf$POP2005[ which(world_sf$POP2005 == 0)] = NA
    world_sf$POP2005 <- as.numeric(as.character(world_sf$POP2005)) / 1000000 %>% round(2)
    
    country <- c("Algeria", "Argentina", "Australia")
    values <- c(1,4,4)
    
    pal <- colorBin(c("blue","#C14A36"), domain = values, bins = 2, reverse = FALSE, na.color = "transparent")
    
    world_sf %>% 
      left_join(
        tibble(country, values), 
        by = c("NAME" = "country")) %>% 
      leaflet() %>% 
      addTiles() %>% 
      setView(lat=10, lng=0 , zoom=2) %>%
      addPolygons(fillColor = ~pal(values), stroke = FALSE)
    

    创建于 2022-11-12,reprex v2.0.2

    【讨论】:

      猜你喜欢
      • 2018-08-24
      • 2018-08-10
      • 2017-07-29
      • 1970-01-01
      • 2020-09-18
      • 2022-11-19
      • 1970-01-01
      • 1970-01-01
      • 2012-08-08
      相关资源
      最近更新 更多