Search code examples
rplotly

Add subgroup text to plotly pie chart


I want to create a donut chart with plotly based on a group and showing the text of the subgroup in the hover info. My data looks like follows:

   Type                            country_group type_group   Value
   <chr>                           <chr>         <chr>        <int>
 1 Biomass                         Central       Res         694531
 2 Fossil Gas                      Central       Gas        3744726
 3 Fossil Hard coal                Central       Coal       1700840
 4 Fossil Oil                      Central       Oil          77682
 5 Geothermal                      Central       Res           2877
 6 Hydro Pumped Storage            Central       HPP         742692
 7 Hydro Run-of-river and poundage Central       HPP         930772
 8 Hydro Water Reservoir           Central       HPP         320887
 9 Other                           Central       Res         183331
10 Other renewable                 Central       Res          18916
11 Solar                           Central       PV          352138
12 Waste                           Central       Res         121710
13 Wind Onshore                    Central       Wind        969843
14 Nuclear                         Central       Nuc        1994802
15 Wind Offshore                   Central       Wind        397997
16 Fossil Brown coal/Lignite       Central       Coal       2037065

I now want to create a donut chart based on the column type_group and add the Type and Value as hover info. What I've come up with is:

data %>%
  mutate(sublabel = paste0('Type: ', Type, ', Value: ', Value)) %>%
  plot_ly(labels = ~type_group, 
        values = ~Value,
        hovertext = ~sublabel,
        textposition = 'inside') %>%
  add_pie(hole = 0.6)

Which produces the donut chart but only adds the first type and value to the hovertext.

donut

How do I get the other Types (e.g. Hydro Water Reservoir) into the hovertext?

Data:

structure(list(Type = c("Biomass", "Fossil Gas", "Fossil Hard coal", 
"Fossil Oil", "Geothermal", "Hydro Pumped Storage", "Hydro Run-of-river and poundage", 
"Hydro Water Reservoir", "Other", "Other renewable", "Solar", 
"Waste", "Wind Onshore", "Nuclear", "Wind Offshore", "Fossil Brown coal/Lignite"
), country_group = c("Central", "Central", "Central", "Central", 
"Central", "Central", "Central", "Central", "Central", "Central", 
"Central", "Central", "Central", "Central", "Central", "Central"
), type_group = c("Res", "Gas", "Coal", "Oil", "Res", "HPP", 
"HPP", "HPP", "Res", "Res", "PV", "Res", "Wind", "Nuc", "Wind", 
"Coal"), Value = c(694531L, 3744726L, 1700840L, 77682L, 2877L, 
742692L, 930772L, 320887L, 183331L, 18916L, 352138L, 121710L, 
969843L, 1994802L, 397997L, 2037065L)), class = c("tbl_df", "tbl", 
"data.frame"), row.names = c(NA, -16L))

Solution

  • You can aggregate the data to the type_group level. Sum the Value column by type_group and concatenate Type to be a single string for each type_group.

    data %>%
        group_by(type_group) %>%
        summarise(types = paste(Type, collapse = ",\n"),
                  sumValue = sum(Value)) %>% 
        ungroup() %>%
        mutate(sublabel = paste0('Type: ', types, ',\n Value: ', sumValue)) %>%
        plot_ly(labels = ~ type_group, 
                values = ~ sumValue,
                hovertext = ~ sublabel,
                textposition = 'inside') %>%
        add_pie(hole = 0.6)
    

    This produces a donut chart with the following hover text: Hover Text