在R中使用Highcharter实现饼图同时显示标签名称与百分比
解决Highcharter饼图同时显示名称与百分比并置于切片内部的问题
要同时显示数据点名称和百分比,只需修改dataLabels的format参数,将名称和百分比变量组合进去;要把标签放在切片内部,添加position = "inside"参数即可。
完整示例代码
library(tidyverse) library(highcharter) df = tibble( id = c(1,2,3,4,5), name = c('John','Kenneth','Aida','Ronda','Jasmine'), value = c(0.2,0.35,0.1,0.3,0.05) ) df %>% hchart('pie', hcaes(name, value)) %>% hc_plotOptions(pie = list( dataLabels = list( enabled = TRUE, # 组合名称与百分比,用换行分隔(也可改为空格拼接成一行) format = '{point.name}<br>{point.percentage:.1f}%', # 将标签置于切片内部 position = "inside" ) ))
关键参数说明
format = '{point.name}<br>{point.percentage:.1f}%':通过{point.name}调用数据点名称,{point.percentage:.1f}%设置保留一位小数的百分比格式;<br>用于换行分隔两者,若想放在同一行,直接替换为空格即可。position = "inside":将标签定位在饼图切片内部,默认标签位置为"outside"。
内容的提问来源于stack exchange,提问作者James
相关产品推荐
相关产品推荐

