简体   繁体   English

R; 并排绘制散点图和热图

[英]R; plotting scatter plot and heat map side by side

I am trying to create an image showing a scatter plot and a heat map side by side. 我正在尝试创建并排显示散点图和热图的图像。 I create the scatter plot with geom_point and the heatmap with heatmap.2. 我用geom_point创建散点图,并用heatmap.2创建热图。 I then use grid.draw to put them in the same image HOWEVER I cannot get the images to be the same size. 然后,我使用grid.draw将它们放在相同的图像中,但是我无法获得相同大小的图像。 How can I make sure they are the same height (this is important as they are ordered the same way and match each other)? 如何确定它们的高度相同(这很重要,因为它们的订购方式相同并且彼此匹配)?

The code I have is: 我的代码是:

    grab_grob <- function(){
       grid.echo()
       grid.grab()
  }
  g1 <- ggplot(x, aes(x=VIPscore, y=reorder(metabolite, VIPscore))) + geom_point(colour="blue") + labs(y="", x="VIP score")
  heatmap.2(xhm, cexRow=0.5, cexCol=1, Colv=FALSE, Rowv = FALSE, keep.dendro = FALSE, trace="none", key=FALSE, lwid = c(0.5, 0.5), col=heat.colors(ncol(xhm)))  
  g2 <- grab_grob()
  grid.newpage()
  lay <- grid.layout(nrow = 1, ncol=2)
  pushViewport(viewport(layout = lay))
  print(g1,vp=viewport(layout.pos.row = 1, layout.pos.col = 1))
  grid.draw(editGrob(g2, vp=viewport(layout.pos.row = 1, layout.pos.col = 2, clip=TRUE)))
  upViewport(1)

I have also tried the geom_tile (instead of heatmap.2) followed by grid.arrange; 我还尝试了geom_tile(而不是heatmap.2),然后尝试了grid.arrange;。 although the images now match in size colors are awful - they look flat across my data set. 尽管现在图像大小匹配的颜色非常糟糕-它们在我的数据集中看起来很平坦。

A package called plotly might be of help here. 一个叫做plotly的程序包在这里可能会有帮助。 Check out their API docs 查看他们的API文档

library(plotly)

df <- data.frame(x = 1:1000,
                 y = rnorm(1000))

p1 <- plot_ly(df, x = x, y = y, mode = "markers") 
p2 <- plot_ly(z = volcano, type = "heatmap")%>% layout(title = "Scatterplot and Heatmap Subplot")

subplot(p1, p2)

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM