图例中的圆圈用于传单地图,R中具有addCircleMarkers-无光泽 [英] Circles in legend for leaflet map with addCircleMarkers in R - without shiny

查看:601
本文介绍了图例中的圆圈用于传单地图,R中具有addCircleMarkers-无光泽的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在R中创建传单地图-我不需要构建闪亮的应用程序,并且还没有解决该特定技能!

我正尝试按照此处的这篇文章在我的传奇中创建圈子: 使用圆环传单R创建图例. /p>

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

任何人都可以帮助我将这段代码微动以使图例项变成圆圈吗?

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

在图例中,我更喜欢圆形标记...而不是如下所示的正方形!

解决方案

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

colorAdditions套css样式中添加border-radius: 50%;,这是您需要的样式之一.

这将使:

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

有点笨拙,但可以.

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!

I was trying to follow along with this post here to create circles in my legend: Creating legend with circles leaflet R .

However, I'm not sure how to incorporate the tags$style attribute to my R code as @K. Rhode suggested to ensure the legend items are circles. In my code, the legend comes up with squares. So close!

Can anyone help me nudge this code into making the legend items circles?

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

In the legend I'd prefer circle markers... not squares as below!

解决方案

From the original answer, you omitted one set of global style definition that is responsible for the round shape.

Add border-radius: 50%; to the colorAdditions set of css styles, which is the one style you are needing from it.

This would make:

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

A bit hacky, but works.

这篇关于图例中的圆圈用于传单地图,R中具有addCircleMarkers-无光泽的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆