Skip to content

Commit 5036cb7

Browse files
authored
Merge pull request #2 from cvanvelt/new/sunburst
Add sunburst notebook
2 parents 4010660 + 90643f6 commit 5036cb7

File tree

23 files changed

+4175
-1
lines changed

23 files changed

+4175
-1
lines changed

Sunburst.Rmd

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
---
2+
title: "Sunburst hierarchy"
3+
output:
4+
html_document:
5+
toc: true
6+
toc_float: true
7+
code_download: true
8+
code_folding: hide
9+
date: "2024-02-06"
10+
---
11+
12+
## Plotting a sunburst diagram
13+
14+
This script can be used to plot a sunburst diagram to represent a hierarchy.
15+
The input can be a data frame with rows representing the leaf-level and columns represent annotations. This data frame needs to be restructured to a parent-child (hierarchical network data frame), which is what the 'as.hierDF' does.
16+
17+
```{r klippy, echo=FALSE, include=TRUE}
18+
klippy::klippy()
19+
```
20+
21+
```{r setup, include=FALSE}
22+
knitr::opts_chunk$set(warning = FALSE, message = FALSE)
23+
```
24+
25+
```{r loading libs, echo=T}
26+
27+
require(dplyr)
28+
require(plotly)
29+
30+
```
31+
32+
Let's start by loading some dummy hierarchical data.
33+
34+
```{r, echo=T, eval=T}
35+
URL = 'https://allen-brain-cell-atlas.s3-us-west-2.amazonaws.com/metadata/WMB-taxonomy/20231215/cl.df_CCN202307220.xlsx'
36+
data = rio::import_list(URL)
37+
38+
colors <- rio::import("https://allen-brain-cell-atlas.s3-us-west-2.amazonaws.com/metadata/WMB-taxonomy/20231215/views/cluster_to_cluster_annotation_membership_color.csv")
39+
```
40+
41+
```{r, echo=T, eval=T}
42+
43+
cl.df <- data$cluster_annotation
44+
cl.df <- cl.df[cl.df$class_label != "LQ",]
45+
46+
cl.df <- cl.df %>% mutate(cluster_size = c(multiome.size + v2.size + v3.size))
47+
48+
# add colors to cluster data frame
49+
colors$cluster_alias <- as.character(as.integer(colors$cluster_alias))
50+
cl.df <- cl.df %>% left_join(colors, by=c("cl"="cluster_alias"))
51+
52+
select.columns <- colnames(cl.df)[grep("^supertype", colnames(cl.df))]
53+
st.df <- cl.df %>% group_by_at(select.columns) %>% summarise(n=n())
54+
55+
select.columns <- colnames(cl.df)[grep("^subclass", colnames(cl.df))]
56+
sc.df <- cl.df %>% group_by_at(select.columns) %>% summarise(n=n())
57+
58+
select.columns <- colnames(cl.df)[grep("^class", colnames(cl.df))]
59+
c.df <- cl.df %>% group_by_at(select.columns) %>% summarise(n=n())
60+
61+
```
62+
63+
A hierarchic structure is basically a set of nodes, with edges linking nodes. Let's create an edge list for plotting using the <igraph> package.We'll do this for a subset of the data.
64+
65+
```{r, echo=T, eval=T}
66+
devtools::source_gist("https://gist.github.com/cvanvelt/ef29e1581b30b9758ec1bba1b8322619")
67+
```
68+
69+
70+
71+
```{r create sunburst.df, fig.width=12, fig.height=4, echo=T}
72+
cl.df <- cl.df[cl.df$class_id %in% c(1:7),]
73+
sunburstDF <- as.sunburstDF(cl.df,
74+
levels=c("class","subclass","supertype"),
75+
valueCol = "cluster_size",
76+
rootname="WMB")
77+
```
78+
79+
```{r create plot, fig.width=11, fig.height=2, eval=T, echo=T}
80+
p <- plot_ly() %>%
81+
add_trace(ids = sunburstDF$ids,
82+
labels = sunburstDF$labels,
83+
parents =sunburstDF$parent,
84+
values = sunburstDF$values,
85+
type = 'sunburst',
86+
sort=FALSE,
87+
marker = list(colors = sunburstDF$color),
88+
domain = list(column = 1),
89+
branchvalues = 'total'
90+
)%>%
91+
layout(grid = list(columns =1, rows = 1),
92+
margin = list(l = 0, r = 0, b = 0, t = 0)
93+
)
94+
95+
96+
97+
```
98+
99+
```{r plot tree, fig.width=6, fig.height=6,echo=T}
100+
p
101+
```
102+
103+

_site.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ navbar:
77
- text: "Plot examples"
88
menu:
99
- text: HierTree
10-
href: HierTree.html
10+
href: HierTree.html
11+
- text: Sunburst
12+
href: Sunburst.html
1113
- text: "About"
1214
href: about.html
1315
output_dir: docs

docs/HierTree.html

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,9 @@
346346
<li>
347347
<a href="HierTree.html">HierTree</a>
348348
</li>
349+
<li>
350+
<a href="Sunburst.html">Sunburst</a>
351+
</li>
349352
</ul>
350353
</li>
351354
<li>
-1.3 KB
Loading

0 commit comments

Comments
 (0)