|
| 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 | + |
0 commit comments