Prometheus
Prometheus

Reputation: 2017

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?

Upvotes: 2

Views: 2651

Answers (3)

Collei Inc.
Collei Inc.

Reputation: 76

You can display the nominal value by using point.y:

tooltip: {
    pointFormat: '<b>Value:</b> {point.y:.0f}<br><b>Percentage: {point.percentage:.2f}%</b>'
},

Upvotes: 0

vorjuelal
vorjuelal

Reputation: 43

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}%")

Upvotes: 3

NpT
NpT

Reputation: 451

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)

Upvotes: 6

Related Questions