[英]Nest donut chart with plotly or highcharts in R
plotly
方式 plotly
的 sunburst 選項的輸入數據格式令人困惑(即有點痛苦); 文檔中的相關示例是Sunburst with Repeated Labels 。
關鍵是
ids
列。library(tidyverse)
df <- bind_rows(
# Full total
pd %>% summarise(labels = "Total", n = sum(n)),
# "Class"-level totals
pd %>%
group_by(labels = Class) %>%
summarise(
n = sum(n),
parents = "Total",
.groups = "drop"),
# Individual Class+Survived-level numbers
pd %>%
rename(parents = Class, labels = Survived) %>%
mutate(parents = paste("Total", parents, sep = " - "))) %>%
# Add unique ids column
mutate(ids = if_else(
is.na(parents), labels, paste(parents, labels, sep = " - ")))
## A tibble: 13 × 4
# labels n parents ids
# <chr> <dbl> <chr> <chr>
# 1 Total 2201 NA Total
# 2 1st 325 Total Total - 1st
# 3 2nd 285 Total Total - 2nd
# 4 3rd 706 Total Total - 3rd
# 5 Crew 885 Total Total - Crew
# 6 No 122 Total - 1st Total - 1st - No
# 7 Yes 203 Total - 1st Total - 1st - Yes
# 8 No 167 Total - 2nd Total - 2nd - No
# 9 Yes 118 Total - 2nd Total - 2nd - Yes
#10 No 528 Total - 3rd Total - 3rd - No
#11 Yes 178 Total - 3rd Total - 3rd - Yes
#12 No 673 Total - Crew Total - Crew - No
#13 Yes 212 Total - Crew Total - Crew - Yes
然后
library(plotly)
plot_ly(
data = df,
ids = ~ids,
labels = ~labels,
parents = ~parents,
values = ~n,
type = "sunburst",
branchvalues = "total")
highcharter
方式 highcharter
提供了方便的 function data_to_hierarchical()
來重新格式化輸入數據以實現旭日形可視化:
library(tidyverse)
library(highcharter)
pd %>%
data_to_hierarchical(c(Class, Survived), n) %>%
hchart(type = "sunburst")
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.