0

我使用创建了以下图表sgplot

proc sgplot data=Colordistricts;
hbar distrct/response=Percent 
group= population;
run;  

在此处输入图像描述

但是,似乎各个人口群体在图表中按字母顺序排列(亚洲人其次是黑色和白色)。

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

事实上,这些地区是有色人种最多的地区。基本上我想创建一个图表,以便每个条都以颜色人口开始

4

1 回答 1

0

要将特定组值强制到第一个位置,您可以将所需组映射到将首先整理的新值。有时这很容易通过在现有值前面放置一个空格字符来完成。

如果组变量是自定义格式的数字 ID 以显示关联的组标签,您可以创建自定义格式的新版本以包含对应于强制组的 0 id。强制组映射到 0 id。

然后,您将以您需要的特定方式对数据进行排序,并使用 SGPLOTyaxis 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

caseSQL 可以通过在order by子句中使用 a 以特定顺序对数据进行排序。然后您将groupOrder=data在 SGPLOT 中使用。

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 后,您可以强制剩余的人口段按类似于respAscor的顺序排列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;
于 2018-04-22T16:12:13.577 回答