[R] How to custom colorscale in treemap

Jim Lemon jim at bitwrit.com.au
Sat Apr 28 13:54:27 CEST 2012


On 04/28/2012 12:58 PM, nhbnhb wrote:
> Hi~I'm new to R. I'm following these two tutorials to make a tree map, but
> the color scale isn't quite what I wanted.
>
> For map of the market, the color scale starts from 0(red) to max(green),
> what I wanted is 0(Green)-1(Red)-max(green). What's the easiest way to
> achieve this kind of divergent color scale?
>
> How shall I make my own palette and use it in the map.market or tree map
> function?
>
Hi nhbnhb,
Here's one way to do it:

library(plotrix)
# get some values from 0 to 5
values<-runif(20,0,5)
# set everything to the color for 1
value_colors<-rep("red",20)
# get colors for all the values less than 1, but drop the first
value_colors[values<1]<-
  color.scale(c(1,values[values<1]),extremes=c("green","red"))[-1]
# get colors for all the values greater than 1, drop the first
value_colors[values>1]<-
  color.scale(c(1,values[values>1]),extremes=c("green","red"))[-1]

Jim



More information about the R-help mailing list