【问题标题】:Assign colors to vertical bars using proc gchart in SAS使用 SAS 中的 proc gchart 将颜色分配给垂直条
【发布时间】:2014-02-11 00:40:57
【问题描述】:

在 PROC GCHART 中,创建条形图时,垂直条的颜色由 PATTERN 语句确定。 PATTERN 语句根据值的字母顺序或层次顺序为条形分配颜色。如何根据值专门分配颜色?例如,如果我的值是“男孩”和“女孩”,我怎样才能让值“男孩”显示为蓝色,而不管值“女孩”是否存在,反之亦然?

【问题讨论】:

    标签: colors sas gchart


    【解决方案1】:

    不幸的是,如果您使用的是 SAS/GRAPH,则不能使用属性映射,这是处理此问题的方法。如果您有 SAS 9.2+,则可以使用具有该选项的 ODS 图形 (PROC SGPLOT)。 attrmap 将 proc 与一个数据集连接起来,该数据集存储变量的值以及与该变量的每个值相关联的颜色(或其他属性)。

    来自SAS doc page on SGPLOT的一个例子:

    *create dummy dataset;
    data finances;
    format income dollar8. expense dollar8.;
    length expensetype $ 9;
    input Year incometype $ income expensetype $ expense;
    datalines;
    2000 Salary 20000 Utilities 4000
    2000 Bonus   2000 Rent      7000
    2000 Gifts    500 Food      8000 
    2001 Salary 25000 Utilities 5000
    2001 Bonus   1000 Rent      8000
    2001 Gifts    200 Food      6000 
    2002 Salary 23000 Utilities 4500
    2002 Bonus    500 Rent      9000
    2002 Gifts    500 Food      7000
    ;
    run;
    
    *Create attribute map.  ID = used to specify its use later, usually matches the variable but does not have to.  Value is the value you are changing the color/etc. for.  Then variables appropriate to what you are changing and to what, so fillcolor here for example.  ;
    data attrmap;
    length value $ 9 fillcolor $ 9; *I find that problems with attrmaps usually are due to length differences in dataset variables vs attrmap variables;
    retain linecolor "black";  *using black for all of them;
    input id $ value $ fillcolor $;
    datalines;
    income  Salary    blue
    income  Bonus     gray
    income  Gifts     lightgray
    expense Utilities red
    expense Rent      yellow
    expense Food      orange
    ;
    run;
    
    
    proc sgplot data=finances dattrmap=attrmap; *specifying the attrmap here;
    yaxis label="Dollars";
    vbarparm category=year response=income / group=incometype attrid=income  /*using the attrmap here */
             barwidth=0.4 discreteoffset=-0.2 name="income"; 
    vbarparm category=year response=expense / group=expensetype attrid=expense
             barwidth=0.4 discreteoffset=0.2 name="expense"; 
    keylegend "income" / position=bottomleft title="Income";
    keylegend "expense" / position=bottomright title="Expenses";
    run;
    

    【讨论】:

    • @Joe,这会很好用,但我的 SAS 比较老,没有这个。但是看看这个,这会很好用。感谢您的意见。
    猜你喜欢
    • 2016-08-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-03
    • 1970-01-01
    • 2014-11-07
    相关资源
    最近更新 更多