高标称值和百分比值

Pro*_*eus 1 r highcharts pie-chart

我用highcharter库制作了一个饼图。

library(highcharter)

test_data
  Gender  Freq  colors
1 Female 29813 #ff99cc
2   Male 38474 #2980b9

hchart(test_data, "pie", hcaes(x = Gender, y = Freq, color=colors))
Run Code Online (Sandbox Code Playgroud)

由于地图是交互式的,因此我希望饼图在同一图上同时显示百分比值和标称值。

有什么想法可以做到吗?

NpT*_*NpT 5

您需要在工具提示选项中使用带有highcharter的JS()函数的格式化程序。此外,Highchart使用this.point.percentage来提供百分比。确保不要忘记%>%第一行结尾处的。

这应该为您解决问题:

hchart(test_data, "pie", hcaes(x = Gender, label=Gender,y = Freq, color=colors))%>%
Run Code Online (Sandbox Code Playgroud)

hc_tooltip(formatter = JS("function(){ return '<b>' + this.point.label + ': </b>( Frequency:' +this.y+', Percentage: '+Highcharts.numberFormat(this.percentage)+'%)' }"),useHTML = FALSE)

通过添加行(再次在行之前或之后需要magrittr'%>%'): hc_plotOptions(pie =list(dataLabels = list(enabled = TRUE,format="{point.label}:{point.y}")))

您可以在标签上添加值或通过将{point.y}替换为:

{point.percentage:.2f}%,您可以在标签上加上百分比(男,女)