在外圈数据是内圈子集的R中如何嵌套饼图?

时间:2018-09-30 21:35:27

标签: r plotly pie-chart r-plotly

有人问过类似的问题,但未得到回答。我需要一个饼图,一个嵌套的饼图,其中包含外部值作为内部值的子集。

例如,我有一个过去一年购买的CD的简单数据表,我希望饼图显示乐队所在的每个大陆和外圈的CD数量,因此请显示每个国家/地区的CD来自特定大陆的乐队。

PieChart

内部饼形图的悬停数据是正确的,但外圈的数据却具有误导性。例如,德国拥有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)
    })

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

谢谢

1 个答案:

答案 0 :(得分:0)

听起来您想用自己的字体覆盖散乱的悬停文本。您可以自己指定并使用它来解决这个问题。

:class_name

然后在您的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

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