You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用R的patchwork包布局绘图时图例未显示的问题

问题:patchwork布局中图例缺失,无法在指定位置显示

我尝试用patchwork的guide_area()和plot_layout(guides = "collect")来把共享图例放在指定的guide space中心位置,但图例却不见了。我期望图例出现在下图红色问号所在的位置(两个绘图区域上方的中心区域)。

library(ggplot2)
library(patchwork)

# 创建四个带共同图例的示例图,先隐藏各自图例
p1 <- ggplot(mtcars, aes(x = wt, y = mpg, color = factor(cyl))) + 
  geom_point() + 
  theme(legend.position = "none")

p2 <- ggplot(mtcars, aes(x = hp, y = mpg, color = factor(cyl))) + 
  geom_point() + 
  theme(legend.position = "none")

p3 <- ggplot(mtcars, aes(x = qsec, y = mpg, color = factor(cyl))) + 
  geom_point() +
  theme(legend.position = "none")

p4 <- ggplot(mtcars, aes(x = disp, y = mpg, color = factor(cyl))) + 
  geom_point() +
  theme(legend.position = "none")

# 定义布局模板
design2 <- 
"
ABC
DEE
"

# 组合绘图并设置布局
p1 + p2 + p3 + p4 + guide_area() +
  plot_layout(guides = "collect", widths = c(5, 5, 5), heights = c(5, 5), design = design2) 

当前布局效果(图例缺失,问号处为期望的图例位置)

内容的提问来源于stack exchange,提问作者Giuseppe Petri

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.17 10:37:05