question about drawing heatmap with R
1
0
Entering edit mode
10.1 years ago
biolab ★ 1.4k

Dear all

I have a simple question about R drawing heatmap. I produced a heatmap using the following code, however, I want to swap the colors between yellow and red (i.e., by default yellow represent high value while red represent low values, i want to swap them).

My second question is: how to add white lines for the boundaries of each unit ? THANKS a lot!

n1 <- read.table(file="test.txt", header=T)
n1 <- data.matrix(n1)
heatmap.2(n1, trace="none",col=heat.colors(256))
heatmap r • 5.7k views
ADD COMMENT
1
Entering edit mode

I don't understand your second question but for your first question simply use

n1 <- read.table(file="test.txt", header=T)
n1 <- data.matrix(n1)
heatmap.2(n1, trace="none",col=rev(heat.colors(256)))
ADD REPLY
1
Entering edit mode

If you are using heatmap.2, then I think you use rowsep and/or colsep to draw lines between the entries in the body of the heatmap

ADD REPLY
2
Entering edit mode
10.1 years ago
arno.guille ▴ 420

Try this

n1 <- read.table(file="test.txt", header=T)
n1 <- data.matrix(n1)
col = heat.colors(256)
col = col[order(col,decreasing=T)]
heatmap.2(n1, trace="none",col=col)
ADD COMMENT
0
Entering edit mode

Hi Irsan and arno.guille, your comments are very helpful,but I still have problem: the picture color becomes white and red! I want it to be between yellow and red.

Sorry for my second unclear question. Each value is presented by a color cell in R heatmap, but by default the cell does not have boundary lines. I want to add white (or gray) boundary lines, and ideally I can adjust the width of the boundary line. THANKS a lot!

ADD REPLY
1
Entering edit mode

With the function colorRampPalette you can create your own gradient color.

For example:

col=colorRampPalette(c("yellow","red"))
col(100)

For the second question, I don't think it's possible to do it with R

ADD REPLY
1
Entering edit mode

You can't create the boundary lines with R, but you can use the rowsep or colsep to create white/grey lines between the cells.

This is just a vector of numeric values, which says how width the gap between two columns or rows should be.

# block sepration
           colsep,
           rowsep,
           sepcolor="white",
           sepwidth=c(0.05,0.05),

colsep, rowsep, sepcolor

(optional) vector of integers indicating which columns or rows should be separated from the preceding columns or rows by a narrow space of color `sepcolor`.

sepwidth

(optional) Vector of length 2 giving the width (colsep) or height (rowsep) the separator box drawn by colsep and rowsep as a function of the width (colsep) or height (rowsep) of a cell. Defaults to c(0.05, 0.05)
ADD REPLY

Login before adding your answer.

Traffic: 1560 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6