我想创建一个基于组的圆环图,并在悬停信息中显示子组的文本。
我的数据如下所示:
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)
它生成圆环图,但只将第一种类型和值添加到悬停文本中。
如何将其他类型(如水力蓄水池)输入到悬停文本中?
数据:
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))