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

如何在ggplot2中将颜色与线型合并为单个图例?

问题:合并ggplot中长度不等的图例

我想用颜色区分3个种群,用线型区分种群是否感染特定细菌,尝试合并两个图例时遇到问题。

初始代码与结果

初始代码:

ggplot(sds, aes(x= Temp, y=Fertile, group=Line, color=Population)) + 
  geom_errorbar(aes(ymin=Fertile-se, ymax=Fertile+se), width=.1) +
  geom_line(aes(linetype=Infection_Status))+
  geom_point()+labs(title="Fertility Thermal Limits", x= "Temperature", y= "Proportion of viable mating pairs") + 
  scale_x_continuous(n.breaks=6)

生成的图形:显示两个独立图例,一个对应3种种群颜色,另一个对应两种感染状态线型。

第一次尝试:手动标注图例

参考建议后尝试手动标注,代码如下:

ggplot(sds, aes(x= Temp, y=Fertile, group=Line, color=Line)) + 
  geom_errorbar(aes(ymin=Fertile-se, ymax=Fertile+se), width=.1) +
  geom_line()+
  geom_point()+labs(title="Fertility Thermal Limits", x= "Temperature", y= "Proportion of viable mating pairs") + 
  scale_x_continuous(n.breaks=6) + 
  scale_colour_manual(name  ="Line",labels=c("Nowra +", "Nowra -", "Brisbane +", "Brisbane -", "Cairns +", "Cairns -"), values = c("blue", "blue", "green", "green", "red", "red")) + 
  scale_linetype_manual(name  ="Line",labels=c("Nowra +", "Nowra -", "Brisbane +", "Brisbane -", "Cairns +", "Cairns -"), values=c("solid", "dotted", "solid", "dotted", "solid", "dotted"))

结果:颜色符合预期,但未感染组的线型不再是虚线,所有线条均为实线。

第二次尝试:重新添加线型映射

重新添加linetype=Infection_Status映射后代码:

ggplot(sds, aes(x= Temp, y=Fertile, group=Line, color=Line)) + 
  geom_errorbar(aes(ymin=Fertile-se, ymax=Fertile+se), width=.1) +
  geom_line(aes(linetype=Infection_Status))+
  geom_point()+labs(title="Fertility Thermal Limits", x= "Temperature", y= "Proportion of viable mating pairs") + 
  scale_x_continuous(n.breaks=6) + 
  scale_colour_manual(name  ="Line",labels=c("Nowra +", "Nowra -", "Brisbane +", "Brisbane -", "Cairns +", "Cairns -"), values = c("blue", "blue", "green", "green", "red", "red")) + 
  scale_linetype_manual(name  ="Line",labels=c("Nowra +", "Nowra -", "Brisbane +", "Brisbane -", "Cairns +", "Cairns -"), values=c("solid", "dotted", "solid", "dotted", "solid", "dotted"))

结果:图形中线型恢复正常(感染组实线、未感染组虚线),但图例仍显示为两个独立部分,且线型图例与实际分组不匹配。

解决建议

要合并颜色和线型图例,核心是让两个美学映射基于统一的分类逻辑,以下两种方法可实现需求:

方法1:创建复合分组变量

先在数据中生成结合种群与感染状态的复合变量,再将color和linetype均映射到此变量:

# 预处理数据,生成复合分组
library(dplyr)
sds <- sds %>%
  mutate(Group = paste(Population, Infection_Status, sep = " "))

# 绘图
ggplot(sds, aes(x= Temp, y=Fertile, group=Group, color=Group, linetype=Group)) + 
  geom_errorbar(aes(ymin=Fertile-se, ymax=Fertile+se), width=.1) +
  geom_line()+
  geom_point()+
  labs(title="Fertility Thermal Limits", x= "Temperature", y= "Proportion of viable mating pairs") + 
  scale_x_continuous(n.breaks=6) +
  # 自定义颜色与线型
  scale_colour_manual(values = c(
    "Nowra Infected"="blue", "Nowra Uninfected"="blue", 
    "Brisbane Infected"="green", "Brisbane Uninfected"="green", 
    "Cairns Infected"="red", "Cairns Uninfected"="red"
  )) +
  scale_linetype_manual(values = c(
    "Nowra Infected"="solid", "Nowra Uninfected"="dotted", 
    "Brisbane Infected"="solid", "Brisbane Uninfected"="dotted", 
    "Cairns Infected"="solid", "Cairns Uninfected"="dotted"
  )) +
  # 统一图例名称
  guides(color=guide_legend(title="Line"), linetype=guide_legend(title="Line"))

方法2:通过override.aes合并图例

无需修改原始数据,直接在guides()中手动合并图例:

ggplot(sds, aes(x= Temp, y=Fertile, group=Line, color=Population)) + 
  geom_errorbar(aes(ymin=Fertile-se, ymax=Fertile+se), width=.1) +
  geom_line(aes(linetype=Infection_Status))+
  geom_point()+
  labs(title="Fertility Thermal Limits", x= "Temperature", y= "Proportion of viable mating pairs") + 
  scale_x_continuous(n.breaks=6) +
  # 自定义颜色与线型
  scale_colour_manual(values = c("Nowra"="blue", "Brisbane"="green", "Cairns"="red")) +
  scale_linetype_manual(values = c("Infected"="solid", "Uninfected"="dotted")) +
  # 合并图例:将线型整合到颜色图例中,隐藏独立的线型图例
  guides(
    color = guide_legend(
      title="Line",
      override.aes = list(
        linetype = rep(c("solid", "dotted"), 3),
        color = rep(c("blue", "blue", "green", "green", "red", "red"), each=1)
      ),
      labels = c("Nowra +", "Nowra -", "Brisbane +", "Brisbane -", "Cairns +", "Cairns -")
    ),
    linetype = "none"
  )

以上两种方法都能实现颜色与线型图例的合并,且保证图形与图例的匹配。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.15 12:10:28