微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

R 传单中的群集选项 pi 图表

如何解决R 传单中的群集选项 pi 图表

我想在这个问题中做同样的事情:

How to change clusterOptions to display pie charts in R leaflet?

我复制粘贴并修改代码,它运行良好,除了一件事,当聚集的点数太大时,饼图变成“饼图”,如下图所示。

enter image description here

如何调整代码?请注意,我认为,但我不确定是因为我有 11 个组,还是因为某些代码限制了圆的大小,因此它变成了正方形而不是圆。

这是我正在使用的 JS 函数,除了我在开始时更改了组和颜色变量外,它完全相同:

jsscript3<-
  paste0(
    "function(cluster) {
const groups= [",paste("'",levels(factor(pts$Sector)),"'",sep="",collapse=","),"];
const colors= {
groups: [",beatCol(levels(factor(pts$Sector))),"],center:'#ddd',text:'black'
};
const markers= cluster.getAllChildMarkers();

const proportions= groups.map(group => markers.filter(marker => marker.options.group === group).length / markers.length);
function sum(arr,first= 0,last) {
return arr.slice(first,last).reduce((total,curr) => total+curr,0);
}
const cumulativeProportions= proportions.map((val,i,arr) => sum(arr,i+1));
cumulativeProportions.unshift(0);

const width = 2*Math.sqrt(markers.length);
const radius= 15+width/2;

const arcs= cumulativeProportions.map((prop,i) => { return {
x   :  radius*Math.sin(2*Math.PI*prop),y   : -radius*Math.cos(2*Math.PI*prop),long: proportions[i-1] >.5 ? 1 : 0
}});
const paths= proportions.map((prop,i) => {
if (prop === 0) return '';
else if (prop === 1) return `<circle cx='0' cy='0' r='${radius}' fill='none' stroke='${colors.groups[i]}' stroke-width='${width}' stroke-alignment='center' stroke-linecap='butt' />`;
else return `<path d='M ${arcs[i].x} ${arcs[i].y} A ${radius} ${radius} 0 ${arcs[i+1].long} 1 ${arcs[i+1].x} ${arcs[i+1].y}' fill='none' stroke='${colors.groups[i]}' stroke-width='${width}' stroke-alignment='center' stroke-linecap='butt' />`
});

return new L.DivIcon({
html: `
<svg width='60' height='60' viewBox='-30 -30 60 60' style='width: 60px; height: 60px; position: relative; top: -24px; left: -24px;' >
<circle cx='0' cy='0' r='15' stroke='none' fill='${colors.center}' />
<text x='0' y='0' dominant-baseline='central' text-anchor='middle' fill='${colors.text}' font-size='15'>${markers.length}</text>
${paths.join('')}
</svg>
`,className: 'marker-cluster'
});
}")

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。