R瓣叶中具有特定颜色的仅标签标记

tct7dpnv  于 12个月前  发布在  其他
关注(0)|答案(1)|浏览(80)

我想建立一个leafletMap与CircleMarkersLabelOnlyMarkers顶部的圆圈。标签和圆圈应该有颜色指定在一个data.frame列。
下面的代码做了我想要做的事情,除了LabelOnlyMarkers.的颜色。如何更改labelOptions以正确的颜色绘制标签?
或者有更简单的解决方案吗?
以下是我的尝试:

  • 部分style = list("color" = data$col)))没有动态定义颜色,但似乎被忽略了。*
library(leaflet)

data <- data.frame(lat = c(47, 48, 47), 
                   lng = c(8, 9, 10),
                   label = c("bli", "bla", "blo"),
                   col = c("#E69F00", "#56B4E9", "#009E73"),
                   stringsAsFactors = FALSE)

leaflet()  %>%
  addProviderTiles(providers$OpenStreetMap.CH, group = "OSM") %>%
  addCircleMarkers(lng = data$lng, 
                   lat = data$lat, 
                   fillColor = data$col, 
                   radius = 5, opacity = 0, fillOpacity = 1) %>%
  addLabelOnlyMarkers(lng = data$lng, 
                      lat = data$lat, 
                      label = data$label,
                      labelOptions = labelOptions(
                        noHide = T, direction = 'top', 
                        textOnly = T, textsize = "20px", 
                        style = list("color" = data$col)))

字符串
这产生:
x1c 0d1x的数据
.这是-除了黑色LabelOnlyMarkers-正是我想要的.

s2j5cfk0

s2j5cfk01#

引用this SO post

# apply label w/ color as HTML
data <- data |>
    dplyr::mutate(
        lab_html = purrr::map(
            glue::glue(
                "<span style='color:{data$col}'>{as.character(data$label)}<span>"
            ),
            htmltools::HTML
        )
    )

# map
leaflet() %>%
    addProviderTiles(providers$OpenStreetMap.CH, group = "OSM") %>%
    addCircleMarkers(
        lng = data$lng,
        lat = data$lat,
        fillColor = data$col,
        radius = 5, opacity = 0, fillOpacity = 1
    ) %>%
    addLabelOnlyMarkers(
        lng = data$lng,
        lat = data$lat,
        label = data$lab_html,
        labelOptions = labelOptions(
            noHide = TRUE, direction = "top",
            textOnly = TRUE, textsize = "20px"
        )
    )

字符串
产量:
x1c 0d1x的数据
希望能帮上忙!

相关问题