如何旋转 SAS SGPLOT 图中的 x 轴标签,使它们垂直(与 x 轴成 90 度)
How do I rotate the x-axis labels in a SAS SGPLOT graph so that they are perpendicular (at 90 degrees to x-axis)
是否可以在 SAS SGPLOT 中旋转 x 轴标签,使其垂直(与 x 轴成 90 度)? fitpolicy选项旋转45度,但这不是我需要的。
SAS 代码:
ods pdf style = custom file='';
proc sgplot data = filename;
series x = date y = unemployment_rate_15_24 / lineattrs = (color = CX963634 thickness = 2 pattern=solid);
series x = date y = unemployment_rate_15_ / lineattrs = (color = CX538DD5 thickness = 2 pattern=solid);
xaxis type=time fitpolicy=rotatethin notimesplit interval=year offsetmax=0 label = ' ' values = ("28FEB1995"d to "28FEB2015"d by year);
yaxis label = ' ' values = (0 to 18 by 2);
x2axis display=(nolabel novalues);
keylegend / location = inside position = topright;
label unemployment_rate_15_24 = "Youth (15-24 years) unemployment rate";
label unemployment_rate_15_ = "Overall (15+ years) unemployment rate";
run;
ods pdf close;
我也尝试过在 SAS 中使用图形模板语言。这是用 GTL 编写的等效代码:
proc template;
define statgraph mysgplot;
begingraph;
layout overlay /
cycleattrs=true
xaxisopts=(label=" " offsetmax=0 type=time timeopts=(tickvaluelist=(12842 13207 13573 13938 14303 14668 15034 15399 15764 16129 16495 16860 17225 17590 17956 18321 18686 19051 19417 19782 20147) viewmin=12842 viewmax=20147 tickvaluefitpolicy=rotatethin splittickvalue=false interval=year))
yaxisopts=(label=" " type=linear linearopts=(tickvaluelist=(0 2 4 6 8 10 12 14 16 18) viewmin=0 viewmax=18 ))
x2axisopts=(display=(ticks line) type=auto);
seriesplot x='date'n y='unemployment_rate_15_24'n / primary=true lineattrs=( color=cx963634 pattern=1 thickness=2) legendlabel="Youth (15-24 years) unemployment rate" NAME="SERIES";
seriesplot x='date'n y='unemployment_rate_15_'n / lineattrs=( color=cx538dd5 pattern=1 thickness=2) legendlabel="Overall (15+ years) unemployment rate" NAME="SERIES1";
discretelegend "SERIES" "SERIES1" / location=inside halign=right valign=top;
endlayout;
endgraph;
end;
run;
proc sgrender data = filename template=mysgplot;
run;
我认为不使用注释就不可能在 SGPLOT 中做到这一点。他们有一段很好的代码可以用注释 here.
来做到这一点
对于 GTL,在 9.4 中他们引入了 TICKVALUEROTATION
,需要与 TICKVALUEFITPOLICY=ROTATE|ROTATEALWAYS
和 SPLITTICKVALUE=FALSE
一起使用。看到这个:
data testdata;
call streaminit(7);
do timevar = 12900 to 16900 by 500;
age=10+rand('Normal',5,2);
output;
end;
format timevar date9.;
run;
proc template;
define statgraph mysgplot;
begingraph;
layout overlay /
cycleattrs=true
xaxisopts=(label=" " offsetmax=0 type=time
timeopts=(tickvaluerotation=VERTICAL TICKVALUEFITPOLICY=rotatealways splittickvalue=false) )
yaxisopts=(label=" " type=linear linearopts=(tickvaluelist=(0 2 4 6 8 10 12 14 16 18) viewmin=0 viewmax=18 ))
x2axisopts=(display=(ticks line) type=auto);
seriesplot x=timevar y=age
/ lineattrs=( color=cx538dd5 pattern=1 thickness=2) legendlabel="Overall (15+ years) unemployment rate" NAME="SERIES1";
discretelegend "SERIES" "SERIES1" / location=inside halign=right valign=top;
endlayout;
endgraph;
end;
run;
proc sgrender data = testdata template=mysgplot;
run;
我稍微简化了您的坐标轴以确保其正常工作,但我认为您的大部分坐标轴语句都没有问题。
在 9.3 或更早版本中,我认为您遇到了注释或 45 度问题。或者看看您是否可以说服 SCATTERPLOT 在轴外绘制。或者也许使用 SERIESPLOT 和 SCATTERPLOT 来绘制您自己的轴,但这似乎有点过头了。
上面的代码如下所示:
从SAS 9.4 TS1M3开始,只需一行简单的代码:
XAXIS VALUESROTATE=VERTICAL;
是否可以在 SAS SGPLOT 中旋转 x 轴标签,使其垂直(与 x 轴成 90 度)? fitpolicy选项旋转45度,但这不是我需要的。
SAS 代码:
ods pdf style = custom file='';
proc sgplot data = filename;
series x = date y = unemployment_rate_15_24 / lineattrs = (color = CX963634 thickness = 2 pattern=solid);
series x = date y = unemployment_rate_15_ / lineattrs = (color = CX538DD5 thickness = 2 pattern=solid);
xaxis type=time fitpolicy=rotatethin notimesplit interval=year offsetmax=0 label = ' ' values = ("28FEB1995"d to "28FEB2015"d by year);
yaxis label = ' ' values = (0 to 18 by 2);
x2axis display=(nolabel novalues);
keylegend / location = inside position = topright;
label unemployment_rate_15_24 = "Youth (15-24 years) unemployment rate";
label unemployment_rate_15_ = "Overall (15+ years) unemployment rate";
run;
ods pdf close;
我也尝试过在 SAS 中使用图形模板语言。这是用 GTL 编写的等效代码:
proc template;
define statgraph mysgplot;
begingraph;
layout overlay /
cycleattrs=true
xaxisopts=(label=" " offsetmax=0 type=time timeopts=(tickvaluelist=(12842 13207 13573 13938 14303 14668 15034 15399 15764 16129 16495 16860 17225 17590 17956 18321 18686 19051 19417 19782 20147) viewmin=12842 viewmax=20147 tickvaluefitpolicy=rotatethin splittickvalue=false interval=year))
yaxisopts=(label=" " type=linear linearopts=(tickvaluelist=(0 2 4 6 8 10 12 14 16 18) viewmin=0 viewmax=18 ))
x2axisopts=(display=(ticks line) type=auto);
seriesplot x='date'n y='unemployment_rate_15_24'n / primary=true lineattrs=( color=cx963634 pattern=1 thickness=2) legendlabel="Youth (15-24 years) unemployment rate" NAME="SERIES";
seriesplot x='date'n y='unemployment_rate_15_'n / lineattrs=( color=cx538dd5 pattern=1 thickness=2) legendlabel="Overall (15+ years) unemployment rate" NAME="SERIES1";
discretelegend "SERIES" "SERIES1" / location=inside halign=right valign=top;
endlayout;
endgraph;
end;
run;
proc sgrender data = filename template=mysgplot;
run;
我认为不使用注释就不可能在 SGPLOT 中做到这一点。他们有一段很好的代码可以用注释 here.
来做到这一点对于 GTL,在 9.4 中他们引入了 TICKVALUEROTATION
,需要与 TICKVALUEFITPOLICY=ROTATE|ROTATEALWAYS
和 SPLITTICKVALUE=FALSE
一起使用。看到这个:
data testdata;
call streaminit(7);
do timevar = 12900 to 16900 by 500;
age=10+rand('Normal',5,2);
output;
end;
format timevar date9.;
run;
proc template;
define statgraph mysgplot;
begingraph;
layout overlay /
cycleattrs=true
xaxisopts=(label=" " offsetmax=0 type=time
timeopts=(tickvaluerotation=VERTICAL TICKVALUEFITPOLICY=rotatealways splittickvalue=false) )
yaxisopts=(label=" " type=linear linearopts=(tickvaluelist=(0 2 4 6 8 10 12 14 16 18) viewmin=0 viewmax=18 ))
x2axisopts=(display=(ticks line) type=auto);
seriesplot x=timevar y=age
/ lineattrs=( color=cx538dd5 pattern=1 thickness=2) legendlabel="Overall (15+ years) unemployment rate" NAME="SERIES1";
discretelegend "SERIES" "SERIES1" / location=inside halign=right valign=top;
endlayout;
endgraph;
end;
run;
proc sgrender data = testdata template=mysgplot;
run;
我稍微简化了您的坐标轴以确保其正常工作,但我认为您的大部分坐标轴语句都没有问题。
在 9.3 或更早版本中,我认为您遇到了注释或 45 度问题。或者看看您是否可以说服 SCATTERPLOT 在轴外绘制。或者也许使用 SERIESPLOT 和 SCATTERPLOT 来绘制您自己的轴,但这似乎有点过头了。
上面的代码如下所示:
从SAS 9.4 TS1M3开始,只需一行简单的代码:
XAXIS VALUESROTATE=VERTICAL;