Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Creating a Donut Chart in R using Highcharts

I am trying to create a donut chart with some interactive-ness in R.

I have achieved a pie chart like below:

highchart() %>% 
  hc_add_series(type = "pie", data = age, hcaes(Age, Mean_percentage_viewed), 
                size = "60%", name = "Mean Percentage viewed", center = c(50, 50), 
                dataLabels = list(distance = -50, 
                                  formatter = JS("function () {
                                                  return this.y > 5 ? this.point.name : null;
  }"))) %>% 
  hc_title(text = "Age group share")

enter image description here

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Is there a way to convert it into a donut chart? I could find other examples that could transform into a donut chart.

> dput(age)
structure(list(Age = c("13–17 years", "18–24 years", "25–34 years", 
"35–44 years", "45–54 years", "55–64 years"), Views = c(0.38, 
70.01, 20.2, 6.91, 2.37, 0.13), Mean_view_duration = c("0:01:51", 
"0:02:25", "0:02:42", "0:02:58", "0:02:57", "0:02:23"), Mean_percentage_viewed = c(21.71, 
21.23, 24.31, 26.36, 24.94, 19.27), Watch.Time.Hours = c(0.28, 
67.14, 21.58, 8.11, 2.76, 0.12)), class = "data.frame", row.names = c(NA, 
-6L))

>Solution :

You could use an innerSize with hc_plotOptions like this:

library(highcharter)
library(dplyr)
highchart() %>% 
  hc_add_series(type = "pie", data = age, hcaes(Age, Mean_percentage_viewed), 
                size = "60%", name = "Mean Percentage viewed", center = c(50, 50), 
                innerSize="50%",
                dataLabels = list(distance = -50, 
                                  formatter = JS("function () {
                                                  return this.y > 5 ? this.point.name : null;
  }"))) %>% 
  hc_title(text = "Age group share") %>%
  hc_plotOptions(
    innersize="50%", 
    startAngle=90, 
    endAngle=90,
    center=list('50%', '75%'),
    size='110%')

![](https://i.imgur.com/Rz2S72N.png)

Created on 2022-10-23 with reprex v2.0.2

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading