用圆形传单R创建图例 [英] Creating legend with circles leaflet R

查看:71
本文介绍了用圆形传单R创建图例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个点图,其点的大小由变量决定.是否可以用不同大小的圆圈表示不同的变量值来创建图例?我发现了另一篇文章,显示了如何在图例中将正方形转换为圆,但是不确定如何在图例中更改不同圆的大小.

I'm trying to create a leaflet map with points sized by a variable. Is it possible to create a legend with different sized circles representing the different variable values? I found another post showing how to convert squares to circles in the legend, but am not sure how to change the size of different circles in the legend.

例如,这是一个虚拟脚本,该脚本创建与10个点关联的2个变量类别(5和10).我想要一个图例,其两个圆的大小与用addCircleMarkers指定的半径相同,半径分别为5和10.如果有人可以修改以创建我想要的内容,我将非常感激!谢谢!

For example, here's a dummy script which creates 10 points associated with 2 classes of a variable (5 and 10). I'd like a legend with two circles the same size as that specified with addCircleMarkers with a radius of 5 and 10. If anyone can modify to create what I want I would be extremely grateful! Thanks!

library(shiny)
library(leaflet)

#create data
Points<-data.frame(x=runif(10,20,21), y=runif(10,0,1), var=rep(c(5,10),5))
map = leaflet() %>% addTiles()

# Set up shiny app
shinyApp(ui=bootstrapPage(
tags$style(type = "text/css", "html, body {width:100%;height:100%}",
".leaflet .legend i{
border-radius: 50%;
width: 10px;
height: 10px;
margin-top: 4px;
}
"
),
leafletOutput("myMap", width = "100%", height = "100%")),

server= function(input, output){

output$myMap = renderLeaflet({map %>% 
    addCircleMarkers(Points$x,Points$y,radius=Points$var) %>%
    addLegend(colors=rep("blue",2), labels=c(5,10))
  })
})

推荐答案

您很幸运.仔细观察leaflet.js会发现,您在addLegend命令中添加的配置实际上是用来创建图例项的:

You're in luck. A closer look at the leaflet.js shows that the configurations your add inside the addLegend command, are used literally to create the legend items:

leaflet.js开始的第1083至1086行:

Lines 1083 - 1086 from leaflet.js:

for (var i = 0; i < colors.length; i++) {
  legendHTML += '<i style="background:' + colors[i] + ';opacity:' +
                options.opacity + '"></i> ' + labels[i] + '<br/>';
}

这样,我们就可以进行一些额外的样式设计.在colors争论中,我们添加一些widthheight来更改i标记的大小(=图例圆圈).并且(这是可选的)我们可以使labels成为div并具有修改后的对齐样式,因为如果圆变大,它们往往会卡在线的顶部.

This way, we can sneak in some extra styling. Within the colors arguement, we add some width and height to change the size of the i tag (= the legend circle). And (this is optional) we can make the labels a div with modified alignment style, because they tend to be stuck way to the top of the line, if the circle gets big.

我自由地为您创建了一个自定义图例功能.这将为圆的大小增加一个值. (对于这个应用程序,这是一个非常小的功能.)然后,它添加了我上面提到的样式,而无需担心拼写错误或其他错误.请注意,标准尺寸为10.

I took the liberty to create a custom legend function for you. This takes an additional value for the circle sizes. (It's a very minimal function for just this one application.) It then adds the styling I mentioned above without you needing to worry about typos or other errors. Note that the standard size is 10.

下面的代码.玩得开心!如果有任何错误或错误,请写信.我无法针对所有可能的情况进行测试.

Code below. Have fun! And please write if there are any mistakes or bugs. I could not test for every possible scenario.

library(shiny)
library(leaflet)

#create data
Points<-data.frame(x=runif(10,20,21), y=runif(10,0,1), var=rep(c(5,10),5))
map = leaflet() %>% addTiles()

# Set up shiny app
shinyApp(
  ui = bootstrapPage(
    tags$style(type = "text/css", "html, body {width:100%;height:100%}",
      ".leaflet .legend i{
      border-radius: 50%;
      width: 10px;
      height: 10px;
      margin-top: 4px;
      }
    "),
    leafletOutput("myMap", width = "100%", height = "100%")
  ),

  server = function(input, output){
    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))
    }

    output$myMap = renderLeaflet({map %>% 
      addCircleMarkers(Points$x,Points$y,radius=Points$var) %>%
      addLegendCustom(colors = c("blue", "blue", "red"), labels = c("A", "B", "C"), sizes = c(10, 20, 40))
    })
  }
)

这篇关于用圆形传单R创建图例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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