繁体   English   中英

leaflet map 的图例中的圆圈,在 R 中带有 addCircleMarkers - 没有 Z531704A02607A16ZEFFAE

[英]Circles in legend for leaflet map with addCircleMarkers in R - without shiny

I am creating a leaflet map in R - I don't need to build a shiny app, and haven't tackled that particular skill set yet!

我试图跟随这篇文章在我的图例中创建圆圈: 用圆圈创建图例 leaflet R

但是,我不确定如何将tags$style属性合并到我的 R 代码中作为@K。 Rhode 建议确保图例项是圆形。 在我的代码中,图例带有正方形。 很近!

谁能帮我推动这段代码使图例项圈起来?

library(leaflet)
library(dplyr)

#create data
Points<-data.frame(x=runif(10,20,21), y=runif(10,0,1), 
                   var=c(rep(c(10,20, 40),3), 20))
Points <- Points %>% 
  mutate(Category = case_when(var == 10 ~ "A", 
                              var == 20 ~ "B",
                              TRUE ~ "C"),
         color = case_when(Category == "A" ~ "blue",
                           Category == "B" ~ "blue",
                           TRUE ~ "red"))


map = leaflet() %>% 
  addTiles()

addLegendCustom <- function(map, colors, labels, sizes, opacity = 0.5){
  colorAdditions <- paste0(colors, "; width:", sizes, "px; height:", sizes, "px")
  labelAdditions <- paste0("<div style='display: inline-block;height: ", 
                           sizes, "px;margin-top: 4px;line-height: ", sizes, "px;'>", 
                           labels, "</div>")

  return(addLegend(map, colors = colorAdditions, 
                   labels = labelAdditions, opacity = opacity))
}


map %>% 
  addCircleMarkers(Points$x,Points$y,radius=Points$var, 
                   color = Points$color, 
                   stroke = FALSE, fillOpacity = 0.5) %>%
  addLegendCustom(colors = c("blue", "blue", "red"), 
                  labels = c("A", "B", "C"), sizes = c(10, 20, 40))

在传说中,我更喜欢圆形标记......而不是下面的正方形!

在此处输入图像描述

从原始答案中,您省略了一组负责圆形的全局样式定义。

添加border-radius: 50%; 到 css styles 的colorAdditions集,这是您需要的一种样式。

这将使:

  colorAdditions <- paste0(colors, "; border-radius: 50%; width:", sizes, "px; height:", sizes, "px")

有点hacky,但有效。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM