类似的问题也被提出了,但没有得到回答。我需要一个饼图,一个嵌套的饼图,它将外部值作为内部值的子集。
例如,我有一个简单的CD数据表,我在过去的一年里购买了CD,我想要饼状图显示来自每个大陆的CD的数量,以及外环,这样就可以显示来自特定大陆的每个乐队的各个国家。

内部饼图的悬停数据是正确的,但外部环的数据具有误导性。例如,在40个欧洲CD中,德国有15个,但德国的百分比为8.62%。相对于今年购买的40张欧洲CD,德国是37.5%,而德国是100%
我不知道如何更改数据框来提供这些数字,也不知道如何通过情节来实现这一点。
这是我的原始数据集的一个示例:
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)
})我使用的是shiny,并没有包含代码的这些部分。
谢谢
发布于 2018-10-01 10:24:37
这听起来像是你想用你自己的来覆盖这个悬停的文本。您可以通过自己指定并使用它来实现这一点。
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/
https://stackoverflow.com/questions/52582394
复制相似问题