Rich
Rich

Reputation: 141

How to display total and percentage in pie chart using highchart in R?

I am trying to prepare a dashboard in R using Rmarkdown. I used below code to display pie chart in the dashboard but how do I display total and percentage

hc <- df %>%
  hchart(
    "pie", hcaes(x = name, y = count))
hc

My output is just a pie chart with name but no number or percentage.

I want output something like this

enter image description here

Thanks for any help.

Upvotes: 0

Views: 1172

Answers (1)

magdalena
magdalena

Reputation: 3695

Use tooltip.pointFormat (or tooltip.format,tooltip.formatter) and dataLabels.format (or dataLabels.formatter). In this is case:

hc_tooltip(pointFormat= "{series.name}: <br>{point.percentage:.1f} %<br>total: {point.total}"),

API Reference: https://api.highcharts.com/highcharts/tooltip.pointFormat

hc_plotOptions(series = list(
  dataLabels =  list(format = "<b>{point.name}</b>:<br>{point.percentage:.1f} %<br>total: {point.total}")
        ))

API Reference: https://api.highcharts.com/highcharts/plotOptions.series.dataLabels.format

JS DEMO: http://jsfiddle.net/BlackLabel/sry5mvzh/

Upvotes: 1

Related Questions