2
votes

A similar question has been asked but left unanswered. I need a pie a nested pie chart that has the outer values as a subset of the inner values.

For example I have a simple data table of CD's I have purchased in the past year, I want the pie chart to show the number of CD's from each continent the band is from and the outer ring so show the individual countries of each band from the particular continent.

PieChart

The hover data for the inside pie chart is correct but the data for the outer ring is misleading. For instance, Germany has 15 CD's out of the 40 European CD's yet Germany's percentage claims 8.62%. The German % is true against the total 100% but against the 40 European CD's purchased this year, Germany is 37.5%

I cannot figure out how to either change the data frame to provide these numbers or how to get plotly to do it.

This is a sample of my orignal data set:

    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

The pie chart is rendered as:

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)
    })

Im using shiny and didn't include those parts of the code.

Thanks

1
Firstly: When it comes to conveying information in an interpretable way, pie charts are way down on the list due to our brains inability to accurately compare angles. You would be better off with a stacked bar chart. That being said, if you create said bar chart with ggplot2, you can twist it into a pie chart with coord_polar().Jannik Buhr

1 Answers

0
votes

It sounds like you want to override the plotly hover text with your own. You might approach this by specifying it yourself and using that.

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

Then use something like this in your add_pie call:

# 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)

More here: https://plot.ly/r/text-and-annotations/