Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Two pies in the same graph - Highcharter

I am trying to reproduce the following example of two pies in the same highchart graph with no success. An example code below. Does any one knows how to create two charts in the same chart in highcharter?

df = tibble(name = c("a","b","c"),
        a1 = c(10,12,11),
        a2 = c(22,23,22))
highchart() %>%
hc_chart(renderTo = "container", type = "pie") %>%
hc_add_series(df, hcaes(name, a1), size = 100, center = c(30,10)) %>%
hc_add_series(df, hcaes(name, a2), size = 100, center = c(10,30)) 
like image 839
Bruno Guarita Avatar asked Dec 05 '25 18:12

Bruno Guarita


2 Answers

A possible solution

highcharter::hw_grid(
  hchart(df, type = "pie", mapping = hcaes(name, a1))
  ,
  hchart(df, type = "pie", mapping = hcaes(name, a2))
) %>% htmltools::browsable()

enter image description here

like image 148
Pierre Avatar answered Dec 08 '25 12:12

Pierre


I initially thought it should be controlled by the center arguments as per your code above (and didn't work as you pointed out).

One workaround is:

highchart() %>% 
hc_add_series(type = "pie", data = df, hcaes(name, a1),size = 100, name = "test1", center = c(0, 0)) %>%
hc_add_series(type = "pie", data = df, hcaes(name, a2),size = 100, name = "test2") %>%
hc_plotOptions(pie = list(center = c(700,450)))

You need the center argument in the first series although it doesn't do anything other than fixing the first pie (changing c(0, 0) doesn't actually change the location of the pie) then using hc_plotOptions to control the location of the second pie chart.

like image 40
MKa Avatar answered Dec 08 '25 11:12

MKa