如何为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具有以下功能: github/Leaflet/Leaflet.markercluster#customising-the集群标记
JS Leaflet has this capability: github/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.
在您的示例中,您可以修改默认标记功能(找到此处),只需修改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手册:如何自定义群集的颜色?
发布评论