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

如何在ggplot2的stat_function中结合线型与颜色生成组合图例

要实现颜色+线型同时区分曲线且合并为同一个图例,核心要满足两个规则:

  1. 两个美学属性(colour、linetype)映射到同一个分类变量(也就是你这里的11种数据类型名称)
  2. 两个scale的图例名称、分类顺序完全一致,ggplot会自动将两个属性的图例合并

修改后的完整代码如下:

library(gamlss)
library(ggplot2)
library(cowplot)

xlower= 50
xupper= 183

# 预定义11种线型,可根据视觉需求调整
line_types <- c("solid", "dashed", "dotted", "dotdash", "longdash", "twodash",
                "1F", "F1", "4C88C488", "12345678", "2288")
# 预定义颜色,和原设置保持一致
col_values <- c("Black","green", "blue","brown","darkgreen","red","cyan",
                "yellow4","aquamarine4","slateblue4","magenta4")
# 分类名称顺序统一,避免图例错位
type_breaks <- c("Observed", "CMCC.ESM2","ECEARTH3","ECEARTH3.CC","ECEARTH3.Veg",
                 "GFDL.CM4","GFDL.ESM4","MPI.ESM1.2.HR","MRI.ESM2","NorESM2.MM","TaiESM1")

plot1 <- ggplot(data.frame(x = c(xlower , xupper)), aes(x = x)) + 
  xlim(c(xlower , xupper)) + 
  stat_function(fun = dNO, args =list(mu= 85.433,sigma=2.208), aes(colour = "Observed", linetype = "Observed"))+
  stat_function(fun = dSN2, args =list(mu= 97.847,sigma=2.896,nu=5.882,log=FALSE), aes(colour = "CMCC.ESM2", linetype = "CMCC.ESM2"))+
  stat_function(fun = dIGAMMA, args =list(mu= 4.520,sigma=2.336,log=FALSE), aes(colour = "ECEARTH3", linetype = "ECEARTH3"))+
  stat_function(fun = dRG, args =list(mu= 93.062,sigma=2.233,log=FALSE), aes(colour = "ECEARTH3.CC", linetype = "ECEARTH3.CC"))+
  stat_function(fun = dRG, args =list(mu= 94.296,sigma=2.237,log=FALSE), aes(colour = "ECEARTH3.Veg", linetype = "ECEARTH3.Veg"))+
  stat_function(fun = dSHASH, args =list(mu= 112.241,sigma=12.133,nu=9.419,tau=9.743,log=FALSE), aes(colour = "GFDL.CM4", linetype = "GFDL.CM4"))+
  stat_function(fun = dRG, args =list(mu= 101.101,sigma=2.372,log=FALSE), aes(colour = "GFDL.ESM4", linetype = "GFDL.ESM4"))+
  stat_function(fun = dLO, args =list(mu= 73.086,sigma=1.485,log=FALSE), aes(colour = "MPI.ESM1.2.HR", linetype = "MPI.ESM1.2.HR"))+
  stat_function(fun = dNO, args =list(mu= 139.373,sigma=2.895), aes(colour = "MRI.ESM2", linetype = "MRI.ESM2"))+
  stat_function(fun = dLO, args =list(mu= 134.221,sigma=2.158,log=FALSE), aes(colour = "NorESM2.MM", linetype = "NorESM2.MM"))+
  stat_function(fun = dNO, args =list(mu= 107.372,sigma=2.232), aes(colour = "TaiESM1", linetype = "TaiESM1"))+
  scale_color_manual("Data Types", breaks = type_breaks, values = col_values)+
  # 新增线型映射scale,名称和分类顺序和颜色scale完全一致
  scale_linetype_manual("Data Types", breaks = type_breaks, values = line_types)+
  labs(x = "Monthly average Precipitation (mm)", y = "PDF") + 
  theme(plot.title = element_text(hjust = 0.5), 
        axis.title.x = element_text(face="plain", colour="black", size = 12),
        axis.title.y = element_text(face="plain", colour="black", size = 12),
        legend.title = element_text(face="plain", size = 10),
        legend.position = "none")

plot2 <- ggplot(data.frame(x = c(xlower , xupper)), aes(x = x)) + 
  xlim(c(xlower , xupper)) + 
  stat_function(fun = pNO, args =list(mu= 85.433,sigma=2.208), aes(colour = "Observed", linetype = "Observed"))+
  stat_function(fun = pSN2, args =list(mu= 97.847,sigma=2.896,nu=5.882,log=FALSE), aes(colour = "CMCC.ESM2", linetype = "CMCC.ESM2"))+
  stat_function(fun = pIGAMMA, args =list(mu= 4.520,sigma=2.336,log=FALSE), aes(colour = "ECEARTH3", linetype = "ECEARTH3"))+
  stat_function(fun = pRG, args =list(mu= 93.062,sigma=2.233,log=FALSE), aes(colour = "ECEARTH3.CC", linetype = "ECEARTH3.CC"))+
  stat_function(fun = pRG, args =list(mu= 94.296,sigma=2.237,log=FALSE), aes(colour = "ECEARTH3.Veg", linetype = "ECEARTH3.Veg"))+
  stat_function(fun = pSHASH, args =list(mu= 112.241,sigma=12.133,nu=9.419,tau=9.743,log=FALSE), aes(colour = "GFDL.CM4", linetype = "GFDL.CM4"))+
  stat_function(fun = pRG, args =list(mu= 101.101,sigma=2.372,log=FALSE), aes(colour = "GFDL.ESM4", linetype = "GFDL.ESM4"))+
  stat_function(fun = pLO, args =list(mu= 73.086,sigma=1.485,log=FALSE), aes(colour = "MPI.ESM1.2.HR", linetype = "MPI.ESM1.2.HR"))+
  stat_function(fun = pNO, args =list(mu= 139.373,sigma=2.895), aes(colour = "MRI.ESM2", linetype = "MRI.ESM2"))+
  stat_function(fun = pLO, args =list(mu= 134.221,sigma=2.158,log=FALSE), aes(colour = "NorESM2.MM", linetype = "NorESM2.MM"))+
  stat_function(fun = pNO, args =list(mu= 107.372,sigma=2.232), aes(colour = "TaiESM1", linetype = "TaiESM1"))+
  scale_color_manual("Data Types", breaks = type_breaks, values = col_values)+
  # 同样新增线型scale
  scale_linetype_manual("Data Types", breaks = type_breaks, values = line_types)+
  labs(x = "Monthly average Precipitation (mm)", y = "CDF") + 
  theme(plot.title = element_text(hjust = 0.5), 
        axis.title.x = element_text(face="plain", colour="black", size = 12),
        axis.title.y = element_text(face="plain", colour="black", size = 12),
        legend.title = element_text(face="plain", size = 10),
        legend.position = "right")

p <- plot_grid(plot1, plot2, labels = "")

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.24 01:06:03