Hi Everyone!!
This is a blog post to help people make Sunburst plots provided they have 2 or more column categorical data (here we will use Dengue Serotype and Genotype data). Since there are very limited examples on how to do advance plotting with echarts4r or make sunburst plots in R, I thought why not write a blog on it. So below is the code
Aim: To make a Sunburst plot of Dengue serotypes and genotypes for some random samples I had such that labels are plotted outside
## Load the data. My data has repeated rows of dengue assignments, where each row corresponds to a dengue sample and the two columns have serotype and genotype information
data <- read.csv("file.tsv", sep = "\t")
library(echarts4r)
library(dplyr)
## capitalizing the labels to make them pretty while plotting
data$ncbi_serotype <- gsub("denv","DENV ",data$ncbi_serotype)
data$nextclade_subtype <- stringr::str_split(data$nextclade_subtype,pattern = "/",simplify = TRUE)[,2]
## Let's group our samples at genotypes andserotype level an count the rows for each category that have
data <- data %>%
group_by(ncbi_serotype, nextclade_subtype) %>%
summarise(value = n(), .groups = "drop")
> data
# A tibble: 3 × 3
ncbi_serotype nextclade_subtype value
<chr> <chr> <int>
1 DENV 1 III 1
2 DENV 2 II 89
3 DENV 3 I 1
## Plotting the sunburst plot
data %>%
group_by(ncbi_serotype) %>%
summarise(
children = list(tibble(
name = paste0(nextclade_subtype," (",value,")"), ## So that in front of Genotypes the number of samples are plotted in brackets)
value = value # Set to the actual count of each genotype
)),
.groups = "drop"
) %>%
rename(name = ncbi_serotype) %>%
tibble() %>%
e_charts() |>
e_sunburst(label = list(show = TRUE,fontWeight = "bold", color = "black",fontSize = 12,position='outside',padding=3 ), ## We wish to add lables outside the sunburst because there are categories with lower no. of samples so their label will overlap.
labelLine = list(show = TRUE))
Another package that helps you achieve the same results but in more interactive (no ideal for static sunbursts) is using sunburst
function
## Another method
#Souce: https://cran.r-project.org/web/packages/sunburstR/vignettes/sunburst-2-0-0.html
library(sunburstR)
library(htmltools)
#> Warning: package 'htmltools' was built under R version 4.1.3
library(d3r)
temp <- data
colnames(temp) <- c("level1","level2","size") ## It requires colnames to be levels and value to be "size"
tree <- d3_nest(temp, value_cols = "size")
sunburstR::sunburst(tree, width="100%", height=400)