R 的传单:如何自定义集群的颜色? [英] Leaflet for R: How to customize the coloring of clusters?

查看:16
本文介绍了R 的传单:如何自定义集群的颜色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何自定义 R 的传单包中的 addMarkers 函数的颜色?

How do I customize the coloring of the addMarkers function in the leaflet package for R?

簇的默认颜色是:

  • 1-10 绿色
  • 11-100 黄色
  • 100+ 红色

我想将范围和颜色更改为:

I'd like to change the ranges and colors to something like:

  • 1-100 红色
  • 101-1000 黄色
  • 1000+ 绿色

JS Leaflet 具有以下功能:https://github.com/Leaflet/Leaflet.markercluster#customising-the-clustered-markers

JS Leaflet has this capability: https://github.com/Leaflet/Leaflet.markercluster#customising-the-clustered-markers

这可以通过 R 包中的 markerClusterOptions 参数实现吗?

Is this possible through a markerClusterOptions parameter in the R package?

leaflet(quakes) %>% addTiles() %>% addMarkers(
  clusterOptions = markerClusterOptions()
)

推荐答案

您可以使用 markerClusterOptions 中的 iconCreateFunction 创建自己的自定义图标函数来显示集群标记.

You can use the iconCreateFunction in the markerClusterOptions to create your own custom icon function to display the cluster markers.

在您的示例中,您可以只修改默认标记函数(找到 here),只需修改 if/else 循环设置标记的 CSS 类.可以在此处找到为标记着色的默认 CSS.如果您想要更多的自定义,您可以创建自己的类.

In your example, you can maybe just modify the default marker function (found here) and just modify the if/else loops setting the CSS class of the markers. The default CSS that colors the markers can be found here. You can create your own classes if you want more customisation.

这是一个代码示例(大是红色,中是黄色,小是绿色,所以我只是切换了默认代码以匹配您的条件):

Here's a code example (large is red coloring, medium is yellow, and small is green so I just switched the default code around to match your conditions):

library(leaflet)
leaflet(quakes) %>% addTiles() %>% addMarkers(
  clusterOptions = markerClusterOptions(iconCreateFunction=JS("function (cluster) {    
    var childCount = cluster.getChildCount(); 
    var c = ' marker-cluster-';  
    if (childCount < 100) {  
      c += 'large';  
    } else if (childCount < 1000) {  
      c += 'medium';  
    } else { 
      c += 'small';  
    }    
    return new L.DivIcon({ html: '<div><span>' + childCount + '</span></div>', className: 'marker-cluster' + c, iconSize: new L.Point(40, 40) });

  }"))
)

这篇关于R 的传单:如何自定义集群的颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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