Leaflet Awesome Markers 图标未显示在 R 中

Leaflet Awesome Markers icon isn't display in R

这是我的数据集:

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))

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

编辑:

在Kaggle中使用R的信息是asker后来分享的。使用 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 <- 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))