Rotate upper triangle of a ggplot tile heatmap

醉酒当歌 提交于 2019-12-22 07:58:11

问题


I've plotted a heat-map like this:

ggplot(test, aes(start1, start2)) +
  geom_tile(aes(fill = logFC), colour = "gray", size=0.05) +
  scale_fill_gradientn(colours=c("#0000FF","white","#FF0000"), na.value="#DAD7D3")

This plots the upper triangle of a heatmap. What i'd like to plot is the very same triangle, but having the hypotenuse as the x-axis.

How would I do that?


Edit: Added reproducible example

library(ggplot2)

# dummy data
df1 <- mtcars[, c("gear","carb", "mpg")]

# normal tile plot
gg1 <- ggplot(df1, aes(gear, carb, fill = mpg)) +
  geom_tile() +
  xlim(c(1, 10)) +
  ylim(c(1, 10)) +
  theme_void() +
  theme(legend.position = "none")

Expected output (rotated manually):

Related post using base plot image(): Visualising and rotating a matrix

Possible solution example code is in LDheatmap package using grid.


回答1:


Using this solution gets the output clipped at the bottom, so workaround would be to add extra plot margins then use grid::viewport() to rotate:

library(ggplot2) #ggplot2_2.2.1
library(grid)

gg1 <- ggplot(df1, aes(gear, carb, fill = mpg)) +
  geom_tile() +
  xlim(c(1, 10)) +
  ylim(c(1, 10)) +
  theme_void() +
  # add extra margins
  theme(legend.position = "none",
        plot.margin = unit(c(1, 1, 1, 1), "cm")) 

# then rotate
print(gg1, vp = viewport(angle = 45))



来源:https://stackoverflow.com/questions/41108399/rotate-upper-triangle-of-a-ggplot-tile-heatmap

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