本文介绍了如何在R中使用plotly绘制嵌套饼图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试绘制嵌套的饼图,但是输出未反映实际情况.

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

让我们说:

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

结果饼图具有误导性,因为它应该按照每个国家/地区分配了动物,所以分布应取决于国家/地区.但是显示的饼图显示的是个体分布.我正在努力使用plotly.

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.

谢谢!

推荐答案

让我们忽略整个问题,是否应该回答一个人如何创建嵌套的饼形图.

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

您可以创建两个饼图,最外面的一个是甜甜圈图,即通过设置hole = 0.7,而内部的图表是已设置domain的子图.

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

这篇关于如何在R中使用plotly绘制嵌套饼图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 08:02