Change tile size for ggplot2 heatmaps

Viewed 1586

I'm making a heatmap in R using ggplot2 and I want to change the size of my tiles because the text in my heatmap doesn't fit in the cells. Below is my data and attempted plots.

library("ggplot2")
s = sprintf("dataset number %s", 1:9)
vars = sprintf("many many words here, here and here %s", 1:6)
data = data.frame(s = rep(s, 6), stringsAsFactors = FALSE)
data$variable = rep(vars, rep.int(9, 6))
data$variable = as.factor(data$variable)
data$value = rep.int(-1000, 54)
data$font = "plain"
for(v in (unique(data$s))) {
  ids = which(data$s %in% "dataset number 1")
  vals = data[ids, ]
  row = rownames(vals[vals$value %in% max(vals$value, na.rm = TRUE), ])
  data[row, ]$font = "bold"
}               

I'm making a heatmap like this

title = "Heatmap"
cbbPalette = list(grey = "#999999", black = "#000000", orange = "#E69F00", sky = "#56B4E9", green = "#009E73", yellow = "#F0E442", blue = "#0072B2", darko = "#D55E00", 
                  pink = "#CC79A7")

pdf(sprintf("./heatmap.pdf"))
heatmap = ggplot(data = data, aes(x = variable, y = s, fill = value)) +
  geom_tile(color = "black") + 
  scale_fill_gradient2(low = cbbPalette$pink, high = cbbPalette$green, mid = cbbPalette$grey, 
                       midpoint = 0, limit = c(-100,100), space = "Lab", 
                       name=title) +
  scale_x_discrete(limits = levels(data$variable)) +
  geom_vline(xintercept = 6 - 0.5, color = "white", size = 1) + 
  geom_vline(xintercept = 2 + 0.5, color = "white", size = 1) + 
  theme_minimal() + 
  theme(axis.text.x = element_text(angle = 45, vjust = 1, 
                                   size = 16, hjust = 1), legend.title = element_text(size = 18),
        axis.text.y = element_text(size = 16)) +
  coord_fixed()

#add numbers to cells
heatmap = heatmap + geom_text(aes(x = variable, y = s, label = value, fontface = font), color = cbbPalette$black, size = 3) +
  theme(
    axis.title.x = element_blank(),
    axis.title.y = element_blank(),
    panel.grid.major = element_blank(),
    panel.border = element_blank(),
    panel.background = element_blank(),
    axis.ticks = element_blank(),
    legend.justification = c(0.5, 0),
    legend.direction = "horizontal",
    legend.position = "top") + 
  guides(fill = guide_colorbar(barwidth = 7, barheight = 1,
                               title.position = "top", title.hjust = 0.5))
# Print the heatmap
print(heatmap)
dev.off()

And I get the following result -- heatmap

Other SO posts suggested to change width and height within geom_tile, but I get the following result:

heatmap = ggplot(data = data, aes(x = variable, y = s, fill = value)) +
  geom_tile(color = "black", aes(width = 2L)) + ...

wrong_heatmap

I also tried changing height in geom_tile as well as coord_fixed(ratio = 1L) but without any success.

Also, I know that I can simply change the size of the text and numbers will fit, but I need to keep the font size the same. I can also shorten the labels but assume this is not an option.

I would appreciate any help with this.

0 Answers
Related