首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在外环数据是内环的子集的R中制作嵌套饼图?

问如何在外环数据是内环的子集的R中制作嵌套饼图?
EN

Stack Overflow用户
提问于 2018-10-01 05:35:27
回答 1查看 3.4K关注 0票数 2

类似的问题也被提出了,但没有得到回答。我需要一个饼图,一个嵌套的饼图,它将外部值作为内部值的子集。

例如,我有一个简单的CD数据表,我在过去的一年里购买了CD,我想要饼状图显示来自每个大陆的CD的数量,以及外环,这样就可以显示来自特定大陆的每个乐队的各个国家。

​

​

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

我不知道如何更改数据框来提供这些数字,也不知道如何通过情节来实现这一点。

这是我的原始数据集的一个示例:

代码语言:javascript
复制
    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

饼图呈现为:

代码语言:javascript
复制
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)
    })

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

谢谢

EN

回答 1

Stack Overflow用户

发布于 2018-10-01 10:24:37

这听起来像是你想用你自己的来覆盖这个悬停的文本。您可以通过自己指定并使用它来实现这一点。

代码语言:javascript
复制
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调用中使用类似以下内容:

代码语言:javascript
复制
# 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/

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52582394

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档