【问题标题】:Leaflet Awesome Markers icon isn't display in R传单真棒标记图标未显示在 R 中
【发布时间】:2022-01-04 08:37:00
【问题描述】:

这是我的数据集:

start_stations <-
  data.frame(
    station = c("StreeterDr", "MichiganAve", "WellsSt"),
    lat = c(41.89228, 41.90096, 41.91213),
    lng = c(-87.61204,-87.62378,-87.63466),
    n = c(23000, 56780, 34520)
  )

这是我尝试使用这些 lat 和 lng 坐标绘制地图并根据其计数为站点(位置)添加颜色变化并使用名称和计数标记每个位置的代码。

install.packages(c("leaflet", "sp")) 
library(leaflet)
library(sp)
install.packages("sf")
library(sf)

lon <- start_stations$lng
lat <- start_stations$lat
name <- start_stations$station
count <- start_stations$n
dfs <- as.data.frame(cbind(lon,lat,name,count))

dfs <- sf::st_as_sf(dfs, coords = c("lon","lat"), crs = 4326)

getColor <- function(dfs) {
  sapply(dfs$count, function(count) {
  if(count <= 20000) {
    "green"
  } else if(count <= 30000) {
    "orange"
  } else {
    "red"
  } })
}

icons <- awesomeIcons(
  icon = 'ios-close',
  iconColor = 'black',
  library = 'ion',
  markerColor = getColor(dfs)
)

leaflet(dfs) %>% addTiles() %>%
  addAwesomeMarkers(~lon, ~lat, icon=icons, popup = ~as.character(name), label=~as.character(count))

除了显示图标外,一切正常。我想用与计数相对应的颜色显示图标。

【问题讨论】:

    标签: r filtering ggmap markers r-leaflet


    【解决方案1】:

    编辑:

    在 Kaggle 中使用 R 的信息后来被提问者分享。在使用 Kaggle 时,使用 addAwesomeMarkers 会出现问题。其他标记,例如addCircleMarkers 运行良好。


    如下图所示,以下代码适用于 Windows 10 上的以下环境。

    • RStudio 2021.09.0 Build 351 © 2009-2021 RStudio, PBC
    • 最新版本的软件包 sf 和 sp
    # ----------------------------------------------------------------------
    # sample for awesomeIcons - color by value
    # ----------------------------------------------------------------------
    
    start_stations <-
      data.frame(
        station = c("StreeterDr", "MichiganAve", "WellsSt"),
        lat = c(41.89228, 41.90096, 41.91213),
        lng = c(-87.61204,-87.62378,-87.63466),
        n = c(23000, 56780, 34520)
      )
    
    library(leaflet)
    library(sp)
    library(sf)
    
    lon <- start_stations$lng
    lat <- start_stations$lat
    name <- start_stations$station
    count <- start_stations$n
    dfs <- as.data.frame(cbind(lon,lat,name,count))
    
    dfs <- sf::st_as_sf(dfs, coords = c("lon","lat"), crs = 4326)
    
    # --- character to integer -----------------------------------------------------
    dfs$count <- as.integer(start_stations$n) 
    
    getColor <- function(dfs) {
      sapply(dfs$count, function(count) {
        if(count <= 25000) {
          "green"
        } else if(count <= 35000) {
          "orange"
        } else {
          "red"
        } })
    }
    
    icons <- awesomeIcons(
      icon = 'ios-close',
      iconColor = 'black',
      library = 'ion',
      markerColor = getColor(dfs)
    )
    
    leaflet() %>% addTiles() %>%
      addAwesomeMarkers(data=dfs, ~lon, ~lat, icon=icons, popup = ~as.character(name), label=~as.character(count))
    

    请注意getColor &lt;- function(dfs) 中的以下编辑行和一些编辑值

    # --- character to integer -------------------------------------------------
    dfs$count <- as.integer(start_stations$n) 
    
    ...
    
    leaflet() %>% addTiles() %>%
      addAwesomeMarkers(data=dfs, ~lon, ~lat, icon=icons, popup = ~as.character(name), label=~as.character(count))
    

    或使用其他解决方案来满足您的需求,如下所示:

    library(dplyr) # add for use of mutate
    
    # --- character to integer -------------------------------------------------
    dfs$count <- as.integer(start_stations$n) 
    
    # --- add color group column -----------------------------------------------
    dfs <- mutate(dfs, group = cut(count, breaks = c(0, 25000, 35000, 99000, Inf),
                                  labels = c("green", "darkred", "red", "purple"),  include.lowest = TRUE))
    dfs
    
    icons <- awesomeIcons(icon = "ios-close",
                          iconColor = "yellow",
                          library = "ion",
                          markerColor = dfs$group)
    
    leaflet() %>% addTiles() %>%
      addAwesomeMarkers(data=dfs,~lon, ~lat, icon=icons, popup = ~as.character(name), label=~as.character(count))
    

    【讨论】:

    • 我尝试了这两个代码。但是位置图标仍然没有出现。
    • @KalsaraL - 查看我的编辑(版本信息和library(dplyr) # add for use of mutate)。变异功能需要library(dplyr)。您安装的软件似乎有问题。另外,回答您的其他问题在 SO (stackoverflow.com/a/70119845/1981088) 上显然不适合你。
    • @help-info.de- 我使用 Kaggle 笔记本来执行此代码。我也认为是版本问题而不是代码问题。我该如何克服这个问题。谢谢!
    • @help-info.de-Leaflet Markers 中的图标是否需要额外的库
    • @KalsaraL - AFAIK noo 附加库。查看我的编辑。
    猜你喜欢
    • 2019-01-14
    • 2017-09-06
    • 2018-12-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-06
    • 1970-01-01
    相关资源
    最近更新 更多