Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

calculate color for higher level rectangles from weighted average of lower level when type='value' #8

Closed
ellisp opened this issue Jul 25, 2015 · 3 comments

Comments

@ellisp
Copy link

ellisp commented Jul 25, 2015

Consider the example below, converting a {treemap} object for use by d3tree2. When you click down to the level where the value that has been given to vColor exists, Borduria looks like this:

image

The average value/color intuitively should be 2.6, hence a pale yellow/green, but instead we get purple/10, more or less aribtrarily (I'm not sure exactly why, but could imagine reasons).

image

It would be very useful if in this case the color assigned to the higher level rectangles was a weighted average of the values assigned to color the level below, until you reach the rectangles that have values actually assigned from originally. (I might have a crack at this myself down the track, but not in the immediate next few weeks.)

library(treemap)
library(d3treeR)
library(dplyr)

mock <- data.frame(
   Country = rep(c("Ruritania", "Borduria"), 3),
   Industry = rep(c("Accommodation", "Manufacturing", "Flower growing"), each = 2),
   GDP = c(1000, 500, 20, 2000, 1800, 70), 
   Growth = c(-5, 4, -3, 2, -7, 10)
   )

tm <- treemap(mock, index = c("Country", "Industry"), vSize = "GDP", vColor = "Growth", 
               type = "value", palette = "Spectral")
d3tree2(tm, rootname = "World")

# what should the country colours be? Probably mean vColor value, weighted by vSize:
mock %>%
   group_by(Country) %>%
   summarise(AvGrowth = round(sum(GDP * Growth) / sum(GDP), 1))
@ignacio82
Copy link

Try adding fun.aggregate:

tm <- treemap(mock, index = c("Country", "Industry"), vSize = "GDP", vColor = "Growth", 
              type = "value", palette = "Spectral", 
              fun.aggregate="weighted.mean")
d3tree2(tm, rootname = "World")

@timelyportfolio
Copy link
Collaborator

Thanks @ignacio82. @ellisp treemap was not originally designed for this since colors only appear at the lowest level. Fortunately @mtennekes quickly added in mtennekes/treemap#18. Would love feedback.

@ellisp
Copy link
Author

ellisp commented Jul 25, 2015

Perfect, exactly what I was looking for, thanks all.

@ellisp ellisp closed this as completed Jul 25, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants