4

I am trying to plot a nested pie chart, but the output didn't reflect the reality.

Let's say:

library('plotly')
library('dplyr')

data <- data.frame(c('cat', 'dog', 'deer','chicken', 'cat', 'dog','duck', 'monkey', 'fish','cow','horse','dog'),c('US', 'US', 'US','US', 'UK', 'UK','UK', 'UK','China','China','China','China'),c(15,70,120,55,47,300,89,62,40,27,103,8))
colnames(data) <- c('animal', 'country', 'total_num')

p <- plot_ly(data) %>% add_pie(labels = ~animal, values = ~total_num, type = 'pie', hole = 0.7, sort = F) %>% add_pie(data, labels = ~country, values = ~total_num, domain = list(x = c(0.15, 0.85),y = c(0.15, 0.85)),sort = F)

p

enter image description here

The resulted pie chart is misleading as it supposed to have distribution of the animals according to each country, the distribution should be dependent on the country. But the shown pie charts are showing individual distribution. I am struggling in getting this using plotly.

Any suggestions or help will be very much appreciated.

Thanks!

Tan
  • 41
  • 1
  • 3
  • 4
    Asking to plot a pie chart will summon the wrath of the R community alone. I can only imagine what happens when someone asks about a **nested** pie chart. :) – jordan Jun 12 '18 at 19:09
  • Can you give some example of the above same chart using ploltly.js? I am struggling to combine multiple pie charts just you like did above – Aniket Tiwari Feb 05 '19 at 13:22

1 Answers1

5

Let's ignore the whole question if one should but answer how one could create a nested pie chart.

You could create two pie charts, the outer one is a donut chart, i.e. by setting hole = 0.7 and the inner chart is a subplot which has set the domain.

library('plotly')
library('dplyr')

data <- data.frame(c('cats', 'monkeys', 'dogs'), c(30, 10, 20), c(20, 10, 10))
colnames(data) <- c('animal', 'street', 'home')

p <- plot_ly(data) %>%
  add_pie(labels = ~animal, values = ~street, type = 'pie', hole = 0.7, sort = F) %>%
  add_pie(data, labels = ~animal, values = ~home, 
          domain = list(
            x = c(0.15, 0.85),
            y = c(0.15, 0.85)),
          sort = F)
p

enter image description here

Maximilian Peters
  • 30,348
  • 12
  • 86
  • 99
  • 1
    Thanks Max for helping out, however, that's not what I asked for cause you neglected my data/question. I am sorry if I wasn't clear in the first place. Perhaps let me put it in your way. – Tan Jun 16 '18 at 03:54
  • I have modified my question. – Tan Jun 16 '18 at 04:08