如何将Inset Plot的图例单独置于整张图表底部?
实现内嵌图图例单独显示在图表底部的方法
要实现这个需求,核心思路是先隐藏内嵌图自带的图例,提取图例后再将主图(含内嵌图)和图例垂直组合,具体步骤及修改后的代码如下:
步骤1:修正内嵌图的图例设置
首先修改你的inset绘图代码,把错误的legend.location参数改为legend.position = "none",让内嵌图本身不显示图例:
inset <- ggplot() + geom_sf(data = dat.eez.usa1, size = 1.5, aes(color = "eez"), fill = 'transparent')+ scale_color_manual(name = "", values = c(eez = "lightblue"), labels = c('EEZ')) + coord_sf(xlim = c(-117, -121.5), ylim = c(31.75, 36), expand = FALSE)+ scale_x_continuous(breaks = c(seq(from = -121, to = -117, by = 2))) + scale_y_continuous(breaks = c(seq(from = 32, to = 36, by = 2)))+ theme( strip.background = element_blank(), strip.text.x = element_text( size = 12, hjust = 0, face = "bold"), panel.border = element_rect(color = "red", fill = NA, linewidth = 1), axis.line.y = element_blank(), axis.line.x = element_blank(), axis.title.x=element_blank(), axis.title.y=element_blank(), axis.text.x = element_text(size = 6), axis.text.y = element_text(size = 6), legend.position = "none") # 隐藏内嵌图自带的图例
步骤2:提取内嵌图的图例
使用ggpubr包的get_legend()函数提取图例(未安装的话先运行install.packages("ggpubr")):
library(ggpubr) inset_legend <- get_legend( # 生成临时带图例的图表,用于提取样式匹配的图例 ggplot() + geom_sf(data = dat.eez.usa1, size = 1.5, aes(color = "eez"), fill = 'transparent')+ scale_color_manual(name = "", values = c(eez = "lightblue"), labels = c('EEZ')) + theme(legend.position = "bottom") # 设置图例横向显示 )
步骤3:组合主图(含内嵌图)与图例
用patchwork包(未安装先运行install.packages("patchwork"))将带内嵌图的主图和提取的图例垂直拼接,调整高度比例让布局更协调:
library(patchwork) # 创建带内嵌图的主图对象 main_with_inset <- ggdraw(bigger_plot) + draw_plot(inset, x = .086, y = 0.15, width = 0.45, height = 0.45) # 垂直组合布局:主图在上,图例在下 final_plot <- main_with_inset / inset_legend + plot_layout(heights = c(10, 1)) # 调整主图与图例的高度占比 # 查看最终图表 print(final_plot)
完整修改后的代码
# 安装依赖包(如果未安装) # install.packages(c("ggpubr", "patchwork", "sf", "rnaturalearth", "ggplot2", "ggdraw", "maps")) library(sf) library(rnaturalearth) library(ggplot2) library(ggdraw) library(ggpubr) library(patchwork) library(maps) path.eez.usa <- ("~/Downloads/EEZ_land_union_v3_202003/") fnam.eez.usa <- "EEZ_Land_v3_202030.shp" eez.usa <- st_read(dsn = path.eez.usa, layer = file_path_sans_ext(fnam.eez.usa)) dat.eez.usa1 <- fortify(eez.usa) world <- ne_countries(scale = "medium", returnclass = "sf") bigger_plot <- ggplot()+ geom_sf(data = world)+ borders("state") + scale_x_continuous(breaks = c(seq(from = -140, to = -114, by = 5))) + scale_y_continuous(breaks = c(seq(from = 29, to = 50, by = 2))) + coord_sf(xlim = c(-113, -140), ylim = c(29, 50.5), expand = FALSE)+ theme(strip.background = element_blank(), legend.position = 'bottom', strip.text.x = element_text( size = 12, hjust = 0, face = "bold"), axis.title.x=element_blank(), axis.title.y=element_blank()) # 修改内嵌图,隐藏自带图例 inset <- ggplot() + geom_sf(data = dat.eez.usa1, size = 1.5, aes(color = "eez"), fill = 'transparent')+ scale_color_manual(name = "", values = c(eez = "lightblue"), labels = c('EEZ')) + coord_sf(xlim = c(-117, -121.5), ylim = c(31.75, 36), expand = FALSE)+ scale_x_continuous(breaks = c(seq(from = -121, to = -117, by = 2))) + scale_y_continuous(breaks = c(seq(from = 32, to = 36, by = 2)))+ theme( strip.background = element_blank(), strip.text.x = element_text( size = 12, hjust = 0, face = "bold"), panel.border = element_rect(color = "red", fill = NA, linewidth = 1), axis.line.y = element_blank(), axis.line.x = element_blank(), axis.title.x=element_blank(), axis.title.y=element_blank(), axis.text.x = element_text(size = 6), axis.text.y = element_text(size = 6), legend.position = "none") # 提取内嵌图的图例 inset_legend <- get_legend( ggplot() + geom_sf(data = dat.eez.usa1, size = 1.5, aes(color = "eez"), fill = 'transparent')+ scale_color_manual(name = "", values = c(eez = "lightblue"), labels = c('EEZ')) + theme(legend.position = "bottom") ) # 组合主图(含内嵌图)和图例 main_with_inset <- ggdraw(bigger_plot) + draw_plot(inset, x = .086, y = 0.15, width = 0.45, height = 0.45) final_plot <- main_with_inset / inset_legend + plot_layout(heights = c(10, 1)) print(final_plot)
关键说明
- 原代码中
legend.location是错误参数,正确控制图例位置的参数是legend.position。 - 提取图例时需生成临时图表,确保图例的样式、颜色与内嵌图完全匹配。
- 通过
patchwork的/运算符实现垂直布局,plot_layout(heights)可灵活调整主图与图例的高度占比,优化视觉效果。
内容的提问来源于stack exchange,提问作者awray
相关产品推荐
相关产品推荐

