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

使用ggplot2合并图例时部分模型分组未显示的技术求助

解决ggarrange合并子图后共同图例不全的问题

你遇到的核心问题是:每个子图的绘图数据仅包含两个模型,即便在scale_color_manual中定义了三个标签,ggplot会自动隐藏无对应数据的图例条目。因此两个子图各自的图例都只有两项,ggarrange合并后自然无法显示完整的三个模型。

方案1:修改scale_color_manual强制保留全图例

在每个子图的scale_color_manual中添加limits参数,指定所有模型的水平,强制ggplot保留完整图例,即使子图中没有对应数据。

修正后的代码:

library(ggplot2)
library(ggpubr)

cols <- c("red", "blue", "green")
# 子图1数据:模型A、B
df <- rbind(
  data.frame(model = "A", x = 10:1, val = runif(n = 10, min = 0, max = 1)),
  data.frame(model = "B", x = 10:1, val = runif(n = 10, min = 0, max = 1))
)

plot <- ggplot(df) +
  geom_point(aes(x = x, y = val, color = model), size = 2) +
  geom_line(aes(x = x, y = val, color = model), size = 1) +
  scale_x_reverse(limits = c(10, 0)) +
  scale_y_continuous(limits = c(0, 1)) +
  labs(y = "y轴", x = "x轴", color = "模型") +
  theme_bw() +
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank()) +
  scale_color_manual(
    limits = c("A", "B", "C"),  # 强制保留所有模型的图例条目
    labels = c("A", "B", "C"),
    values = cols
  )

# 子图2数据:模型A、C
gdf <- rbind(
  data.frame(model = "A", x = 10:1, val = runif(n = 10, min = 0, max = 1)),
  data.frame(model = "C", x = 10:1, val = runif(n = 10, min = 0, max = 1))
)

gplot <- ggplot(gdf) +
  geom_point(aes(x = x, y = val, color = model), size = 2) +
  geom_line(aes(x = x, y = val, color = model), size = 1) +
  scale_x_reverse(limits = c(10, 0)) +
  scale_y_continuous(limits = c(0, 1)) +
  labs(y = "y轴", x = "x轴", color = "模型") +
  theme_bw() +
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank()) +
  scale_color_manual(
    limits = c("A", "B", "C"),
    labels = c("A", "B", "C"),
    values = cols
  )

# 合并子图并使用共同图例
final_plot <- ggarrange(plot, gplot, nrow = 1, ncol = 2, common.legend = TRUE, legend = "bottom")
print(final_plot)

方案2:用facet替代ggarrange(更简洁)

将所有数据整合到一个数据集,添加子图分组列,用ggplot的facet_wrap实现分图,自动生成完整图例,无需额外处理。

代码示例:

library(ggplot2)
library(dplyr)

cols <- c("red", "blue", "green")
# 构建包含所有数据的数据集,添加subplot列区分两个子图
full_df <- bind_rows(
  # 子图1:模型A、B
  data.frame(subplot = "图1", model = "A", x = 10:1, val = runif(10)),
  data.frame(subplot = "图1", model = "B", x = 10:1, val = runif(10)),
  # 子图2:模型A、C
  data.frame(subplot = "图2", model = "A", x = 10:1, val = runif(10)),
  data.frame(subplot = "图2", model = "C", x = 10:1, val = runif(10))
)

# 用facet绘图,自动生成完整图例
final_plot <- ggplot(full_df, aes(x = x, y = val, color = model)) +
  geom_point(size = 2) +
  geom_line(size = 1) +
  scale_x_reverse(limits = c(10, 0)) +
  scale_y_continuous(limits = c(0, 1)) +
  labs(y = "y轴", x = "x轴", color = "模型") +
  theme_bw() +
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank()) +
  scale_color_manual(labels = c("A", "B", "C"), values = cols) +
  facet_wrap(~subplot, nrow = 1) +
  theme(legend.position = "bottom")

print(final_plot)

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.31 17:45:28