如何在proc sgplot中按降序创建单个条形图?

时间:2018-04-21 19:27:45

标签: sas sgplot

我使用sgplot

创建了以下图表
proc sgplot data=Colordistricts;
hbar distrct/response=Percent 
group= population;
run;  

enter image description here

然而,似乎个体人口群体在图表中按字母顺序排列(亚洲人接着是黑色和白色)。

如何按百分比的降序为人口组创建相同的图?

事实上,这些是色彩群体最高的地区。基本上我想创建一个图形,以便每个条形都以颜色填充

开头

1 个答案:

答案 0 :(得分:0)

要将特定组值强制到第一个位置,您可以将所需的组映射到将首先进行整理的新值。有时,通过在现有值前放置空格字符可以轻松完成此操作。

如果组变量是自定义格式以显示关联组标签的数字ID,则可以创建新版本的自定义格式以包含与强制组对应的0标识。强制组映射到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;

强制groupOrder

SQL可以使用case子句中的order by按特定顺序对数据进行排序。然后,您将在SGPLOT中使用groupOrder=data

proc sql;
  create table have3 as
  select *
  from have
  order by 
    district
  , case 
      when populationID = 3 then 0
      when populationID = 2 then 1
      when populationID = 4 then 2
      when populationID = 1 then 3
      else 99
    end
  ;
quit;

proc sgplot data=have3;
  hbar district
  / group = populationID
    groupOrder = data
    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后,您可以强制将其余的细分受众群排序为respAscrespDesc。该过程需要额外的编码来确定其他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;