R语言 我如何通过将这些图彼此靠近来整合它们?

a9wyjsp7  于 2022-12-06  发布在  其他
关注(0)|答案(2)|浏览(154)

我有这两个图(一个热图和一个堆叠条形图)。我怎样才能把这两个图放在一起呢?我确实想把这两个图整合起来。
谢谢你的帮助。
下面是我的脚本:

df.bar <- data.frame(Gene.name=c('Gene1','Gene1','Gene1','Gene2','Gene2','Gene2','Gene3','Gene3','Gene3',
                                 'Gene4','Gene4','Gene4'),
                     Alteration.frequency=c(0.0909, 2.1838, 0.6369, 0.1819, 1.0919, 0.3639, 0.4549,0.7279,
                                            0.7279, 0.000, 0.3639, 0.4549),
                     Alteration.Type=c("Deep deletion",  "Amplification",  "Point mutation", "Deep deletion",
                                       "Amplification",  "Point mutation", "Deep deletion",  "Amplification" ,
                                       "Point mutation", "Deep deletion",  "Amplification" , "Point mutation"))

library(ggplot2)
ggplot(df.bar,
       aes(fill=factor(Alteration.Type, levels = c('Point mutation','Amplification','Deep deletion')),
           y=Alteration.frequency, x=Gene.name)) + 
  geom_bar(position="stack", stat="identity")+theme_bw()+
  theme(axis.text.x = element_text(size = 10, angle = 45, hjust = 1, colour = 'black'))+
  scale_fill_manual(values=c("seagreen2", "maroon2",'deepskyblue3'))+
  labs(fill = 'Alteration type')

df.heatmap <- data.frame(Gene_name=c('Gene1','Gene2','Gene3','Gene4'),
                 log2FC=c(0.56,-1.5,-0.8,2))
library(gplots)
heatmap.2(cbind(df.heatmap$log2FC, df.heatmap$log2FC), trace="n", Colv = NA, 
          dendrogram = "none", labCol = "", labRow = df$Gene_name, cexRow = 0.75,
          col=my_palette)

我需要这样的情节。

ojsjcaue

ojsjcaue1#

有多种方法可以将heatmap.2之类的基本R图形和ggplot之类的基于网格的图形混合在一起,但这种情况稍微复杂一些,因为heatmap.2似乎使用了多个绘图区域。

library(gplots)
library(ggplot2)
library(patchwork)
library(gridGraphics)

p1 <- ggplot(df.bar,
       aes(fill = factor(Alteration.Type, levels = c('Point mutation',
                                                     'Amplification', 
                                                     'Deep deletion')),
           y = Alteration.frequency, x = Gene.name)) + 
  geom_col(position = "stack") +
  theme_bw() +
  theme(axis.text.x = element_text(size = 10, angle = 45, hjust = 1, 
                                   colour = 'black')) +
  scale_fill_manual(values = c("seagreen2", "maroon2", 'deepskyblue3')) +
  labs(fill = 'Alteration type')

heatmap.2(cbind(df.heatmap$log2FC, df.heatmap$log2FC), trace = "n", 
          Colv = NA, dendrogram = "none", labCol = "", 
          labRow = df.heatmap$Gene_name, cexRow = 0.75, 
          col = colorRampPalette(c("red", "white", "blue")))

grid.echo()
p2 <- wrap_elements(grid.grab())

p1 + p2

编辑

从评论来看,我们似乎希望一个图在另一个图的上面,OP使用heatmap.2,因为似乎不可能在ggplot中获得单个变量热图。
此编辑解决了以下两个问题:

library(ggplot2)
library(patchwork)

p1 <- ggplot(df.bar,
       aes(fill = factor(Alteration.Type, levels = c('Point mutation',
                                                     'Amplification', 
                                                     'Deep deletion')),
           y = Alteration.frequency, x = Gene.name)) + 
  geom_col(position = "stack") +
  theme_bw() +
  theme(axis.text.x = element_blank()) +
  scale_fill_manual(values = c("seagreen2", "maroon2", 'deepskyblue3')) +
  labs(fill = 'Alteration type', x = NULL)

p2 <- ggplot(df.heatmap, aes(y = "A", x = Gene_name,
                             fill = log2FC)) +
  geom_tile() +
  scale_fill_viridis_c() +
  scale_y_discrete(NULL, position = "right") +
  scale_x_discrete(NULL, expand = c(0.17, 0.1)) +
  theme_minimal() +
  theme(axis.text.y = element_blank(),
        plot.title = element_blank())

p1 / p2 + plot_layout(height = c(3, 1))

vlju58qv

vlju58qv2#

另一个选择是使用cowplot包中的plot_grid来合并和basegraphics或其他框架。对于热图,您可以使用~,我稍微修改了图例的大小以更好地适应它。下面是一个可重现的示例:

library(ggplot2)
library(cowplot)
library(gridGraphics)
library(gplots)

# first plot
p1 <- ggplot(df.bar,
       aes(fill=factor(Alteration.Type, levels = c('Point mutation','Amplification','Deep deletion')),
           y=Alteration.frequency, x=Gene.name)) + 
  geom_bar(position="stack", stat="identity")+theme_bw()+
  theme(axis.text.x = element_text(size = 10, angle = 45, hjust = 1, colour = 'black'))+
  scale_fill_manual(values=c("seagreen2", "maroon2",'deepskyblue3'))+
  labs(fill = 'Alteration type')

# Second plot
p2<- ~heatmap.2(cbind(df.heatmap$log2FC, df.heatmap$log2FC), trace="n", Colv = NA, 
          dendrogram = "none", labCol = "", labRow = df.heatmap$Gene_name, cexRow = 0.75,
          col=my_palette, key.par=list(mar=c(3,0,3,0), cex.main = 0.75), keysize = 1.5)

# combine all plots together
plot_grid(p1, p2)

创建于2022年12月3日,使用reprex v2.0.2

相关问题