问题
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