1

I have plot quite easily a matrix (thus giving a heatmap) with ggplot like this:

test <- data.frame(start1=c(1,1,1,1,2,2,2,3,3,4),start2=c(1,2,3,4,2,3,4,3,4,4),logFC=c(5,5,1,0,8,0,5,2,4,3))
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")

Since I have only the lower part of the heatmap, it gives this plot:

enter image description here

But I would like to rotate the matrix 45 degrees, just like I can find here: Visualising and rotating a matrix. So, the diagonal next to the X axis. However, they use the graphics from R without ggplot. Do you have any idea how to do that with ggplot?

Community
  • 1
  • 1
user2979409
  • 773
  • 1
  • 12
  • 23
  • Are you re-asking the same question: http://stackoverflow.com/questions/41108399/ggplot-rotate-upper-triangle-of-a-heatmap? – MrFlick Dec 13 '16 at 17:26
  • It's a different one. – user2979409 Dec 13 '16 at 17:27
  • Originally I thought that you want the "hypotenuse" to be "down-going" from the upper-left cornerbut that's more of a 90 degree rotation. So what is it that you want exactly? And post a small dataset that resembles the structure of your data. – IRTFM Dec 13 '16 at 18:30
  • 1
    @Hack-R I've modified the post. – user2979409 Dec 13 '16 at 20:30
  • @41 I just want the diagonal you see in my heatmap to be the X axis, just like the post I said. i added a small dataset. – user2979409 Dec 13 '16 at 20:30
  • you could add a rasterGrob rotated by 45 degrees via annotation_custom, but that's unlikely to be pretty and kind of defeats the purpose of using ggplot2, since the mapping to scale will be dangerously fiddly. – baptiste Dec 13 '16 at 20:51

1 Answers1

1

You can first rotate the matrix (data frame) by the following function:

rotate <- function(df, degree) {
  dfr <- df
  degree <- pi * degree / 180
  l <- sqrt(df$start1^2 + df$start2^2)
  teta <- atan(df$start2 / df$start1)
  dfr$start1 <- round(l * cos(teta - degree))
  dfr$start2 <- round(l * sin(teta - degree))
  return(dfr)
}

Rotate the data frame by 90 degrees counter clockwise by

test2 <- rotate(test, -90)

Then plot test2 by using the same code.

Erol
  • 31
  • 1