R: How to Draw heat map divided by a digonal line?

时光总嘲笑我的痴心妄想 提交于 2021-02-10 23:51:02

问题


I came across this article on Nature Methods which provided a very nice heatmap: http://www.nature.com/nmeth/journal/v12/n4/full/nmeth.3311.html

Different from other heat map is each rectangle is divided by a diagonal line, with 1 part represent the literature data and the other in-house data. I think this is a very nice way to compare the data. However, I do not know how to draw this pic in R. Does anyone have any clue on how to do this?

A small screenshot is provided below:

enter image description here

Below is a demo dataset of a 2*2 grid. I would like the colour of the four rectagles divided by Group.

Para1  Para2 Value Group
 A      D      0.2   A1
 A      E      0.4   A1
 B      D      0.56  A1
 B      E      0.32  A1
 A      D      0.7   B1
 A      E      0.16  B1
 B      D      0.12  B1
 B      E      0.71  B1

回答1:


I don't see anything elegant in heatmap or lattice::levelplot. Maybe someone knows how in ggplot.

Here is a brute force proof-of-concept:

d=data.frame(p1=rep(LETTERS[1:2],times=2,each=2),
             p2=rep(LETTERS[4:5],times=4),
             value=c(.2,.4,.56,.32,.7,.16,.12,.71),
             group=rep(c("A1","B1"),each=4))

x=as.numeric(d$p1)
y=as.numeric(d$p2)

plot(1,xlim=c(1,length(unique(x))+1),ylim=c(1,length(unique(y))+1),
  type="n",bty="n",xaxt="n",yaxt="n",xlab="",ylab="")

for(i in 1:nrow(d)) {
  if(d$group[i]=="A1") polygon(x[i]+c(0,1,1),y[i]+c(0,0,1),col=gray(d$value[i]))
  if(d$group[i]=="B1") polygon(x[i]+c(0,1,0),y[i]+c(0,1,1),col=gray(d$value[i]))
}

axis(1,at=sort(unique(x))+.5,labels=levels(d$p1),lty=0)
axis(2,at=sort(unique(y))+.5,labels=levels(d$p2),lty=0)

You'd probably want to add a color scale, and use something more colorful than my mapping of value to shades of gray (lower values are darker).



来源:https://stackoverflow.com/questions/30214043/r-how-to-draw-heat-map-divided-by-a-digonal-line

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!