问题描述
我很容易用ggplot
绘制一个矩阵(从而给出一个热图):
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:
但是我想将矩阵旋转45度,就像在这里可以找到的一样:可视化并旋转矩阵.因此, X轴旁边的对角线.但是,他们使用R中没有ggplot
的图形.您是否知道如何使用ggplot
做到这一点?
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
?
推荐答案
您可以首先通过以下功能旋转矩阵(数据框):
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)
}
将数据框逆时针旋转90度
Rotate the data frame by 90 degrees counter clockwise by
test2 <- rotate(test, -90)
然后使用相同的代码绘制test2.
Then plot test2 by using the same code.
这篇关于将矩阵旋转45度并使用ggplot对其进行可视化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!