gpt4 book ai didi

旋转 ggplot 平铺热图的上三角形

转载 作者:行者123 更新时间:2023-12-01 04:50:59 25 4
gpt4 key购买 nike

我已经绘制了这样的热图:

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 .
enter image description here
我该怎么做?

编辑:添加了可重现的示例
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
预期输出(手动轮换):
enter image description here
使用基图的相关帖子 image() :
Visualising and rotating a matrix
可能的解决方案示例代码在 LDheatmap package 中使用 grid .

最佳答案

使用 this solution将输出剪裁在底部,因此解决方法是添加额外的绘图边距,然后使用 grid::viewport()旋转:

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

关于旋转 ggplot 平铺热图的上三角形,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41108399/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com