代码之家  ›  专栏  ›  技术社区  ›  Nico

将子组文本添加到绘图饼图

  •  0
  • Nico  · 技术社区  · 2 年前

    我想创建一个基于组的圆环图,并在悬停信息中显示子组的文本。 我的数据如下所示:

       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
    

    我现在想根据列创建一个圆环图 type_group 并添加 Type Value 作为悬停信息。 我想到的是:

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

    它生成圆环图,但只将第一种类型和值添加到悬停文本中。

    donut

    如何将其他类型(如水力蓄水池)输入到悬停文本中?

    数据:

    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))
    
    1 回复  |  直到 2 年前
        1
  •  1
  •   Steven314    2 年前

    您可以将数据聚合到 type_group 数量求和 Value 列依据 类型组 并连接 Type 为每个字符串 类型组 .

    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)
    

    这将生成一个带有以下悬停文本的圆环图: Hover Text