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

查看:86
本文介绍了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集群标记

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.

在您的示例中,您可以修改默认标记功能(找到此处上找到为标记着色的默认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天全站免登陆