📜  如何使用cowplot加入多个ggplot2图?

📅  最后修改于: 2022-05-13 01:55:17.717000             🧑  作者: Mango

如何使用cowplot加入多个ggplot2图?

在本文中,我们将看到如何使用 cowplot 连接多个 ggplot2 图。为了连接多个 ggplot2 图,我们使用 R 语言的cowplot包的plot_grid()函数。

示例 1:使用 plot_grid()函数组合的两个基本 ggplot2 图。

R
# Create sample data
set.seed(5642)                             
sample_data1 <- data.frame(
  name = c("Geek1","Geek2","Geek3",
           "Geek4","Geeek5") ,
  value=c(31,12,15,28,45))
  
sample_data2 <- data.frame(x = rnorm(400))
  
# Load ggplot2 and cowplot
library("ggplot2") 
library("cowplot")
  
# Create both plot and store in variable
plot1<-ggplot(sample_data1,
              aes(x = name, y = value)) +
geom_point(size=4)
  
plot2<-ggplot(sample_data2, aes(x = x)) +
geom_density(alpha=0.8)
plot_grid(plot1, plot2, labels = c('Plot1', 'Plot2'))


R
# Create sample data
set.seed(5642)                             
sample_data <- data.frame(
  name = c("Geek1","Geek2","Geek3",
           "Geek4","Geeek5") ,
  value = c(31,12,15,28,45))
  
# Load ggplot2 and cowplot
library("ggplot2") 
library("cowplot")
  
# Create both plot and store in variable
plot1<-ggplot(sample_data, aes(x=name, y=value)) +
geom_bar(stat = "identity")
  
plot2<-ggplot(sample_data, aes(x = name, y=value)) +
geom_segment( aes(x=name, xend=name, y=0, yend=value)) 
  
plot_grid(plot1,NULL, NULL, plot2, labels = c(
  'Plot1','','', 'Plot2'), ncol=2)


R
# Create sample data
set.seed(5642)                             
sample_data <- data.frame(name=c("Geek1","Geek2","Geek3",
                                 "Geek4","Geeek5") ,
                          value=c(31,12,15,28,45))
  
# Load ggplot2 and cowplot
library("ggplot2") 
library("cowplot")
  
# Create both plot and store in variable without legend
plot1<-ggplot(sample_data, aes(x=name, y=value, fill=name)) +
geom_bar(stat = "identity") +
theme(legend.position = "none")
  
plot2<-ggplot(sample_data, aes(x = name, y=value, fill=name)) +  
geom_point(aes(colour = factor(name)), size = 6)+
theme(legend.position = "none")
  
# combine both plot using plot_grid()
combined_plot<-plot_grid(plot1, plot2,ncol=2)
  
# extract legend from plot2
legend <- get_legend(
  plot1 +
    guides(color = guide_legend(nrow = 1)) +
    theme(legend.position = "bottom")
)
  
# Combine combined plot and legend using plot_grid()
plot_grid(combined_plot, legend,ncol=1,rel_heights = c(1, .1))


R
# Create sample data
set.seed(5642)                             
sample_data <- data.frame(name=c("Geek1","Geek2",
                                 "Geek3","Geek4","Geeek5") ,
                          value=c(31,12,15,28,45))
  
# Load ggplot2 and cowplot
library("ggplot2") 
library("cowplot")
  
# Create both plot and store in variable without legend
plot1<-ggplot(sample_data, aes(x=name, y=value, fill=name)) +
geom_bar(stat = "identity") +
theme(legend.position = "none")
  
plot2<-ggplot(sample_data, aes(x = name, y=value, fill=name)) +  
geom_point(aes(colour = factor(name)), size = 6)+
theme(legend.position = "none")
  
# combine both plot using plot_grid()
combined_plot<-plot_grid(plot1, plot2,ncol=2)
  
# create title for plot
title <- ggdraw() + 
  draw_label(
    "Two Plots together with shared title",
    fontface = 'bold',
    x = 0,
    hjust = 0,
    size = 24,
  )
# Combine combined plot and title using plot_grid()
plot_grid(title, combined_plot ,ncol=1,rel_heights = c(0.1, 1))


输出:

输出

示例 2:使用 NULL 作为绘图值在绘图之间创建间隙。

为了固定列数,我们使用 plot_grid()函数的 ncol 属性。



电阻

# Create sample data
set.seed(5642)                             
sample_data <- data.frame(
  name = c("Geek1","Geek2","Geek3",
           "Geek4","Geeek5") ,
  value = c(31,12,15,28,45))
  
# Load ggplot2 and cowplot
library("ggplot2") 
library("cowplot")
  
# Create both plot and store in variable
plot1<-ggplot(sample_data, aes(x=name, y=value)) +
geom_bar(stat = "identity")
  
plot2<-ggplot(sample_data, aes(x = name, y=value)) +
geom_segment( aes(x=name, xend=name, y=0, yend=value)) 
  
plot_grid(plot1,NULL, NULL, plot2, labels = c(
  'Plot1','','', 'Plot2'), ncol=2)

输出:

输出

示例 3:具有共享图例的并排图。

为了创建具有共享图例的图,我们创建没有图例的图并创建一个单独的图例并将它们组合起来。

电阻

# Create sample data
set.seed(5642)                             
sample_data <- data.frame(name=c("Geek1","Geek2","Geek3",
                                 "Geek4","Geeek5") ,
                          value=c(31,12,15,28,45))
  
# Load ggplot2 and cowplot
library("ggplot2") 
library("cowplot")
  
# Create both plot and store in variable without legend
plot1<-ggplot(sample_data, aes(x=name, y=value, fill=name)) +
geom_bar(stat = "identity") +
theme(legend.position = "none")
  
plot2<-ggplot(sample_data, aes(x = name, y=value, fill=name)) +  
geom_point(aes(colour = factor(name)), size = 6)+
theme(legend.position = "none")
  
# combine both plot using plot_grid()
combined_plot<-plot_grid(plot1, plot2,ncol=2)
  
# extract legend from plot2
legend <- get_legend(
  plot1 +
    guides(color = guide_legend(nrow = 1)) +
    theme(legend.position = "bottom")
)
  
# Combine combined plot and legend using plot_grid()
plot_grid(combined_plot, legend,ncol=1,rel_heights = c(1, .1))

输出:

输出

示例 4:具有共享标题的并排图

为了创建具有共享标题的绘图,我们创建没有标题的绘图并创建一个单独的标题并将它们组合起来。

电阻

# Create sample data
set.seed(5642)                             
sample_data <- data.frame(name=c("Geek1","Geek2",
                                 "Geek3","Geek4","Geeek5") ,
                          value=c(31,12,15,28,45))
  
# Load ggplot2 and cowplot
library("ggplot2") 
library("cowplot")
  
# Create both plot and store in variable without legend
plot1<-ggplot(sample_data, aes(x=name, y=value, fill=name)) +
geom_bar(stat = "identity") +
theme(legend.position = "none")
  
plot2<-ggplot(sample_data, aes(x = name, y=value, fill=name)) +  
geom_point(aes(colour = factor(name)), size = 6)+
theme(legend.position = "none")
  
# combine both plot using plot_grid()
combined_plot<-plot_grid(plot1, plot2,ncol=2)
  
# create title for plot
title <- ggdraw() + 
  draw_label(
    "Two Plots together with shared title",
    fontface = 'bold',
    x = 0,
    hjust = 0,
    size = 24,
  )
# Combine combined plot and title using plot_grid()
plot_grid(title, combined_plot ,ncol=1,rel_heights = c(0.1, 1))

输出:

输出