Sas 如何在proc sgplot中按降序创建单个条形图?
我使用Sas 如何在proc sgplot中按降序创建单个条形图?,sas,sgplot,Sas,Sgplot,我使用sgplot proc sgplot data=Colordistricts; hbar distrct/response=Percent group= population; run; 然而,在图表中,各个群体似乎是按字母顺序排列的(亚洲人,然后是黑色和白色) 我如何用人口组按百分比降序创建相同的图 事实上,这些地区的有色人种最多。基本上,我想创建一个图形,使每个条以颜色填充开始。要强制特定的组值到第一个位置,您可以将所需的组映射到一个新值,该值将首先进行整理。有时,将空格字符
sgplot
proc sgplot data=Colordistricts;
hbar distrct/response=Percent
group= population;
run;
然而,在图表中,各个群体似乎是按字母顺序排列的(亚洲人,然后是黑色和白色)
我如何用人口组按百分比降序创建相同的图
事实上,这些地区的有色人种最多。基本上,我想创建一个图形,使每个条以颜色填充开始。要强制特定的组值到第一个位置,您可以将所需的组映射到一个新值,该值将首先进行整理。有时,将空格字符放在现有值的前面可以很容易地做到这一点 如果组变量是自定义格式的数字ID,以显示关联的组标签,则可以创建自定义格式的新版本,以包含与强制组相对应的0 ID。强制组已映射到0 id 然后,您将按照需要的特定方式对数据进行排序,并使用SGPLOT
yaxis type=discrete discreteOrder=data
强制hbar类别按特定顺序显示
下面是一些要探索的示例代码。最后的SGPLOT使用映射技术强制特定的种群片段首先出现
ods html close;
%let path = %sysfunc(pathname(work));
ods html file="&path.\sgplot_hbar.html" gpath="&path.";
proc format;
value popId
0 = 'Color'
1 = 'Asian'
2 = 'Black'
3 = 'Color'
4 = 'White'
;
data have;
do _n_ = rank('A') to rank('P');
district = byte (_n_);
x = 0;
populationID = 2; percent = ceil(40*ranuni(123)); output;
x + percent;
populationID = 3; percent = ceil(40*ranuni(123)); output;
x + percent;
if (ranuni(123) < 0.10) then do;
populationID = 1; percent = ceil(10*ranuni(123)); output;
x + percent;
end;
percent = 100 - x;
populationID = 4;
output;
end;
keep district populationID percent;
label
percent = 'Percent of Total Frequency'
;
format
populationID popId.
;
run;
proc sgplot data=have;
hbar district
/ group = populationID
response = percent
;
title j=L 'default group order by populationID value';
title2 j=L 'districts (yaxis) also implicitly sorted by formatted value';
run;
proc sgplot data=have;
hbar district
/ group = populationID
response = percent
categoryOrder = respAsc
;
title j=L 'categoryOrder: ascending response';
title2 j=L 'districts (yaxis) also implicitly sorted by min(response)';
run;
proc sgplot data=have;
hbar district
/ group = populationID
response = percent
categoryOrder = respDesc
;
title j=L 'categoryOrder: descending response';
title2 j=L 'districts (yaxis) also implicitly sorted by descending max(response)';
run;
proc sql;
create table have2 as
select
case
when populationID = 3 then 0 else populationID
end as hbar_populationID format=popId.
, *
from have
order by
hbar_populationID, percent
;
quit;
proc sgplot data=have2;
yaxis type=discrete discreteOrder=data;
hbar district
/ group = hbar_populationID
response = percent
;
title j=L 'population seqment ordering is partially forced by tweaking populationID values';
title2 j=L 'districts in data order per yaxis statement';
run;
根据响应值,强制将一个段放在第一位,然后将其他段放在第一位
将populationID 2映射到0后,您可以强制剩余的填充段的顺序类似于respAsc
或respDesc
。这个过程需要额外的编码来确定其他populationID值的新映射。此附加示例显示了如何使用全局响应和强制对一个地区内的剩余人口段执行降序
proc sql;
create table way as
select populationID, sum(percent) as allPct
from have
where populationID ne 3
group by populationID
order by allPct descending
;
data waySeq;
set way;
seq + 1;
run;
proc sql;
create table have3 as
select
have.*
, case
when have.populationID = 3 then 1000 else 1000+seq
end as hbar_populationID
from have
left join waySeq on have.populationID = waySeq.populationID
order by
hbar_populationID, percent
;
create table fmtdata as
select distinct
hbar_populationID as start
, put(populationID, popId.) as label
, 'mappedPopId' as fmtname
from have3;
quit;
proc format cntlin = fmtdata;
run;
%let syslast = have3;
proc sgplot data=have3;
yaxis type=discrete discreteOrder=data;
hbar district
/ group = hbar_populationID
response = percent
groupOrder = data
;
format hbar_populationID mappedPopId.;
title j=L 'population seqment ordering is partially forced by tweaking populationID values';
title2 j=L 'districts in data order per yaxis statement';
run;
title;
请尝试CategoryOrder或GROUPORDER?您的SAS版本是什么,选项因您的版本而异。在第一个填充位置放置“颜色”后,其他填充段应如何排序?按特定百分比排序可以使总体项目(不同位置的彩色条)混乱,按总体项目排序可以使百分比(锯齿状的相同颜色条)混乱@Reeza I有SAS 9。4@Richard我要颜色、白色、黑色和黑色asian@Nathan123您必须提供准确的版本,即9.4 TS1M5或M4。M3和M5之后有显著的改进。
proc sql;
create table way as
select populationID, sum(percent) as allPct
from have
where populationID ne 3
group by populationID
order by allPct descending
;
data waySeq;
set way;
seq + 1;
run;
proc sql;
create table have3 as
select
have.*
, case
when have.populationID = 3 then 1000 else 1000+seq
end as hbar_populationID
from have
left join waySeq on have.populationID = waySeq.populationID
order by
hbar_populationID, percent
;
create table fmtdata as
select distinct
hbar_populationID as start
, put(populationID, popId.) as label
, 'mappedPopId' as fmtname
from have3;
quit;
proc format cntlin = fmtdata;
run;
%let syslast = have3;
proc sgplot data=have3;
yaxis type=discrete discreteOrder=data;
hbar district
/ group = hbar_populationID
response = percent
groupOrder = data
;
format hbar_populationID mappedPopId.;
title j=L 'population seqment ordering is partially forced by tweaking populationID values';
title2 j=L 'districts in data order per yaxis statement';
run;
title;