[英]Nest donut chart with plotly or highcharts in R
library(webr)
data <- as.data.frame(Titanic)
pd = data %>%
group_by(Class, Survived) %>%
summarise(n = sum(Freq))
PieDonut(pd, aes(Class, Survived, count = n),
labelposition = 0,
r0 = 0.5,
r1 = 0.95,
title = "Titanic: Survial by Class")
This donut fig was made With webr
package, is it possible to polt such an interactive fig with plotly
or highchart
.这个甜甜圈图是用webr
package 制作的,是否可以用plotly
或highchart
这样一个交互式图。
plotly
way 1. plotly
方式The input data format for plotly
's sunburst option is confusing (ie a bit of a pain); plotly
的 sunburst 选项的输入数据格式令人困惑(即有点痛苦); the relevant example in the documentation is Sunburst with Repeated Labels .文档中的相关示例是Sunburst with Repeated Labels 。
The key is to关键是
ids
column.添加一个唯一的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
Then然后
library(plotly)
plot_ly(
data = df,
ids = ~ids,
labels = ~labels,
parents = ~parents,
values = ~n,
type = "sunburst",
branchvalues = "total")
highcharter
way 2. highcharter
方式highcharter
provides the convenience function data_to_hierarchical()
to reformat input data for a sunburst visualisation: 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.