3

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)) 
Bruno Guarita
  • 767
  • 10
  • 21
  • Pie chart are not optimal for visualization, see for example Steph Few [piecharts](https://www.google.com/url?sa=t&rct=j&q=&esrc=s&source=web&cd=&cad=rja&uact=8&ved=2ahUKEwiL64q0n5jtAhWEqHEKHUkLAeIQFjAAegQIBBAC&url=https%3A%2F%2Fwww.perceptualedge.com%2Farticles%2Fvisual_business_intelligence%2Fsave_the_pies_for_dessert.pdf&usg=AOvVaw028_adqzITh5jP7qRNOAHK). Unless you have only 2 categories and some comparison around 25% or 50%, a bar chart will be more readable. – Paul Rougieux Nov 23 '20 at 08:29

2 Answers2

2

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

Pierre
  • 671
  • 8
  • 25
1

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.

MKa
  • 2,248
  • 16
  • 22
  • This worked for me when making a pie nested in a donut chart, like this one: https://www.highcharts.com/demo/pie-donut – Mr.Rlover Dec 22 '21 at 23:36