【问题标题】:How to do nested pie chart in R where the outer ring data is a subset of inner ring?如何在R中做嵌套饼图,其中外环数据是内环的子集?
【发布时间】:2018-09-30 21:35:26
【问题描述】:

有人问过类似的问题,但没有得到回答。我需要一个饼图,一个嵌套的饼图,它的外部值是内部值的子集。

例如,我有一个简单的 CD 数据表,其中包含我在过去一年中购买的 CD,我希望饼图显示乐队所在的每个大陆的 CD 数量以及外环,以便显示每个国家的各个国家来自特定大陆的乐队。

内部饼图的悬停数据是正确的,但外圈的数据具有误导性。例如,在 40 个欧洲 CD 中,德国拥有 15 个 CD,但德国的百分比声称为 8.62%。德国的百分比对总 100% 是正确的,但相对于今年购买的 40 张欧洲 CD,德国是 37.5%

我不知道如何更改数据框以提供这些数字或如何巧妙地做到这一点。

这是我原始数据集的一个样本:

    Artist   Album  Year    Country Continent
1   Myrath  Legacy  2016    Tunisia Asia
2   Myrath   Sands  2011    Tunisia Asia
3   Orphaned Shalem 2011    Israel  Asia
4   Orphaned Unsung 2018    Israel  Asia

饼图呈现为:

output$bandChart <- renderPlotly({



        plot_ly(freefilter() ,labels = ~Country, values = ~AlbumCount,
               showlegend = FALSE
                ) %>%

        add_pie(hole = 0.6,
                textinfo = 'label',
                textposition = 'inside',
                insidetextfont = list(color = '#FFFFFF'),
                marker = list(line = list(color = '#FFFFFF', width = 1)),
                direction = 'clockwise',
                sort = FALSE,
                text = ~paste(Country, AlbumCount),
                hoverinfo = 'text + percent') %>%

        add_pie(freefilter(),labels = ~Continent, values = ~AlbumCount,
                textinfo = 'label',
                textposition = 'inside',
                direction = 'clockwise',
                sort = FALSE,
                name = "Continent Data,
                marker = list(line = list(color = '#FFFFFF', width = 1)),
                domain = list(x = c(.2, 0.8), y = c(0.2, .8))

                        ) %>%

              config (collaborate = FALSE, displaylogo = FALSE ) %>%

             layout(title = "Band Locations",
               xaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE),
               yaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE),
               autosize = FALSE)
    })

我使用的是闪亮的,没有包含这些代码部分。

谢谢

【问题讨论】:

  • 首先:当谈到以可解释的方式传达信息时,由于我们的大脑无法准确比较角度,饼图在列表中的排名靠后。使用堆积条形图会更好。话虽如此,如果您使用 ggplot2 创建所述条形图,您可以使用 coord_polar() 将其扭曲成饼图。

标签: r plotly pie-chart r-plotly


【解决方案1】:

听起来你想用你自己的覆盖情节悬停文本。您可以通过自己指定并使用它来解决此问题。

library(dplyr)
library(gapminder)

# Example data from gapminder, which happens to have countries and continents
worldpop_share = gapminder %>%
  # Let's just use the last year of data, I think 2007
  filter(year == max(year)) %>%
  # We only need country, continent, and population
  select(country, continent, pop) %>%

  # Make a grouping for the rows related to each continent
  group_by(continent) %>%
  # and calc that row's (country's) share of it's continent's population
  mutate(share_of_cont = pop / sum(pop %>% as.numeric)) %>%
  ungroup()  # This is just to tidy up so our table isn't grouped anymore

然后在您的add_pie 通话中使用类似的内容:

# This could replace the current plotly text, which has plotly calculate the percentage, which seems to be happening for the whole and not at the continent level.
text = ~paste(Country, share_of_cont)

更多: https://plot.ly/r/text-and-annotations/

【讨论】:

  • ummmm.... 在尝试您的建议之前,我还有很多东西要学,但感谢您的指导。
  • 别担心!我添加了一些注释,希望对您有所帮助。可能可以指定您希望在情节中获得大陆级别的份额,但我不知道如何,所以我的方法是首先计算您想要的确切统计数据,然后将其提供给情节。
  • 同意!从我的 text = ~paste(Country, AlbumCount), hoverinfo = 'text + percent') 中可以看出,我有点搞砸了,但正如你所说,在表格中获取我需要的数据看起来是去。弄清楚变异可能是关键。再次感谢
猜你喜欢
  • 2018-07-15
  • 2021-03-07
  • 2017-08-02
  • 2021-08-06
  • 2012-03-29
  • 1970-01-01
  • 2022-01-14
  • 2021-06-01
  • 1970-01-01
相关资源
最近更新 更多