旋转ggplot切片热图的上三角 [英] Rotate upper triangle of a ggplot tile heatmap
问题描述
我已经绘制了这样的热图:
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")
这将绘制热图的上部三角形.我想绘制的是一个完全相同的三角形,但斜边为x-axis
.
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
.
我该怎么做?
修改:添加了可复制的示例
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")
预期的输出(手动旋转):
Expected output (rotated manually):
使用基本图image()
的相关帖子:
可视化和旋转矩阵
Related post using base plot image()
:
Visualising and rotating a matrix
可能的解决方案示例代码在 LDheatmap包中,使用grid
.
Possible solution example code is in LDheatmap package using grid
.
推荐答案
使用此解决方案将输出裁剪到底部,因此解决方法是添加额外的绘图边距,然后使用grid::viewport()
进行旋转:
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))
这篇关于旋转ggplot切片热图的上三角的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!