JohnCoene / echarts4r

🐳 ECharts 5 for R
http://echarts4r.john-coene.com/
Other
601 stars 81 forks source link

e_sunburst or e_treemap only worked on a portion of the data tree structure? #322

Open tungttnguyen opened 3 years ago

tungttnguyen commented 3 years ago

Hi John,

Thanks for working on this awesome package! I am loving it so far! One issue I have at the moment is that the e_sunburst and e_treemap did not use the whole data.tree structure when generate sunburst or treemap charts. An example is posted below. Please let me know if I did something wrong!

library(stringr)
library(dplyr)
library(data.tree)
library(echarts4r)
packageVersion("echarts4r")
#> [1] '0.4.1.9000'

### Sample data
DF <- data.frame(
  parents = c("Mid Stream","Mid Stream","Mid Stream","Mid Stream",
              "Mid Stream","Mid Stream","Mid Stream","Mid Stream",
              "Mid Stream","Mid Stream","Mid Stream","Mid Stream",
              "Low Stream","Low Stream",
              "Low Stream","Low Stream",
              "Low Stream","Mac River","Mac River","Mac River",
              "Mac River","Mac River","Mac River","Mac River",
              "Mac River","Mac River","Mac River","Mac River",
              "Mac River","Mid Stream","Mid Stream",
              "Mid Stream","Low Stream","Low Stream",
              "Low Stream","Low Stream",
              "Low Stream","Low Stream","Low Stream",
              "Low Stream","Low Stream",
              "Low Stream","Low Stream",
              "Low Stream","Low Stream","Low Stream",
              "Low Stream","Low Stream",
              "Low Stream","Low Stream",
              "Low Stream","Low Stream","Low Stream",
              "Low Stream","Low Stream",
              "Low Stream","Low Stream","Low Stream",
              "Low Stream","Low Stream",
              "Low Stream","Upp King","Upp King","Upp King",
              "Upp King","Upp King","Upp King","Upp King",
              "Upp King","Upp King","Upp King","Upp King",
              "Upp King","Upp King","Upp King","Upp King",
              "Upp King","Upp King","Upp King","Upp King",
              "Mid King","Mid King","Mid King","Mid King","Mid King",
              "Mid King","Mid King","Mid King",
              "Low King","Low King","Low King",
              "Low King","Low King","Low King",
              "Low King","Low King",
              "Low King","Low King","Low King",
              "Low King","Mid King","Low King",
              "Low King","Low King",
              "Low King","Low King","Low King",
              "Low King","MAC","MAC","MAC","King","King",
              "King","Basin","Basin"),
  labels = c("MAC01","MAC02",
             "MAC03","MAC04","MAC05","MAC06","MAC07",
             "MAC08","MAC09","MAC10","MAC11","MAC12","MAC13",
             "MAC14","MAC15","MAC16","MAC17","MAC18",
             "MAC20","MAC21","MAC22","MAC23","MAC24","MAC25",
             "MAC26","MAC27","MAC28","MAC29","MAC30",
             "MAC32","MAC33","MAC34","MAC35","MAC36","MAC37",
             "MAC38","MAC39","MAC40","MAC41","MAC42","MAC43",
             "MAC44","MAC45","MAC46","MAC47","MAC48",
             "MAC49","MAC50","MAC51","MAC52","MAC53","MAC54",
             "MAC55","MAC56","MAC57","MAC58","MAC59",
             "MAC60","MAC61","MAC62","MAC63","King01","King02",
             "King03","King04","King05","King06","King07",
             "King08","King09","King10","King11","King12",
             "King13","King14","King15","King16","King17",
             "King18","King19","King20","King21","King22",
             "King23","King24","King25","King26","King27",
             "King28","King29","King30","King31",
             "King31c","King36","King37","King38","King39",
             "King40","King41","King42","King43","King50",
             "King51","King52","King53","King54","King55",
             "King56","Mid Stream","Low Stream","Mac River",
             "Upp King","Mid King","Low King","MAC","King"),
  value = c(16611,1960,8,
            183,15,0,242,1346,1780,19,1225,107,145,76,
            10,233,216,14,2,238,103,75,2311,28106,
            9308,3118,2959,NA,447,1127,64,939,141,
            37083,NA,27775,3454,551,335,678,2,783,14,
            367,398,160,184,1492,42,38,1101,4694,465,
            142,570,2462,72,6,16,237,12356,0,80,2,0,
            533,2465,3,40,816,187,28,9,87,283,613,
            1,114,160,40,39,78,88,45,83,579,43,120,
            227,69,123,118,7,384,5,249,614,37,27,
            102,0,18339,1027,582,245,25634,27139,162,
            25626,96298,46681,5461,1075,75090,168605,
            81626)
)

Create data tree structure

DF_tree <- data.tree::FromDataFrameNetwork(DF)
print(DF_tree)
#>                             levelName
#> 1   Basin                            
#> 2    ¦--MAC                          
#> 3    ¦   ¦--Mid Stream               
#> 4    ¦   ¦   ¦--MAC01                
#> 5    ¦   ¦   ¦--MAC02                
#> 6    ¦   ¦   ¦--MAC03                
#> 7    ¦   ¦   ¦--MAC04                
#> 8    ¦   ¦   ¦--MAC05                
#> 9    ¦   ¦   ¦--MAC06                
#> 10   ¦   ¦   ¦--MAC07                
#> 11   ¦   ¦   ¦--MAC08                
#> 12   ¦   ¦   ¦--MAC09                
#> 13   ¦   ¦   ¦--MAC10                
#> 14   ¦   ¦   ¦--MAC11                
#> 15   ¦   ¦   ¦--MAC12                
#> 16   ¦   ¦   ¦--MAC32                
#> 17   ¦   ¦   ¦--MAC33                
#> 18   ¦   ¦   °--MAC34                
#> 19   ¦   ¦--Low Stream               
#> 20   ¦   ¦   ¦--MAC13                
#> 21   ¦   ¦   ¦--MAC14                
#> 22   ¦   ¦   ¦--MAC15                
#> 23   ¦   ¦   ¦--MAC16                
#> 24   ¦   ¦   ¦--MAC17                
#> 25   ¦   ¦   ¦--MAC35                
#> 26   ¦   ¦   ¦--MAC36                
#> 27   ¦   ¦   ¦--MAC37                
#> 28   ¦   ¦   ¦--MAC38                
#> 29   ¦   ¦   ¦--MAC39                
#> 30   ¦   ¦   ¦--MAC40                
#> 31   ¦   ¦   ¦--MAC41                
#> 32   ¦   ¦   ¦--MAC42                
#> 33   ¦   ¦   ¦--MAC43                
#> 34   ¦   ¦   ¦--MAC44                
#> 35   ¦   ¦   ¦--MAC45                
#> 36   ¦   ¦   ¦--MAC46                
#> 37   ¦   ¦   ¦--MAC47                
#> 38   ¦   ¦   ¦--MAC48                
#> 39   ¦   ¦   ¦--MAC49                
#> 40   ¦   ¦   ¦--MAC50                
#> 41   ¦   ¦   ¦--MAC51                
#> 42   ¦   ¦   ¦--MAC52                
#> 43   ¦   ¦   ¦--MAC53                
#> 44   ¦   ¦   ¦--MAC54                
#> 45   ¦   ¦   ¦--MAC55                
#> 46   ¦   ¦   ¦--MAC56                
#> 47   ¦   ¦   ¦--MAC57                
#> 48   ¦   ¦   ¦--MAC58                
#> 49   ¦   ¦   ¦--MAC59                
#> 50   ¦   ¦   ¦--MAC60                
#> 51   ¦   ¦   ¦--MAC61                
#> 52   ¦   ¦   ¦--MAC62                
#> 53   ¦   ¦   °--MAC63                
#> 54   ¦   °--Mac River                
#> 55   ¦       ¦--MAC18                
#> 56   ¦       ¦--MAC20                
#> 57   ¦       ¦--MAC21                
#> 58   ¦       ¦--MAC22                
#> 59   ¦       ¦--MAC23                
#> 60   ¦       ¦--MAC24                
#> 61   ¦       ¦--MAC25                
#> 62   ¦       ¦--MAC26                
#> 63   ¦       ¦--MAC27                
#> 64   ¦       ¦--MAC28                
#> 65   ¦       ¦--MAC29                
#> 66   ¦       °--MAC30                
#> 67   °--King                         
#> 68       ¦--Upp King                 
#> 69       ¦   ¦--King01               
#> 70       ¦   ¦--King02               
#> 71       ¦   ¦--King03               
#> 72       ¦   ¦--King04               
#> 73       ¦   ¦--King05               
#> 74       ¦   ¦--King06               
#> 75       ¦   ¦--King07               
#> 76       ¦   ¦--King08               
#> 77       ¦   ¦--King09               
#> 78       ¦   ¦--King10               
#> 79       ¦   ¦--King11               
#> 80       ¦   ¦--King12               
#> 81       ¦   ¦--King13               
#> 82       ¦   ¦--King14               
#> 83       ¦   ¦--King15               
#> 84       ¦   ¦--King16               
#> 85       ¦   ¦--King17               
#> 86       ¦   ¦--King18               
#> 87       ¦   °--King19               
#> 88       ¦--Mid King                 
#> 89       ¦   ¦--King20               
#> 90       ¦   ¦--King21               
#> 91       ¦   ¦--King22               
#> 92       ¦   ¦--King23               
#> 93       ¦   ¦--King24               
#> 94       ¦   ¦--King25               
#> 95       ¦   ¦--King26               
#> 96       ¦   ¦--King27               
#> 97       ¦   °--King43               
#> 98       °--Low King                 
#> 99           ¦--King28               
#> 100          °--... 18 nodes w/ 0 sub

Sunburst plot missing data from the King branch

DF_tree %>%
  e_charts() %>%
  e_sunburst() %>%
  e_tooltip()

Treemap plot missing data from the King branch

DF_tree %>%
  e_charts() %>%
  e_treemap() %>%
  e_tooltip()

If I created the Treemap for just the data from the King branch, they showed up

DF_tree2 <- data.tree::FromDataFrameNetwork(DF %>% filter(str_detect(labels, "King")))
print(DF_tree2)
#>              levelName
#> 1  Basin              
#> 2   °--King           
#> 3       ¦--Upp King   
#> 4       ¦   ¦--King01 
#> 5       ¦   ¦--King02 
#> 6       ¦   ¦--King03 
#> 7       ¦   ¦--King04 
#> 8       ¦   ¦--King05 
#> 9       ¦   ¦--King06 
#> 10      ¦   ¦--King07 
#> 11      ¦   ¦--King08 
#> 12      ¦   ¦--King09 
#> 13      ¦   ¦--King10 
#> 14      ¦   ¦--King11 
#> 15      ¦   ¦--King12 
#> 16      ¦   ¦--King13 
#> 17      ¦   ¦--King14 
#> 18      ¦   ¦--King15 
#> 19      ¦   ¦--King16 
#> 20      ¦   ¦--King17 
#> 21      ¦   ¦--King18 
#> 22      ¦   °--King19 
#> 23      ¦--Mid King   
#> 24      ¦   ¦--King20 
#> 25      ¦   ¦--King21 
#> 26      ¦   ¦--King22 
#> 27      ¦   ¦--King23 
#> 28      ¦   ¦--King24 
#> 29      ¦   ¦--King25 
#> 30      ¦   ¦--King26 
#> 31      ¦   ¦--King27 
#> 32      ¦   °--King43 
#> 33      °--Low King   
#> 34          ¦--King28 
#> 35          ¦--King29 
#> 36          ¦--King30 
#> 37          ¦--King31 
#> 38          ¦--King31c
#> 39          ¦--King36 
#> 40          ¦--King37 
#> 41          ¦--King38 
#> 42          ¦--King39 
#> 43          ¦--King40 
#> 44          ¦--King41 
#> 45          ¦--King42 
#> 46          ¦--King50 
#> 47          ¦--King51 
#> 48          ¦--King52 
#> 49          ¦--King53 
#> 50          ¦--King54 
#> 51          ¦--King55 
#> 52          °--King56
DF_tree2 %>%
  e_charts() %>%
  e_treemap() %>%
  e_tooltip()

These are the plots generated by the treemap and highcharter packages for comparison

library(highcharter)
DF_hc <- data_to_hierarchical(DF, c(parents, labels), value)
hchart(DF_hc, type = "sunburst")

hchart(DF_hc, type = "treemap")

library(treemap)
treemap(DF,
        index = c("parents", "labels"),
        vSize = "value",
        type = "index",
        palette = "Set2",
        bg.labels = c("white")
        )

Created on 2021-05-23 by the reprex package (v2.0.0)

AndrewDisher commented 1 year ago

Hi tungttnguyen,

I am not sure if you are still looking for a solution to this problem, but I believe I may have found a solution. I was running into the same error you are talking about with my own data.

I am currently working on your sunburst chart example, although I believe this should be applicable to your treemap as well.

I believe your node structure is implying that you want "Basin" to be your root node, where the values for "MAC" and "King" sum up to be the total value of your sunburst chart. If that is the case, then you should include another row in DF where the cell for parents is an empty string (""), label is ("Basin"), and value is (0). I've modified your code to reflect this.

I apologize first for the apparent "messiness" of the following code. I'm not sure how to insert the code as neatly as you did in your comment, so I've just copied my code and pasted it inside of my comment. This is my first time posting in a comment section inside of a github repository.

:)

----- Code -----

DF <- data.frame(

parents = c("Mid Stream","Mid Stream","Mid Stream","Mid Stream", "Mid Stream","Mid Stream","Mid Stream","Mid Stream", "Mid Stream","Mid Stream","Mid Stream","Mid Stream", "Low Stream","Low Stream", "Low Stream","Low Stream", "Low Stream","Mac River","Mac River","Mac River", "Mac River","Mac River","Mac River","Mac River", "Mac River","Mac River","Mac River","Mac River", "Mac River","Mid Stream","Mid Stream", "Mid Stream","Low Stream","Low Stream", "Low Stream","Low Stream", "Low Stream","Low Stream","Low Stream", "Low Stream","Low Stream", "Low Stream","Low Stream", "Low Stream","Low Stream","Low Stream", "Low Stream","Low Stream", "Low Stream","Low Stream", "Low Stream","Low Stream","Low Stream", "Low Stream","Low Stream", "Low Stream","Low Stream","Low Stream", "Low Stream","Low Stream", "Low Stream","Upp King","Upp King","Upp King", "Upp King","Upp King","Upp King","Upp King", "Upp King","Upp King","Upp King","Upp King", "Upp King","Upp King","Upp King","Upp King", "Upp King","Upp King","Upp King","Upp King", "Mid King","Mid King","Mid King","Mid King","Mid King", "Mid King","Mid King","Mid King", "Low King","Low King","Low King", "Low King","Low King","Low King", "Low King","Low King", "Low King","Low King","Low King", "Low King","Mid King","Low King", "Low King","Low King", "Low King","Low King","Low King", "Low King","MAC","MAC","MAC","King","King", "King","Basin","Basin", ""),

labels = c("MAC01","MAC02", "MAC03","MAC04","MAC05","MAC06","MAC07", "MAC08","MAC09","MAC10","MAC11","MAC12","MAC13", "MAC14","MAC15","MAC16","MAC17","MAC18", "MAC20","MAC21","MAC22","MAC23","MAC24","MAC25", "MAC26","MAC27","MAC28","MAC29","MAC30", "MAC32","MAC33","MAC34","MAC35","MAC36","MAC37", "MAC38","MAC39","MAC40","MAC41","MAC42","MAC43", "MAC44","MAC45","MAC46","MAC47","MAC48", "MAC49","MAC50","MAC51","MAC52","MAC53","MAC54", "MAC55","MAC56","MAC57","MAC58","MAC59", "MAC60","MAC61","MAC62","MAC63","King01","King02", "King03","King04","King05","King06","King07", "King08","King09","King10","King11","King12", "King13","King14","King15","King16","King17", "King18","King19","King20","King21","King22", "King23","King24","King25","King26","King27", "King28","King29","King30","King31", "King31c","King36","King37","King38","King39", "King40","King41","King42","King43","King50", "King51","King52","King53","King54","King55", "King56","Mid Stream","Low Stream","Mac River", "Upp King","Mid King","Low King","MAC","King", "Basin"),

value = c(16611,1960,8, 183,15,0,242,1346,1780,19,1225,107,145,76, 10,233,216,14,2,238,103,75,2311,28106, 9308,3118,2959,NA,447,1127,64,939,141, 37083,NA,27775,3454,551,335,678,2,783,14, 367,398,160,184,1492,42,38,1101,4694,465, 142,570,2462,72,6,16,237,12356,0,80,2,0, 533,2465,3,40,816,187,28,9,87,283,613, 1,114,160,40,39,78,88,45,83,579,43,120, 227,69,123,118,7,384,5,249,614,37,27, 102,0,18339,1027,582,245,25634,27139,162, 25626,96298,46681,5461,1075,75090,168605, 81626, 0) )

----- End of Code -----

You'll notice that at the end at the end of the parents vector there is an additional empty string value (""); at the end of the labels vector there is an additional value ("Basin"); and there is finally an additional (0) value at the end of your value vector.

When you do this, you should see a 117th row in DF, like in the image below.

New_DF

Then, when you run your echart4r code, your chart should appear the way I think you meant it to.

----- Code -----

DF_tree <- data.tree::FromDataFrameNetwork(DF)

DF_tree %>% e_charts() %>% e_sunburst() %>% e_tooltip()

----- End of Code -----

Sunburst_Plot

I hope this has helped in some way.

Best, Andrew

tungttnguyen commented 1 year ago

Thanks a lot Andrew! Much appreciated! FYI, you can use datapasta and reprex packages to quickly create posts similar to mine

AndrewDisher commented 1 year ago

No problem, and thanks for the info about the packages!