5

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.

Community
  • 1
  • 1
user2979409
  • 773
  • 1
  • 12
  • 23
  • @Gregor I think they/I want 45 degrees. – zx8754 May 24 '17 at 22:24
  • 1
    You'll have top lay around this, but @baptiste's answer to [this Q](https://stackoverflow.com/questions/24312713/rotate-a-ggplot2-plot-object) will get you close if you can resolve the clipping issues. – BrodieG May 24 '17 at 22:45
  • @BrodieG indeed close enough, but clipping issues. – zx8754 May 24 '17 at 22:48
  • @BrodieG Aha, add margins then rotate! Thank you. Do you want to add as an answer? – zx8754 May 24 '17 at 22:52
  • 1
    @zx8754 Feel free to add it yourself, you figured out the difficult part. I almost duped it but for the clipping problem, which you resolved. – BrodieG May 24 '17 at 23:00

1 Answers1

2

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

zx8754
  • 52,746
  • 12
  • 114
  • 209