如何在SAS SGPLOT中为多分组系列设置自定义图例条目
解决方案:调整SAS SGPLOT图例显示
问题原因
你当前代码中使用group=t会让每个series语句生成对应t值的多条线,但默认图例只会展示t的取值,而非系列名称。以下提供两种符合需求的调整方式:
方案1:显示7个系列标签(每个标签包含对应t的两种线型)
将每个系列按t值拆分为两个独立的series语句,再通过keylegend的merge选项把同系列的两条线合并为一个图例项,同时保留颜色和线型区分:
proc sgplot data=natempdisdat3; by sex; /* 按t值分别绘制每个系列的实线/虚线,替换<t1>和<t2>为实际t的取值(比如0/1) */ series x=age y=LE / name='LE_solid' lineattrs=(thickness=2 color=black pattern=solid) where=(t=<t1>); series x=age y=LE / name='LE_dash' lineattrs=(thickness=2 color=black pattern=dash) where=(t=<t2>); series x=age y=WLE / name='WLE_solid' lineattrs=(thickness=2 color=gray pattern=solid) where=(t=<t1>); series x=age y=WLE / name='WLE_dash' lineattrs=(thickness=2 color=gray pattern=dash) where=(t=<t2>); series x=age y=DFLE / name='DFLE_solid' lineattrs=(thickness=2 color=GREEN pattern=solid) where=(t=<t1>); series x=age y=DFLE / name='DFLE_dash' lineattrs=(thickness=2 color=GREEN pattern=dash) where=(t=<t2>); series x=age y=NHNWLE / name='NHNWLE_solid' lineattrs=(thickness=2 color=aquamarine pattern=solid) where=(t=<t1>); series x=age y=NHNWLE / name='NHNWLE_dash' lineattrs=(thickness=2 color=aquamarine pattern=dash) where=(t=<t2>); series x=age y=NHWLE / name='NHWLE_solid' lineattrs=(thickness=2 color=orange pattern=solid) where=(t=<t1>); series x=age y=NHWLE / name='NHWLE_dash' lineattrs=(thickness=2 color=orange pattern=dash) where=(t=<t2>); series x=age y=HWLE / name='HWLE_solid' lineattrs=(thickness=2 color=bippk pattern=solid) where=(t=<t1>); series x=age y=HWLE / name='HWLE_dash' lineattrs=(thickness=2 color=bippk pattern=dash) where=(t=<t2>); series x=age y=HNWLE / name='HNWLE_solid' lineattrs=(thickness=2 color=crimson pattern=solid) where=(t=<t1>); series x=age y=HNWLE / name='HNWLE_dash' lineattrs=(thickness=2 color=crimson pattern=dash) where=(t=<t2>); /* 合并同系列的两条线为一个图例项 */ keylegend "LE_solid" "LE_dash" / merge label='LE' "WLE_solid" "WLE_dash" / merge label='WLE' "DFLE_solid" "DFLE_dash" / merge label='DFLE' "NHNWLE_solid" "NHNWLE_dash" / merge label='NHNWLE' "NHWLE_solid" "NHWLE_dash" / merge label='NHWLE' "HWLE_solid" "HWLE_dash" / merge label='HWLE' "HNWLE_solid" "HNWLE_dash" / merge label='HNWLE' location=inside position=topright across=1; run;
方案2:显示14个标签(每个标签对应「系列名称+t值」组合)
先将宽格式数据转为长格式,再通过groupby组合系列和t值,自动生成带t值的图例标签,这种方式代码更简洁易维护:
第一步:转换数据为长格式
data long_data; set natempdisdat3; length series $20.; /* 逐个映射系列名称与对应数值 */ series = 'LE'; value = LE; output; series = 'WLE'; value = WLE; output; series = 'DFLE'; value = DFLE; output; series = 'NHNWLE'; value = NHNWLE; output; series = 'NHWLE'; value = NHWLE; output; series = 'HWLE'; value = HWLE; output; series = 'HNWLE'; value = HNWLE; output; /* 保留需要的变量 */ keep sex age t series value; run;
第二步:绘制图形并生成带t值的图例
proc sgplot data=long_data; by sex; /* 用series+t作为分组,自动生成标签、匹配颜色和线型 */ series x=age y=value / groupby=(series t) lineattrs=(thickness=2) /* 为每个系列指定固定颜色 */ colorresponse=series colormodel=(black gray green aquamarine orange bippk crimson) /* 为每个t值指定线型 */ patternresponse=t patternmodel=(solid dash) /* 生成带t值的图例标签 */ legendlabel=catx(' (t=', t, ')'); /* 设置图例位置 */ keylegend / location=inside position=topright across=1; run;
内容的提问来源于stack exchange,提问作者Raf
相关产品推荐
相关产品推荐

