This is a test version of Biostars. For the public version, visit https://www.biostars.org.
question about drawing heatmap with R

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

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)))

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

1 answer

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)

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!

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

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)

Log in to answer this question.