📜  将通用图例添加到 R 中的组合 ggplot2 图

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

将通用图例添加到 R 中的组合 ggplot2 图

在本文中,我们将讨论如何使用 ggplot2 包在 R 语言中创建具有共享图例的组合图。

要加入多个 ggplot2 绘图,我们使用 R 语言中 gridExtra 包的grid.arrange()函数。 grid.arrange()函数将框架转换为所需行数和列数的网格。然后我们在网格的不同部分放置不同的图,为同一帧中的多个图创建所需的外观。

句法:

在哪里,

  1. ncol:确定网格中的列数。
  2. nrow:确定网格中的行数。

例子:

这是使用 ggplot2 包的 grid.arrange()函数制作的组合图。

R
# Load ggplot2 and gridExtra
library("ggplot2") 
library("gridExtra")
  
# Create sample data
set.seed(5642)                             
sample_data <- data.frame(
  name = c("Geek1","Geek2","Geek3",
           "Geek4","Geeek5") ,
  value=c(31,12,15,28,45))
  
  
# Create both plot and store in variable
plot1<-ggplot(sample_data,
              aes(x = name, y = value, col=name)) +
              geom_point(size=4)
plot2<-ggplot(sample_data,
              aes(x = name, y = value, fill=name)) +
              geom_col()
  
# combine both plots using grid.arrange()
grid.arrange(plot1, plot2, ncol = 2)


R
# To remove legend
plot+ theme( legend.position = "none" ) 
  
# arrange side by side in a grid
grid.arrange( plot1 , plot2 , ncol=2)


R
get_only_legend <- function(plot) {
    
# get tabular interpretation of plot
plot_table <- ggplot_gtable(ggplot_build(plot)) 
    
#  Mark only legend in plot
legend_plot <- which(sapply(plot_table$grobs, function(x) x$name) == "guide-box") 
                              
# extract legend
legend <- plot_table$grobs[[legend_plot]]
                              
# return legend
return(legend) 
}


R
grid.arrange(combined_plot, legend, nrow = 2, heights = c(10, 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 gridExtra
library("ggplot2") 
library("gridExtra")
  
# Create both plot and store in variable without legend
plot1<-ggplot(sample_data,
              aes(x = name, y = value, col=name)) +
              geom_point(size=4)+
              theme(legend.position = "none")
plot2<-ggplot(sample_data,
              aes(x = name, y = value, fill=name)) +
              geom_col()+
              theme(legend.position = "none")
  
# combine both plots using grid.arrange()
combined_plot <- grid.arrange(plot1, plot2, ncol = 2)
  
# plot1 with legend
plot1_legend <- ggplot(sample_data,
              aes(x = name, y = value, col=name)) +
              geom_point(size=4)+
              theme(legend.position = "bottom")
  
# function to extract legend from plot
get_only_legend <- function(plot) {
  plot_table <- ggplot_gtable(ggplot_build(plot))
  legend_plot <- which(sapply(plot_table$grobs, function(x) x$name) == "guide-box")
  legend <- plot_table$grobs[[legend_plot]]
  return(legend)
}
                            
# extract legend from plot1 using above function
legend <- get_only_legend(plot1_legend)   
     
# final combined plot with shared legend
grid.arrange(combined_plot, legend, nrow = 2, heights = c(10, 1))


输出:

具有共享图例的组合图

要创建具有共享图例的组合图,我们遵循以下步骤:

第 1 步:使用不带任何图例的 grid.arrange()函数创建组合图。

R

# To remove legend
plot+ theme( legend.position = "none" ) 
  
# arrange side by side in a grid
grid.arrange( plot1 , plot2 , ncol=2)

第 2 步:创建一个函数,从 ggplot2 图中提取图例并将其作为 ggplot 元素返回。

R

get_only_legend <- function(plot) {
    
# get tabular interpretation of plot
plot_table <- ggplot_gtable(ggplot_build(plot)) 
    
#  Mark only legend in plot
legend_plot <- which(sapply(plot_table$grobs, function(x) x$name) == "guide-box") 
                              
# extract legend
legend <- plot_table$grobs[[legend_plot]]
                              
# return legend
return(legend) 
}

Step3:使用 grid.arrange()函数合并从上述函数获得的图例。

R

grid.arrange(combined_plot, legend, nrow = 2, heights = c(10, 1))

生成的图将是具有共享图例的组合图。

例子:

在这里,是使用带有共享图例的 grid.arrange()函数制作的组合图。

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 gridExtra
library("ggplot2") 
library("gridExtra")
  
# Create both plot and store in variable without legend
plot1<-ggplot(sample_data,
              aes(x = name, y = value, col=name)) +
              geom_point(size=4)+
              theme(legend.position = "none")
plot2<-ggplot(sample_data,
              aes(x = name, y = value, fill=name)) +
              geom_col()+
              theme(legend.position = "none")
  
# combine both plots using grid.arrange()
combined_plot <- grid.arrange(plot1, plot2, ncol = 2)
  
# plot1 with legend
plot1_legend <- ggplot(sample_data,
              aes(x = name, y = value, col=name)) +
              geom_point(size=4)+
              theme(legend.position = "bottom")
  
# function to extract legend from plot
get_only_legend <- function(plot) {
  plot_table <- ggplot_gtable(ggplot_build(plot))
  legend_plot <- which(sapply(plot_table$grobs, function(x) x$name) == "guide-box")
  legend <- plot_table$grobs[[legend_plot]]
  return(legend)
}
                            
# extract legend from plot1 using above function
legend <- get_only_legend(plot1_legend)   
     
# final combined plot with shared legend
grid.arrange(combined_plot, legend, nrow = 2, heights = c(10, 1))

输出: