我很确定这已经被问到了,但是我找不到它
问题
如何在无规ggplot的x和y轴上显示ggplots。
#devtools::install_github("omarwagih/ggseqlogo")
library(ggplot2)
library(ggseqlogo)
# sample data
set.seed(1)
seqs <- c("VVGARRSSWRVVSSI" ,"GPRSRSRSRDRRRKE", "LLCLRRSSLKAYGNG", "TERPRPNTFIIRCLQ", "LSRERVFSEDRARFY", "PSTSRRFSPPSSSLQ")
heat <- data.frame(pos1 = round(runif(n = 100, min = 1, max = 15)), pos2 = round(runif(n = 100, min = 1, max = 15)), value = runif(n = 100, min = 0, max = 1) )
# plot heatmap
heat.map <- ggplot(heat, aes(x=pos1, y = pos2)) +
geom_tile(aes(fill = value)) +
scale_fill_gradient(low = "blue", high = "red")
# plot sequence logo
logo <- ggseqlogo(seqs, method = 'p')
print(class(logo)) # --> [1] "gg" "ggplot"
现在,我想绘制热图并在两个轴上显示序列徽标,我可以使用grid.arrange(heat.map, logo, heights = c(0.7, 0.3))
在x轴上相对容易地显示它:
但是,我也无法弄清楚如何在y轴上显示序列徽标图。
我尝试了this tutorial:
# get legend
tmp <- ggplot_gtable(ggplot_build(logo))
leg <- which(sapply(tmp$grobs, function(x) x$name) == "guide-box")
legend <- tmp$grobs[[leg]]
# plot it
logo <- ggseqlogo(seqs, method = 'p') + guides(fill = F)
grid.arrange(logo, legend, heat.map, logo + coord_flip(), nrow = 2, ncol = 2)
答案 0 :(得分:1)
软件包ggExtra
能够按照散点图的两个轴绘制图,如其手册所述:
ggExtra是增强ggplot2的功能和层的集合。 旗舰功能是ggMarginal,可用于添加边际 直方图/箱线图/密度图到ggplot2散点图。
不幸的是,我找不到自己提供图的函数,因此我检查了源代码并提出了以下简单解决方案:
library(ggExtra)
grob <- ggplot2::ggplotGrob(heat.map)
grob <- ggExtra:::addTopMargPlot(grob, top = logo, size = 10)
grob <- ggExtra:::addRightMargPlot(grob, right = logo + coord_flip(), size = 10)
plot(grob)
希望它将对他人有所帮助!