将两个 ggplots 组合成一个具有共享图例的图
Combine two ggplots into one plot with shared legend
通过使用 R,是否可以将 2 个 ggplot 放在一起(即在同一个图上)?我希望显示共享同一 x 轴的 2 个不同数据的趋势,而不是将它们一个挨着一个放置,我想将它们整合到一个图中。
更具体地说,我有以下 2 个视觉效果:
require(tidyverse)
require(ggplot2)
df1<- data.frame(Ensembl_ID = c("ENSG00000000003", "ENSG00000000005", "ENSG00000000419", "ENSG00000000457", "ENSG00000000460", "ENSG00000000938", "ENSG00000000971", "ENSG00000001036", "ENSG00000001084", "ENSG00000001167" ), logFC.1 = c(0.147447019707984, 0.278643924528991, 0.00638502079233481, 0.00248371473862579, 0.0591639590814736, 0.289257808065979, -0.0139042150604349, 0.15210410748665, -0.0273174541997048, 0.0373813166759115), logFC.2 = c(0.14237211045168, -0.153847067952652, 0.00806519294435945, -0.0243298183425441, 0.0639184480028851, 0.279112646057397, -0.0517704622015086, 0.100033161692714, 0.105136768894399, 0.0509474174745926), logFC.3 = c(0.0692402101693023, -0.212626837128185, 0.0665466667502187, 0.0189664498456434, 0.073631371224761, -0.0642014520794086, 0.0115060035255512, 0.104767159584613, 0.140378485980222, 0.0814931176279395), logFC.4 = c(0.175916688982428, 0.160644030220114, 0.0862627141013101, 0.105179938123113, 0.128866411791584, -0.0988927171791539, 0.128758540724723, 0.0997656895899759, 0.345468063926355, 0.130898388184307 ), logFC.5 = c(0.144743421921328, 0.247159332221974, 0.0232237466183996, 0.0800788300610377, 0.178887735169961, -0.0592727391427514, -0.0723099661837084, 0.0387715967173523, -0.0607793368610136, 0.110464511693512), logFC.6 = c(0.0848187321362019, -0.299283590551811, 0.0366788808661408, 0.117632803700627, 0.0145148270035513, -0.0384916970002755, -3.35640771631606e-05, 0.0851895375297912, -0.00364050261322463, 0.0602143760128463), logFC.7 = c(0.305256444042024, 0.274308408751318, 0.0977066795857243, -0.0265659018074027, 0.136348613124811, -0.0938364533000299, -0.143634179166262, 0.139913812601005, 0.268708965044232, 0.133427360632365), logFC.8 = c(0.12744808339884, -0.285015311267508, 0.0459140048745496, -0.00976012971218515, 0.13292412700208, 0.184687147498946, 0.141155871544752, 0.165717944056239, 0.323358546432839, 0.0502386767987279), logFC.9 = c(0.286824598926274, 0.095530985319937, 0.101370835445593, 0.0352336819150421, 0.0573659992830985, 0.173977901095588, 0.214669936284809, 0.0486643748696862, 0.0322601740536419, 0.0873158516027886 ), sum = c(1.48406730973606, 0.105513874142178, 0.47215374197863, 0.298919568521957, 0.845621491684206, 0.572340444016291, 0.214437965390758, 0.934927384128027, 1.12357371065775, 0.74238101670299 ))
#first dot plot (only 1:10 column)
df <- df1 %>% select(1:10) %>% pivot_longer( cols = -Ensembl_ID )
mid <- mean(df$value)
p <- ggplot(df, aes(x = factor(Ensembl_ID), y = value, color=value)) + geom_point() + geom_jitter(size=4, position = position_jitter(width = 0.05, height = 0.05)) + scale_color_gradient2(midpoint=mid, low="blue", mid="grey", high="red", space ="Lab" )+ theme_classic()
p
a <- ggplot(df1, aes(x=Ensembl_ID, y=sum, fill=Ensembl_ID)) + geom_bar(stat="identity")
a
两者共享公共 x 轴,因此是否可以将 than 放在单个图中并从每个原始单个图中分配最终图的左右 y 轴?
提前致谢。
澄清后更新
require(tidyverse)
require(ggplot2)
library(ggeasy)
#install.packages("ggeasy")
df1<- data.frame(Ensembl_ID = c("ENSG00000000003", "ENSG00000000005", "ENSG00000000419", "ENSG00000000457", "ENSG00000000460", "ENSG00000000938", "ENSG00000000971", "ENSG00000001036", "ENSG00000001084", "ENSG00000001167" ), logFC.1 = c(0.147447019707984, 0.278643924528991, 0.00638502079233481, 0.00248371473862579, 0.0591639590814736, 0.289257808065979, -0.0139042150604349, 0.15210410748665, -0.0273174541997048, 0.0373813166759115), logFC.2 = c(0.14237211045168, -0.153847067952652, 0.00806519294435945, -0.0243298183425441, 0.0639184480028851, 0.279112646057397, -0.0517704622015086, 0.100033161692714, 0.105136768894399, 0.0509474174745926), logFC.3 = c(0.0692402101693023, -0.212626837128185, 0.0665466667502187, 0.0189664498456434, 0.073631371224761, -0.0642014520794086, 0.0115060035255512, 0.104767159584613, 0.140378485980222, 0.0814931176279395), logFC.4 = c(0.175916688982428, 0.160644030220114, 0.0862627141013101, 0.105179938123113, 0.128866411791584, -0.0988927171791539, 0.128758540724723, 0.0997656895899759, 0.345468063926355, 0.130898388184307 ), logFC.5 = c(0.144743421921328, 0.247159332221974, 0.0232237466183996, 0.0800788300610377, 0.178887735169961, -0.0592727391427514, -0.0723099661837084, 0.0387715967173523, -0.0607793368610136, 0.110464511693512), logFC.6 = c(0.0848187321362019, -0.299283590551811, 0.0366788808661408, 0.117632803700627, 0.0145148270035513, -0.0384916970002755, -3.35640771631606e-05, 0.0851895375297912, -0.00364050261322463, 0.0602143760128463), logFC.7 = c(0.305256444042024, 0.274308408751318, 0.0977066795857243, -0.0265659018074027, 0.136348613124811, -0.0938364533000299, -0.143634179166262, 0.139913812601005, 0.268708965044232, 0.133427360632365), logFC.8 = c(0.12744808339884, -0.285015311267508, 0.0459140048745496, -0.00976012971218515, 0.13292412700208, 0.184687147498946, 0.141155871544752, 0.165717944056239, 0.323358546432839, 0.0502386767987279), logFC.9 = c(0.286824598926274, 0.095530985319937, 0.101370835445593, 0.0352336819150421, 0.0573659992830985, 0.173977901095588, 0.214669936284809, 0.0486643748696862, 0.0322601740536419, 0.0873158516027886 ), sum = c(1.48406730973606, 0.105513874142178, 0.47215374197863, 0.298919568521957, 0.845621491684206, 0.572340444016291, 0.214437965390758, 0.934927384128027, 1.12357371065775, 0.74238101670299 ))
#first dot plot (only 1:10 column)
df <- df1 %>% select(1:10) %>% pivot_longer( cols = -Ensembl_ID )
mid <- mean(df$value)
p <- ggplot(df, aes(x = factor(Ensembl_ID), y = value, color=value)) +
geom_point() + geom_jitter(size=4, position = position_jitter(width = 0.05, height = 0.05)) +
scale_color_gradient2(midpoint=mid, low="blue", mid="grey", high="red", space ="Lab" )+
theme_classic() +
easy_remove_x_axis()
a <- ggplot(df1, aes(x=Ensembl_ID, y=sum, fill=Ensembl_ID)) + geom_bar(stat="identity")+
theme_classic()
library(cowplot)
plot_grid(a, p, labels = "AUT0")
plot_grid(p,a, align = "hv", ncol = 1, rel_heights = c(3/5, 2/5))
第一个答案:
我们可以使用 cowplot
包中的 plot_grid
:
#first dot plot (only 1:10 column)
df <- df1 %>% select(1:10) %>% pivot_longer( cols = -Ensembl_ID )
mid <- mean(df$value)
p <- ggplot(df, aes(x = factor(Ensembl_ID), y = value, color=value)) + geom_point() + geom_jitter(size=4, position = position_jitter(width = 0.05, height = 0.05)) + scale_color_gradient2(midpoint=mid, low="blue", mid="grey", high="red", space ="Lab" )+ theme_classic()
p
a <- ggplot(df1, aes(x=Ensembl_ID, y=sum, fill=Ensembl_ID)) + geom_bar(stat="identity")
a
library(cowplot)
plot_grid(a, b, labels = "AUT0")
通过使用 R,是否可以将 2 个 ggplot 放在一起(即在同一个图上)?我希望显示共享同一 x 轴的 2 个不同数据的趋势,而不是将它们一个挨着一个放置,我想将它们整合到一个图中。
更具体地说,我有以下 2 个视觉效果:
require(tidyverse)
require(ggplot2)
df1<- data.frame(Ensembl_ID = c("ENSG00000000003", "ENSG00000000005", "ENSG00000000419", "ENSG00000000457", "ENSG00000000460", "ENSG00000000938", "ENSG00000000971", "ENSG00000001036", "ENSG00000001084", "ENSG00000001167" ), logFC.1 = c(0.147447019707984, 0.278643924528991, 0.00638502079233481, 0.00248371473862579, 0.0591639590814736, 0.289257808065979, -0.0139042150604349, 0.15210410748665, -0.0273174541997048, 0.0373813166759115), logFC.2 = c(0.14237211045168, -0.153847067952652, 0.00806519294435945, -0.0243298183425441, 0.0639184480028851, 0.279112646057397, -0.0517704622015086, 0.100033161692714, 0.105136768894399, 0.0509474174745926), logFC.3 = c(0.0692402101693023, -0.212626837128185, 0.0665466667502187, 0.0189664498456434, 0.073631371224761, -0.0642014520794086, 0.0115060035255512, 0.104767159584613, 0.140378485980222, 0.0814931176279395), logFC.4 = c(0.175916688982428, 0.160644030220114, 0.0862627141013101, 0.105179938123113, 0.128866411791584, -0.0988927171791539, 0.128758540724723, 0.0997656895899759, 0.345468063926355, 0.130898388184307 ), logFC.5 = c(0.144743421921328, 0.247159332221974, 0.0232237466183996, 0.0800788300610377, 0.178887735169961, -0.0592727391427514, -0.0723099661837084, 0.0387715967173523, -0.0607793368610136, 0.110464511693512), logFC.6 = c(0.0848187321362019, -0.299283590551811, 0.0366788808661408, 0.117632803700627, 0.0145148270035513, -0.0384916970002755, -3.35640771631606e-05, 0.0851895375297912, -0.00364050261322463, 0.0602143760128463), logFC.7 = c(0.305256444042024, 0.274308408751318, 0.0977066795857243, -0.0265659018074027, 0.136348613124811, -0.0938364533000299, -0.143634179166262, 0.139913812601005, 0.268708965044232, 0.133427360632365), logFC.8 = c(0.12744808339884, -0.285015311267508, 0.0459140048745496, -0.00976012971218515, 0.13292412700208, 0.184687147498946, 0.141155871544752, 0.165717944056239, 0.323358546432839, 0.0502386767987279), logFC.9 = c(0.286824598926274, 0.095530985319937, 0.101370835445593, 0.0352336819150421, 0.0573659992830985, 0.173977901095588, 0.214669936284809, 0.0486643748696862, 0.0322601740536419, 0.0873158516027886 ), sum = c(1.48406730973606, 0.105513874142178, 0.47215374197863, 0.298919568521957, 0.845621491684206, 0.572340444016291, 0.214437965390758, 0.934927384128027, 1.12357371065775, 0.74238101670299 ))
#first dot plot (only 1:10 column)
df <- df1 %>% select(1:10) %>% pivot_longer( cols = -Ensembl_ID )
mid <- mean(df$value)
p <- ggplot(df, aes(x = factor(Ensembl_ID), y = value, color=value)) + geom_point() + geom_jitter(size=4, position = position_jitter(width = 0.05, height = 0.05)) + scale_color_gradient2(midpoint=mid, low="blue", mid="grey", high="red", space ="Lab" )+ theme_classic()
p
a <- ggplot(df1, aes(x=Ensembl_ID, y=sum, fill=Ensembl_ID)) + geom_bar(stat="identity")
a
提前致谢。
澄清后更新
require(tidyverse)
require(ggplot2)
library(ggeasy)
#install.packages("ggeasy")
df1<- data.frame(Ensembl_ID = c("ENSG00000000003", "ENSG00000000005", "ENSG00000000419", "ENSG00000000457", "ENSG00000000460", "ENSG00000000938", "ENSG00000000971", "ENSG00000001036", "ENSG00000001084", "ENSG00000001167" ), logFC.1 = c(0.147447019707984, 0.278643924528991, 0.00638502079233481, 0.00248371473862579, 0.0591639590814736, 0.289257808065979, -0.0139042150604349, 0.15210410748665, -0.0273174541997048, 0.0373813166759115), logFC.2 = c(0.14237211045168, -0.153847067952652, 0.00806519294435945, -0.0243298183425441, 0.0639184480028851, 0.279112646057397, -0.0517704622015086, 0.100033161692714, 0.105136768894399, 0.0509474174745926), logFC.3 = c(0.0692402101693023, -0.212626837128185, 0.0665466667502187, 0.0189664498456434, 0.073631371224761, -0.0642014520794086, 0.0115060035255512, 0.104767159584613, 0.140378485980222, 0.0814931176279395), logFC.4 = c(0.175916688982428, 0.160644030220114, 0.0862627141013101, 0.105179938123113, 0.128866411791584, -0.0988927171791539, 0.128758540724723, 0.0997656895899759, 0.345468063926355, 0.130898388184307 ), logFC.5 = c(0.144743421921328, 0.247159332221974, 0.0232237466183996, 0.0800788300610377, 0.178887735169961, -0.0592727391427514, -0.0723099661837084, 0.0387715967173523, -0.0607793368610136, 0.110464511693512), logFC.6 = c(0.0848187321362019, -0.299283590551811, 0.0366788808661408, 0.117632803700627, 0.0145148270035513, -0.0384916970002755, -3.35640771631606e-05, 0.0851895375297912, -0.00364050261322463, 0.0602143760128463), logFC.7 = c(0.305256444042024, 0.274308408751318, 0.0977066795857243, -0.0265659018074027, 0.136348613124811, -0.0938364533000299, -0.143634179166262, 0.139913812601005, 0.268708965044232, 0.133427360632365), logFC.8 = c(0.12744808339884, -0.285015311267508, 0.0459140048745496, -0.00976012971218515, 0.13292412700208, 0.184687147498946, 0.141155871544752, 0.165717944056239, 0.323358546432839, 0.0502386767987279), logFC.9 = c(0.286824598926274, 0.095530985319937, 0.101370835445593, 0.0352336819150421, 0.0573659992830985, 0.173977901095588, 0.214669936284809, 0.0486643748696862, 0.0322601740536419, 0.0873158516027886 ), sum = c(1.48406730973606, 0.105513874142178, 0.47215374197863, 0.298919568521957, 0.845621491684206, 0.572340444016291, 0.214437965390758, 0.934927384128027, 1.12357371065775, 0.74238101670299 ))
#first dot plot (only 1:10 column)
df <- df1 %>% select(1:10) %>% pivot_longer( cols = -Ensembl_ID )
mid <- mean(df$value)
p <- ggplot(df, aes(x = factor(Ensembl_ID), y = value, color=value)) +
geom_point() + geom_jitter(size=4, position = position_jitter(width = 0.05, height = 0.05)) +
scale_color_gradient2(midpoint=mid, low="blue", mid="grey", high="red", space ="Lab" )+
theme_classic() +
easy_remove_x_axis()
a <- ggplot(df1, aes(x=Ensembl_ID, y=sum, fill=Ensembl_ID)) + geom_bar(stat="identity")+
theme_classic()
library(cowplot)
plot_grid(a, p, labels = "AUT0")
plot_grid(p,a, align = "hv", ncol = 1, rel_heights = c(3/5, 2/5))
第一个答案:
我们可以使用 cowplot
包中的 plot_grid
:
#first dot plot (only 1:10 column)
df <- df1 %>% select(1:10) %>% pivot_longer( cols = -Ensembl_ID )
mid <- mean(df$value)
p <- ggplot(df, aes(x = factor(Ensembl_ID), y = value, color=value)) + geom_point() + geom_jitter(size=4, position = position_jitter(width = 0.05, height = 0.05)) + scale_color_gradient2(midpoint=mid, low="blue", mid="grey", high="red", space ="Lab" )+ theme_classic()
p
a <- ggplot(df1, aes(x=Ensembl_ID, y=sum, fill=Ensembl_ID)) + geom_bar(stat="identity")
a
library(cowplot)
plot_grid(a, b, labels = "AUT0")