I'm trying to create a sunburst plot, but can't get it to render while having the hierarchy represented. If I remove the "parents" argument, I get an output (a pie chart with no tiers), but the output is null when I try and represent the different levels. Thanks in advance for any help I can get on this!
Please provide your plotting code and some data. You can post data here using the dput() function. If your data frame is named DF. Post the output of
dput(DF)
Please put a line with three back ticks just before and after the data, like this
```
output of dput() goes here
```
Here is an example of the data I'm working with, and the code I currently have. Ideally, I would like to have all levels of the hierarchy represented on the sunburst plot, but for now I'm even just trying to get something with the first few.
kingdom phylum class order family genus Abundance
Animalia Cnidaria Cubozoa Chirodropida Chirodropidae Chironiex 10.0
Animalia Cnidaria Hydrozoa Leptothecata Aequoreidae Aequorea 31.0
Animalia Cindari Hydrozoa Anthoathecata Oceaniidae Turritopsis 15.0
# Create the sunburst plot
sunburst_plot <- plot_ly(
data = taxonomic_data,
ids = ~genus,
labels = ~family,
parents = ~order,
values = ~Abundance,
type = "sunburst"
)
# Display the plot
sunburst_plot
Hopefully this gives a good look at what I'm trying to accomplish. Thanks!
I have never made a sunburst plot, so the following may not be the best way to make one. I invented some data with a format similar to what you have and I derived a data frame from it that can be used to make a plot. The important point is that you have to specify the size of each section of the plot. If an Order has two Families and the Families have 2 and 3 Genera, you have to give the size of the Order, two Families and five Genera. You also have to specify the parent sector (Order) of each Family and parent sector (Family) of each Genus. The ids argument of plot_ly() is used to name sectors so they can be referred to as Parents.
In my example, the total Abundance of the Genera in Class B is 95, so I assign that abundance to B in the data frame I use for plotting. The Class is the base level of my plot, so Class B does not have a Parent. The Family D, which is part of Class B, has genera summing to 55, so I assign its Parent as B and its abundance as 55. Genus H is in Family D and has an abundance of 10. Its Parent is set to D and its Abundance to 10.
library(dplyr)
library(plotly)
taxonomic_data <- data.frame(class = c("A","A","A","A","A","A","A","A","A"),
order = c("B","B","B","B","B","C","C","C","C"),
family = c("D","D","D","E","E","F","G","G","G"),
genus = c("H","I","J","K","L","M","N","O","P"),
Abundance = c(10,30,15,25,15,10,30,20,10))
taxonomic_data
#> class order family genus Abundance
#> 1 A B D H 10
#> 2 A B D I 30
#> 3 A B D J 15
#> 4 A B E K 25
#> 5 A B E L 15
#> 6 A C F M 10
#> 7 A C G N 30
#> 8 A C G O 20
#> 9 A C G P 10
taxonomic_data |> group_by(order) |> summarize(TotalOrder = sum(Abundance))
#> # A tibble: 2 × 2
#> order TotalOrder
#> <chr> <dbl>
#> 1 B 95
#> 2 C 70
taxonomic_data |> group_by(family) |> summarize(TotalFamily = sum(Abundance))
#> # A tibble: 4 × 2
#> family TotalFamily
#> <chr> <dbl>
#> 1 D 55
#> 2 E 40
#> 3 F 10
#> 4 G 60
taxonomic_data_plot <- data.frame(
Parent = c("","","B","B","C","C","D","D","D","E","E","F","G","G","G"),
IDS = c("B","C","D","E","F","G","H","I","J","K","L","M","N","O","P"),
LABS = c("B","C","D","E","F","G","H","I","J","K","L","M","N","O","P"),
Abundance = c(95,70,55,40,10,60,10,30,15,25,15,10,30,20,10)
)
taxonomic_data_plot
#> Parent IDS LABS Abundance
#> 1 B B 95
#> 2 C C 70
#> 3 B D D 55
#> 4 B E E 40
#> 5 C F F 10
#> 6 C G G 60
#> 7 D H H 10
#> 8 D I I 30
#> 9 D J J 15
#> 10 E K K 25
#> 11 E L L 15
#> 12 F M M 10
#> 13 G N N 30
#> 14 G O O 20
#> 15 G P P 10
sunburst_plot <- plot_ly(
data = taxonomic_data_plot,
ids = ~IDS,
labels = ~LABS,
parents = ~Parent,
values = ~Abundance,
branchvalues = "total",
type = "sunburst"
)
Created on 2023-08-03 with reprex v2.0.2
Thank you so much! This was a really helpful and informative answer (and probably just saved me two hours of troubleshooting). Have a wonderful rest of your day!
Actually, one quick question if you have a chance-- is there a way to create the taxonomic_data_plot programatically (if I were reading in a large dataset), rather than manual entry? Thanks and no worries if not!
Here is some code that constructs taxonomic_data_plot. It isn't fully automatic. You have to manually adjust the names of the columns over which you want to summarize. DF1 is handled differently than DF2 and DF3 because there is no Parent at that level.
The process could be automated further but, as the lazy often say, I leave that as and exercise for the reader.
library(dplyr)
taxonomic_data <- data.frame(class = c("A","A","A","A","A","A","A","A","A"),
order = c("B","B","B","B","B","C","C","C","C"),
family = c("D","D","D","E","E","F","G","G","G"),
genus = c("H","I","J","K","L","M","N","O","P"),
Abundance = c(10,30,15,25,15,10,30,20,10))
DF1 <- taxonomic_data |> group_by(order) |>
summarize(Abundance = sum(Abundance),.groups = "drop") |>
select(LABS = order, Abundance) |>
mutate(Parent = "", IDS = LABS)
DF2 <- taxonomic_data |> group_by(order, family) |>
summarize(Abundance = sum(Abundance), .groups = "drop") |>
select(Parent = order, LABS = family, Abundance) |>
mutate(IDS = LABS)
DF3 <- taxonomic_data |> group_by(family, genus) |>
summarize(Abundance = sum(Abundance), .groups = "drop") |>
select(Parent = family, LABS = genus, Abundance) |>
mutate(IDS = LABS)
taxonomic_data_plot <- bind_rows(DF1, DF2, DF3)
This topic was automatically closed 42 days after the last reply. New replies are no longer allowed.
If you have a query related to it or one of the replies, start a new topic and refer back with a link.