简体   繁体   中英

highcharter both nominal and percentage values

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?

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)

hchart(test_data, "pie", hcaes(x = Gender, y = Freq, color=colors)) %>% 
      hc_tooltip(pointFormat = "<b>Value:</b> {point.y} <br>
                 <b>Percentage</b> {point.percentage:,.2f}%")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM