首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >R小叶中的饼图,将计数转换为总和,并控制更大的大小

R小叶中的饼图,将计数转换为总和,并控制更大的大小
EN

Stack Overflow用户
提问于 2021-11-08 08:08:58
回答 1查看 168关注 0票数 6

我对将标记分组并以小片段图https://stackoverflow.com/a/60525137/3480717的形式按组显示计数的解决方案着迷,我只是R,不知道JS。我希望代码对每个数据点的值进行求和,而不是计数(每个单独的数据点可能已经代表了一个计数)。我想要更多地控制气泡的大小,依赖于这个值。你能帮我展示一下如何修改js代码,使其对数据点的值进行求和,以及如何增加/控制气泡的大小吗?

这里有一个合计而不是计数的非饼形标记的解决方案:How to display the value (sum) rather than count of markers in a dc.leaflet.js这里有一个也可以控制气泡大小的解决方案:Clustered leaflet markers with sum (not count) totals: how to get consistent round red shape and label format like in unclustered markers

原始代码由https://stackoverflow.com/users/2711712/danielbonnery @DanielBonnery提供

代码语言:javascript
复制
library(leaflet)
library(dplyr)
#Creates data
data("breweries91",package="leaflet")
#set.seed(1);
breweries91$goodbear<-sample(as.factor(c("terrific","marvelous","culparterretaping")),nrow(breweries91),replace=T)
#Colors
joliepalette<-c("red","green","blue")[1:nlevels(breweries91$goodbear)]
getColor <- function(breweries91) {joliepalette[breweries91$goodbear]}

icons <- awesomeIcons(
  icon = 'ios-close',
  iconColor = 'black',
  library = 'ion',
  markerColor = getColor(breweries91)
)

#Generate the javascript

jsscript3<-
  paste0(
"function(cluster) {
const groups= [",paste("'",levels(breweries91$goodbear),"'",sep="",collapse=","),"];
const colors= {
groups: [",paste("'",joliepalette,"'",sep="",collapse=","),"],
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, 0, 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'
});
}")

# Generates the map.
leaflet() %>%
  addTiles() %>%
  addAwesomeMarkers(data=breweries91,
                    group=~goodbear,
                    icon = icons,
                    clusterOptions = markerClusterOptions(
                      iconCreateFunction =
                        JS(jsscript3)))
EN

回答 1

Stack Overflow用户

发布于 2021-11-16 05:36:44

请注意这一行代码

代码语言:javascript
复制
const arcs= cumulativeProportions.map((prop, i) => { return {

据我所知,使用SVG创建的paths的大小由prop的值控制,我猜它的意思是“比例”。

我将prop复制到vim中,并搜索设置javascript的位置。在我看来,prop从来没有被赋值。

他们一直告诉我,保持事物的“比例”总是有帮助的。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69880307

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档