Rotate upper triangle of a ggplot tile heatmap

2019-05-03 20:52发布

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.

enter image description here

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

enter image description here

Expected output (rotated manually):

enter image description here

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

Possible solution example code is in LDheatmap package using grid.

1条回答
劫难
2楼-- · 2019-05-03 21:30

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

enter image description here

查看更多
登录 后发表回答