highcharter both nominal and percentage values

試著忘記壹切 提交于 2019-12-11 05:55:21

问题


I've made a pie chart with the highcharter library.

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))

As the map is interactive, I want to have the pie chart showing both percentage values and nominal values on the same plot.

Any ideas how I can do this?


回答1:


You need to use a formatter with JS() function of highcharter, in the tooltip options. Moreover Highchart uses the this.point.percentage to provide the percentages. Make sure not to forget the %>% at the end of the first line.

This should do the trick for you:

hchart(test_data, "pie", hcaes(x = Gender, label=Gender,y = Freq, color=colors))%>%

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

by adding the line (again the magrittr '%>%' is needed before or after the line): hc_plotOptions(pie =list(dataLabels = list(enabled = TRUE,format="{point.label}:{point.y}")))

you can add on the labels the values or by exchanging the {point.y} with the:

{point.percentage:.2f}%, you can add the percentage along with the label (Male,Felame)



来源:https://stackoverflow.com/questions/44680182/highcharter-both-nominal-and-percentage-values

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!