Quantcast
Channel: R-bloggers » Xianjun Dong
Viewing all articles
Browse latest Browse all 8

How to correctly set color in the image() function?

$
0
0

(This article was first published on One Tip Per Day, and kindly contributed to R-bloggers)

Sometimes we want to make our own heatmap using image() function. I recently found it’s tricky to set the color option there, as its manual has very little information on col:


col
a list of colors such as that generated by rainbowheat.colorstopo.colorsterrain.colors or similar functions.

I posted my question on BioStars. The short answer is: Unless the breaks is set, the range of Z is evenly cut into N intervals (where N = the length of color) and values in Z are assigned to the color of corresponding interval. For example, when x=c(3,1,2,1) and col=c(“blue”,”red”,’green’,’yellow’), the minimal of x is assigned as the first color, and max to the last color. Any value between is calculated proportionally to a color. In this case, 2 is the the middle one, according to the principal that intervals are closed on the right and open on the left, it’s assigned to “red”. So, that’s why we see the colors are yellow–>blue–>red–>blue.

In practice, unless we want to manually define the color break points, we just set the first and last color, it will automatically find colors for the values in Z.

collist<-c(0,1)
image(1:ncol(x),1:nrow(x), as.matrix(t(x)), col=collist, asp=1)

If we want to manually define the color break points, we need to

x=matrix(rnorm(100),nrow=10)*100
xmin=0; xmax=100;
x[x<xmin]=xmin; x[x>xmax]=xmax;
collist<-c(“#053061″,”#2166AC”,”#4393C3″,”#92C5DE”,”#D1E5F0″,”#F7F7F7″,”#FDDBC7″,”#F4A582″,”#D6604D”,”#B2182B”,”#67001F”)
ColorRamp<-colorRampPalette(collist)(10000)
ColorLevels<-seq(from=xmin, to=xmax, length=10000)
ColorRamp_ex <- ColorRamp[round(1+(min(x)-xmin)*10000/(xmax-xmin)) : round( (max(x)-xmin)*10000/(xmax-xmin) )]
par(mar=c(2,0,2,0), oma=c(3,3,3,3))
layout(matrix(seq(2),nrow=2,ncol=1),widths=c(1),heights=c(3,0.5))
image(t(as.matrix(x)), col=ColorRamp_ex, las=1, xlab=””,ylab=””,cex.axis=1,xaxt=”n”,yaxt=”n”)
image(as.matrix(ColorLevels),col=ColorRamp, xlab=””,ylab=””,cex.axis=1,xaxt=”n”,yaxt=”n”)
axis(1,seq(xmin,xmax,10),seq(xmin,xmax,10))

To leave a comment for the author, please follow the link and comment on his blog: One Tip Per Day.

R-bloggers.com offers daily e-mail updates about R news and tutorials on topics such as: visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...

Viewing all articles
Browse latest Browse all 8

Latest Images

Trending Articles



Latest Images