Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Hierarchical data representation for sunburst/treemaps/radial tree #207

Closed
barveaditya opened this issue Oct 7, 2020 · 13 comments
Closed
Assignees
Labels
enhancement New feature or request

Comments

@barveaditya
Copy link

barveaditya commented Oct 7, 2020

Hello,

I have been trying to plot my data using sunburst in a Shiny app, however the example (below) has a manually specified structure, which is not possible for me to code as part of a shiny visualisation. I have to do it programmatically, but even using tidyr::nest does not help as it does not allow the same name to multiple hierarchies. Is there another data format that I could use.

The example is below:

df <- tibble(
  name = c("earth", "mars", "venus"), value = c(30, 40, 30),        # 1st level
  itemStyle = tibble(color = c(NA, 'red', 'blue')),     # embedded styles, optional
  children = list(
    tibble(name = c("land", "ocean"), value = c(10,20),             # 2nd level
           children = list(
             tibble(name = c("forest", "river"), value = c(3,7)),   # 3rd level 
             tibble(name = c("fish", "kelp"), value = c(10,5),
                    children = list(
                      tibble(name = c("shark", "tuna"), value = c(2,6)),  # 4th level 
                      NULL  # kelp
                    ))
           )),
    tibble(name = c("crater", "valley"), value = c(20,20)),
    NULL  # venus
  )
)

for example, how would one convert the following data frame in a data structure accepted by e_sunburst programmatically?

df = data.frame(labels = c("Eve", "Cain", "Seth", "Enos", "Noam", "Abel", "Awan", "Enoch", "Azura"),
                parents = c("", "Eve", "Eve", "Seth", "Seth", "Eve", "Eve", "Awan", "Eve"),
                values = c(10, 14, 12, 10, 2, 6, 6, 4, 4))

This issue is as important as e_sunburst as one cannot use these great visualisations otherwise!

@JohnCoene
Copy link
Owner

I know, it's an absolute pain.

In a previous version it accepted a data.frame but was limited to one level of nesting. It's due to the underlying structure that echarts expects here it's rather convoluted

Ideally we'd have a recursive function go over the data.frame to produce the list but multiple attempts of mine have all ended failures.

@JohnCoene JohnCoene added the enhancement New feature or request label Oct 8, 2020
@barveaditya
Copy link
Author

barveaditya commented Oct 8, 2020

Hi John,

Thank you for the reply. 👍 I have posted the question on stackoverflow [https://stackoverflow.com/questions/64240732/is-there-a-programmatic-way-to-create-in-r-create-a-nested-list-of-tibbles
], but nothing yet. Will keep trying and update if I get it! :)

@helgasoft
Copy link
Contributor

@barveaditya - try this with library data.tree

library(echarts4r)
df <- data.frame(parents = c("","earth", "earth", "mars", "mars", "land", "land", "ocean", "ocean", "fish", "fish", "Everything", "Everything", "Everything"),
                 labels = c("Everything", "land", "ocean", "valley", "crater", "forest", "river", "kelp", "fish", "shark", "tuna", "venus","earth", "mars"),
                 value = c(0, 30, 40, 10, 10, 20, 10, 20, 20, 8, 12, 10, 70, 20))
library(data.tree)
universe <- FromDataFrameNetwork(df)
jsonl <- ToListExplicit(universe, unname=TRUE)
jsonl$children[[1]]$children %>% e_charts() %>% e_sunburst()

@JohnCoene JohnCoene self-assigned this Oct 26, 2020
@JohnCoene
Copy link
Owner

Thanks @helgasoft, I'll see if I can bring support for this in the package (passing {data.tree} Node to e_charts)

@barveaditya
Copy link
Author

oh this is brilliant @helgasoft Fantastic. I am very happy! :) @JohnCoene - thanks in advance for bringing in support.
Cheers,
Adi

@JohnCoene
Copy link
Owner

I just pushed a change to the e_charts so it supports these objects out-of-the box.

df <- data.frame(parents = c("","earth", "earth", "mars", "mars", "land", "land", "ocean", "ocean", "fish", "fish", "Everything", "Everything", "Everything"),
                 labels = c("Everything", "land", "ocean", "valley", "crater", "forest", "river", "kelp", "fish", "shark", "tuna", "venus","earth", "mars"),
                 value = c(0, 30, 40, 10, 10, 20, 10, 20, 20, 8, 12, 10, 70, 20))

# create a tree object
universe <- data.tree::FromDataFrameNetwork(df)

# use it in echarts4r
universe %>% 
  e_charts() %>% 
  e_sunburst()

@barveaditya
Copy link
Author

Brilliant. I tested it too. All good. Thank you for making this simpler @JohnCoene @helgasoft

JohnCoene added a commit that referenced this issue Oct 29, 2020
@barveaditya
Copy link
Author

@JohnCoene while you fixed the above for e_sunburst, the same doesn't work for e_tree

universe %>% e_charts() %>% e_tree() just shows me venus. Any ideas?

@helgasoft
Copy link
Contributor

Yes, e_tree is a little different

library(echarts4r)
library(data.tree)
df <- data.frame(parents = c("","earth", "earth", "mars", "mars", "land", "land", "ocean", "ocean", "fish", "fish", "Everything", "Everything", "Everything"),
                 labels = c("Everything", "land", "ocean", "valley", "crater", "forest", "river", "kelp", "fish", "shark", "tuna", "venus","earth", "mars"),
                 value = c(0, 30, 40, 10, 10, 20, 10, 20, 20, 8, 12, 10, 70, 20))
universe <- FromDataFrameNetwork(df)
jsonl <- ToListExplicit(universe, unname=TRUE)
jsonl$children %>% e_charts() %>% e_tree()

@helgasoft
Copy link
Contributor

Did you check the help '?e_tree'

e_tree(label = list(offset = c(0, -11)))  # works

@barveaditya
Copy link
Author

@helgasoft Yes, unfortunately right after I put it in here. :) which is why I deleted it, but I guess you already got an email! :) Thank you for getting back nonetheless. I am trying to look at echarts doc to enable some very cool looking features. :)
Now that I have you here, how would I scale the size of each node in the tree dependent on their value?

@helgasoft
Copy link
Contributor

jsonl$children %>% e_charts() %>% e_tree( 
        label = list(offset = c(0, -12)),
        symbolSize = htmlwidgets::JS("function(d) { return d; }"))

@GillesSanMartin
Copy link

GillesSanMartin commented Jan 26, 2023

I'm trying to apply the solutions proposed here but I still have problems :

With this, I get no graph (because : no "value" in acme data ??):

library(data.tree)
library(echarts4r)
data(acme)
acme |> 
    e_charts() |> 
    e_sunburst()

With this I get only a partial graph of the first subnode :

jsonl <- acme |> ToListExplicit(unname = TRUE)
jsonl$children|> 
    e_charts() |> 
    e_tree(initialTreeDepth = 3)

NB : I first posted a question on stackoverflow (edited since then) before finding the current page :
https://stackoverflow.com/questions/75247364

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

4 participants