使用awesomeIcons更改图标颜色

时间:2018-06-11 09:25:15

标签: r icons leaflet

我正在使用awesomeIcons(),在那里我找到了一个医院图标('hospital-o')。但是,图标变成了黑色。使用下面的代码,我无法将其颜色更改为白色。

library(dplyr)
library(DT)
library(flexdashboard)
library(leaflet)
library(leaflet.extras)
library(reshape2)
library(shiny)

icons <- awesomeIcons(
          icon = 'hospital-o',
          lib = 'fa',
          markerColor = getColor(DATASET))

其中getColor()是以下函数:

  getColor <-
  function(DATASET) {
    sapply(DATASET$VAR, function(VAR) {
    if(VAR == 0 | is.na(VAR) | is.nan(VAR)) {"gray"}
    else if(VAR <= 2){"darkgreen"}
    else if(VAR <= 4){"green"}
    else if(VAR <= 6){"orange"}
    else if(VAR <= 10){"red"}
    else {"darkred"}
    })
  }

颜色功能正常工作(即灰色,绿色,红色,标记)。我尝试了类似的东西(并且还实现了颜色代码),没有结果:

icons <- awesomeIcons(
          icon = 'hospital-o',
          lib = 'fa',
          iconColor = "white",
          markerColor = getColor(DATASET))

虚拟数据

NAME    VAR latitude    longitude
A   1   51.792  4.682
B   12  52.214  6.894
C   6   51.982  5.148

制作地图:

map <- leaflet(DATASET) %>%          
addTiles() %>% 
addAwesomeMarkers(lng = ~longitude, lat = ~latitude, icon = icons, label = ~as.character(DATASET$NAME),
                popup = paste0("<strong>Name: </strong>", DATASET$NAME, "<br>","<strong>VAR: </strong>", DATASET$VAR)) %>% 
     addMarkers(lng = longitude, lat = latitude) %>%
addProviderTiles(providers$nlmaps.pastel, group = "Pastel") %>%
addMiniMap(toggleDisplay = T, minimized = T, position = "bottomright") %>%
addLayersControl(
  baseGroups = c("Pastel"), 
  options = layersControlOptions(collapsed = F), position = "topleft") %>%
       addMarkers(lng = longitude, lat = latitude)

有人能帮助我吗?提前谢谢!

1 个答案:

答案 0 :(得分:0)

iconColor = "#FFFFFF"中实施awesomeIcons()就可以了!所以代码看起来像这样:

  icons <- awesomeIcons(
  icon = 'hospital-o',
  lib = 'fa',
  iconColor = "#FFFFFF",
  markerColor = getColor(DATASET))

我以为我已经尝试过了。谢谢,MLavoie!