Search code examples
rtreemapcolorbrewer

RColorBrewer Treemap package R, Change color for neutral value?


I would like to change the color of my boxes created in the treemap below. Like the code is written now all my boxes get colored green cause all values are higher than zero. How can I change the "neutral color value" in the palette so 100 represent the neutral value? In this case the boxes with a value smaller than 100 should go in the red color.

To just get the colors right I could take the value and subtract it by 100 but I also want my numbers to be correct.

Any and all help will be greatly appreciated.

Code:

library(treemap)
library(RColorBrewer)

name = c('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h')
weight = c(53796, 6897, 12928, 19838, 22745, 13456, 2333, 17567) 
value = c(79, 87, 73, 109, 85, 76, 91, 104)

df = data.frame(name, weight, value)

treemap(df,
        index="name",
        vSize="weight",
        vColor="value",
        type="value",
        palette = brewer.pal(n=8, "RdYlGn"))

Solution

  • Add mapping, e.g.:

    treemap(df,
            index="name",
            vSize="weight",
            vColor="value",
            type="value",
            palette = brewer.pal(n=8, "RdYlGn"),
            mapping = c(min(value), 100, max(value)))
    

    enter image description here