2016-10-04 202 views
0

我试图使用ggplotgeom_tile生成heatmap绘图。我的数据有比行多得多的行。获取geom_tile绘制正方形而不是矩形单元格

set.seed(1) 
    df <- data.frame(val=rnorm(100),gene=rep(letters[1:20],5),cell=c(sapply(LETTERS[1:5],function(l) rep(l,20)))) 

运行:

library(ggplot2) 
ggplot(df,aes(y=gene,x=cell,fill=val))+geom_tile(color="white") 

生产: enter image description here

如何获得heatmap细胞是对称的尺寸 - 方形而不是长方形(高度=宽度)?而不会扭曲图的尺寸。

回答

3

欢迎调整比率如下

set.seed(1) 
df <- data.frame(val=rnorm(100),gene=rep(letters[1:20],5), 
      cell=c(sapply(LETTERS[1:5],function(l) rep(l,20)))) 

library(ggplot2) 
p <- ggplot(df,aes(y=gene,x=cell,fill=val))+geom_tile(color="white") 
p <- p + coord_fixed(ratio = 0.7) 
p 

enter image description here

+0

但是,如果我的实际数据有更多行:df < - data.frame(val = rnorm(500),gene = rep(1:100,5),cell = c(sapply(LETTERS [1:5],function(l) rep(l,100)))),这大大缩小了这个数字。 – dan

+2

但是...你_want_广场。我不确定你打算如何在没有漫长阴谋的情况下获得广场。你可以'coord_flip()'它(我会在尝试之前交换x和y,因为它是相同的事物,并且更容易记住)。或者你可以将它分成2,3,4个图,但保持相同的色阶。 – hrbrmstr

6

一个选项是添加coord_equal

默认,比= 1,确保了在x轴上的一个单位是 相同的长度作为一个单元在y轴

ggplot(df, aes(y = gene, x = cell, fill = val)) + 
    geom_tile(color = "white") + 
    coord_equal() 

enter image description here