Comments (6)
@jstrin, thanks so much for pointing this out as I have been meaning to clarify, since this can be both confusing and misleading. Your solution is very nice, so I appreciate both taking the time to find a solution and sharing it here.
The problem arises when a tree is pre-summed (generally not the case in my experience, but definitely the case with treemap
) and can be traced to this line. My proposed fix would be to only use size
at the leaf level, so the sums would be recalculated at all non-leaf nodes.
var root = hierarchy(json)
.sum(function(d) {
// only sum if no children (or is leaf)
if(!(d.children && d.children.length > 0)) return d[x.options.valueField || "size"];
});
This would be a potentially breaking change, so I'll let this sit for a while and seek input in case someone has an opinion. Please let me know if you see anything amiss.
For testing, we can use the following code.
library(sunburstR)
library(d3r)
df <- data.frame(
index1 = c(rep("A",3),"B"),
index2 = c(NA,"A.1","A.1",NA),
index3 = c(NA, NA, "A.1.1", NA),
size = c(5,5,5, 10),
stringsAsFactors = FALSE
)
sunburst(
d3_nest(df, value_cols="size"),
count = TRUE
)
... and should expect
This would be much easier if we can assume all data passed to sunburst
is pre-summed, but I think case this does not fit reality, so I prefer the above approach.
from sunburstr.
More for testing...
library(sunburstR)
library(d3r)
library(treemap)
library(dplyr)
df <- data.frame(
index1 = c(rep("A",3),"B"),
index2 = c(NA,"A.1","A.1",NA),
index3 = c(NA, NA, "A.1.1", NA),
size = c(5,5,5, 10),
stringsAsFactors = FALSE
)
sunburst(
d3_nest(df, value_cols="size"),
count = TRUE
)
rhd <- random.hierarchical.data()
sunburst(
d3_nest(rhd, value_cols="x"),
valueField = "x"
)
tm <- treemap(
rhd,
index = paste0("index", 1:3),
vSize = "x"
)$tm
sunburst(
tm %>%
select(index1, index2, index3, vSize) %>%
d3_nest(value_cols = "vSize"),
valueField = "vSize"
)
from sunburstr.
@jstrin b8a18d1 implements the proposed change. Would you mind testing with a couple trees on your side to see if it works as expected?
from sunburstr.
from @jstrin
@timelyportfolio sorry for the delayed response. I ran through a handful of tests including using data with truncated paths (e.g., NA values for one of the indices) and it looks good on my end. I'll re-run my test scripts after you push the updated d2b code.
from sunburstr.
Test code now
library(sunburstR)
library(d3r)
library(treemap)
library(dplyr)
df <- data.frame(
index1 = c(rep("A",3),"B"),
index2 = c(NA,"A.1","A.1",NA),
index3 = c(NA, NA, "A.1.1", NA),
size = c(5,5,5, 10),
stringsAsFactors = FALSE
)
sunburst(
d3_nest(df, value_cols="size"),
count = TRUE,
valueField = "size",
sumNodes = FALSE
)
rhd <- random.hierarchical.data()
sunburst(
d3_nest(rhd, value_cols="x"),
valueField = "x"
)
tm <- treemap(
rhd,
index = paste0("index", 1:3),
vSize = "x"
)$tm
sunburst(
tm %>%
select(index1, index2, index3, vSize) %>%
d3_nest(value_cols = "vSize"),
valueField = "vSize",
sumNodes = FALSE
)
from sunburstr.
going to close, since I believe the new sumNodes
argument introduced in 8bc0b1c will satisfy this need.
from sunburstr.
Related Issues (20)
- sund2b rootLabel malfunction when tooltip option used HOT 2
- no output HOT 4
- sunburst d2b & d3tree treemap fail to render together in shiny HOT 3
- Export sunburst plot as static vector image
- Have the legend open by default HOT 1
- Sizing problems of sund2b output in shiny window or browser with fluidPage HOT 2
- Radial color gradients within arcs HOT 3
- csv_to_hier not working as expected when only one element is spitted out by str_split HOT 18
- Sunburst function examples all fail "Error: Argument 1 must have names" HOT 6
- legend font HOT 1
- crosstalk support HOT 5
- sund2b - display root label on top, centered, and horizontal?
- Solution for manually coloring each leaf? HOT 7
- sunburst example is not working HOT 3
- rootLabel in sund2b is not working HOT 1
- warning when using sunburstOutput with shiny 1.7.4 (latest CRAN) HOT 3
- sunburst plot is rounding when it shouldn't v2.1.8 HOT 3
- custom sund2bBreadcrumb outputs text in all upper case HOT 1
- Modify sund2b breadcrumb size HOT 2
- Error when exporting HTML-plot
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from sunburstr.