Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting geom_tile to draw square rather than rectangular cells

Tags:

r

ggplot2

heatmap

I'm trying to generate a heatmap plot using ggplot's geom_tile. My data have far more rows than columns.

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

Running:

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

produces: enter image description here

How do I get the heatmap cells to be of symmetric dimensions - squares instead of rectangles (height=width)? without distorting the dimensions of the figure.

like image 445
dan Avatar asked Oct 04 '16 18:10

dan


1 Answers

An option is to add coord_equal.

The default, ratio = 1, ensures that one unit on the x-axis is the same length as one unit on the y-axis

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

enter image description here

like image 102
mpalanco Avatar answered Sep 21 '22 03:09

mpalanco