3

I'm trying to do a semi circle donut with highcharter library but I only know how to do a pie chart. I know that with JS you can do it by adding "startAngle" and "endAngle" but I want to know how to do it with R:

A <- c("a", "b", "c", "d")
B <- c(4, 6, 9, 2)

C <- c(23, 26, 13, 15)
df <- data.frame(A, B, C)

highchart() %>% 
  hc_chart(type = "pie") %>% 
  hc_add_series_labels_values(labels = df$A, values = df$B)%>%    

  hc_tooltip(crosshairs = TRUE, borderWidth = 5, sort = TRUE, shared = TRUE, table = TRUE,
             pointFormat = paste('<b>{point.percentage:.1f}%</b>')
  ) %>%

  hc_title(text = "ABC",
           margin = 20,
           style = list(color = "#144746", useHTML = TRUE))

enter image description here

Thank you!

TobiO
  • 1,335
  • 1
  • 9
  • 24
Angela
  • 39
  • 2

2 Answers2

1

You can do something like this though not using Highcharts library.

library(tidyverse)
library(ggforce)
library(scales)
library(ggplot2)

# -------------------------------------------------------------------------

A <- c("a", "b", "c", "d")
B <- c(4, 6, 9, 2)

C <- c(23, 26, 13, 15)
df <- data.frame(A, B, C)

# Ensure A is a factor (we'll be using it to fill the pie)
df$A <- factor(df$A)

# compute the individual proportion in this case using var C
df$prop <- df$C/sum(df$C) 

# compute the cumulative proportion and use that to plot ymax 
df$p_end <- cumsum(df$prop)

# generate a y-min between 0 and 1 less value than p_end (using p_end) 
df$p_start <- c(0, head(df$p_end ,-1))



# -------------------------------------------------------------------------

# plot 
df %>%
  mutate_at(c("p_start", "p_end"), rescale, to=pi*c(-.5,.5), from=0:1) %>%
  ggplot + 
  geom_arc_bar(aes(x0 = 0, y0 = 0, r0 = .5, r = 1, start = p_start, end = p_end, fill=A)) + 
  coord_fixed() +xlab("X_label") + ylab("Y_lablel") + guides(fill=guide_legend(title="Legend Title"))


Output

pit_chart

Hope that helps.

Community
  • 1
  • 1
deepseefan
  • 3,701
  • 3
  • 18
  • 31
  • This is not what OP has asked for, though – mnist Nov 06 '19 at 10:03
  • In my defense, that is addressed in my first line. – deepseefan Nov 06 '19 at 10:17
  • In such cases, I would suggest a comment in which you ask OP whether he/she is interested in solutions outside of the specific framework. Just my opinion though – mnist Nov 06 '19 at 10:20
  • Am I the only one providing a `data.table` solution for `dplyr` question here? – deepseefan Nov 06 '19 at 10:24
  • I am not a big fan of that either but some points about that: (I) other people looking for an answer for the same problem might actually prefer to use data.table, (II) OP might be not aware of data.table, and (III), relating to this case, data.table and dplyr are tools that can - roughly speaking - do the same things yet highcharter provides interactive plots and ggplot does not. Therefore, interchangable usage is usually not possible/desired – mnist Nov 06 '19 at 10:53
  • You might be right, but the question is tagged `r` and `pie-chart` as well. When I ask a question on SO, I keep my options wide open so long as it does the job. This is a way of saying "hey, you know you can also make a semi-circle using ggplot which you can turn into interactive plot if you so will". Anyway I'll correct next time, and thanks for the heads up. – deepseefan Nov 06 '19 at 12:08
1

Try adding startAngle = -90, endAngle = 90 inside hc_add_series_labels_values.

Note as per the warning hc_add_series_labels_values is deprecated so suggest using hc_add_series.

highchart() %>%
  hc_add_series(type = "pie", data = df, hcaes(x = A, y = B), startAngle = -90, endAngle = 90) %>%
  hc_tooltip(pointFormat = '<b>{point.percentage:.1f}%</b>') %>%
  hc_title(text = "ABC",
           margin = 20,
           style = list(color = "#144746", useHTML = TRUE))

enter image description here

MKa
  • 2,248
  • 16
  • 22