问题
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:
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
?
回答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.
来源:https://stackoverflow.com/questions/41127054/rotate-a-matrix-45-degrees-and-visualize-it-using-ggplot