mtennekes / treemap

R package for treemap visualisation
64 stars 24 forks source link

Setting palette when it start at 0? #25

Closed ignacio82 closed 9 years ago

ignacio82 commented 9 years ago

I would like the color palette in the following example to start in red instead of yellow

 library(treemap)
 data(business)
 business$employees.growth <- business$employees - business$employees.prev + 15457
 treemap(business,
           index=c("NACE1", "NACE2"),
           vSize="employees",
           vColor="employees.growth",
           type="value",
           palette="RdYlGn")

Is that possible?

mtennekes commented 9 years ago

A "value" treemap always considers the color palette to be diverging. So one way to start with red is this dirty trick:

library(RColorBrewer)
RdYlGn <- brewer.pal(9, "RdYlGn")

treemap(business,
        index=c("NACE1", "NACE2"),
        vSize="employees",
        vColor="employees.growth",
        type="value",
        palette=c(rep("#000000", 8), RdYlGn))

Another option is to use a "manual" treemap:

treemap(business,
        index=c("NACE1", "NACE2"),
        vSize="employees",
        vColor="employees.growth",
        type="manual",
        palette="RdYlGn",
        range=c(00000,800000))

The downside is of course that you'll have to specify a range.

Please let me know if this solution is satisfying. If you want, I can relax the diverging-palette-constraint for "value" treemaps with another argument. This may be useful for https://github.com/timelyportfolio/d3treeR.

ignacio82 commented 9 years ago

Manual + d3treeR works but I think is not perfect. In this example, "2" should be yellow:

Imgur

It would be great to be able to specify minColor, midColor, and maxColor like in googleVis::gvisTreeMap

Thanks!

mtennekes commented 9 years ago

I'm not sure what you mean.

The underlying thought of "value" treemap is that 0 is always the midColorValue to speak in googleVis language. The minColorValue and maxColorValue are -max(abs(values)) and max(abs(values)) respectively.

So if one could specify minColor, midColor and maxColor, how would you like to map them to the range of values? In your example I think it is sufficient to add midColorValue, and set it to 2 instead of 0, isn't it?

ignacio82 commented 9 years ago

exactly, midColorValue would be yellow and =2 instead of 0

mtennekes commented 9 years ago

Ok, but what values do you want to map to the minColor and maxColor? I think there are three options:

  1. -max(abs(values)) and max(abs(values)), like the current behaviour of the "value" type
  2. min(values) and max(values)
  3. self defined, as with the range argument for the "manual" type
ignacio82 commented 9 years ago

option 2 or 3. I guess 3 is the most flexible one

mtennekes commented 9 years ago

It is possible now! Besides the range argument, which now only specifies the range of the legend colors, there is a mapping argument.

Please let me know if it works as you expected.

ignacio82 commented 9 years ago

Thanks @mtennekes ! Could you share an example?

mtennekes commented 9 years ago

Sure, see https://github.com/mtennekes/treemap/blob/master/examples/treemap.R#L172-L180.

ignacio82 commented 9 years ago

Thanks!