読者です 読者をやめる 読者になる 読者になる

カラーパレットマクロ

SAS

便利かなと思って作成してみました。

data palet;
input color depth code $ @@;
cards;
1 1 cxA60000 1 2 cxBF3030 1 3 cxFF0000 1 4 cxFF4040 1 5 cxFF7373
2 1 cx85004B 2 2 cx992667 2 3 cxCD0074 2 4 cxE6399B 2 5 cxE667AF
3 1 cx48036F 3 2 cx602580 3 3 cx7109AA 3 4 cx9F3ED5 3 5 cxAD66D5
4 1 cx200772 4 2 cx3914AF 4 3 cx412C84 4 4 cx6A48D7 4 5 cx876ED7
5 1 cx06266F 5 2 cx1240AB 5 3 cx2A4480 5 4 cx4671D5 5 5 cx6C8CD5
6 1 cx006363 6 2 cx009999 6 3 cx1D7373 6 4 cx33CCCC 6 5 cx5CCCCC
7 1 cx008500 7 2 cx00CC00 7 3 cx269926 7 4 cx39E639 7 5 cx67E667
8 1 cx679B00 8 2 cx9FEE00 8 3 cx86B22D 8 4 cxB9F73E 8 5 cxC9F76F
9 1 cxA6A600 9 2 cxBFBF30 9 3 cxFFFF00 9 4 cxFFFF40 9 5 cxFFFF73
10 1 cxA68900 10 2 cxBFA730 10 3 cxFFD300 10 4 cxFFDE40 10 5 cxFFE773
11 1 cxA68900 11 2 cxBFA730 11 3 cxFFD300 11 4 cxFFDE40 11 5 cxFFE773
12 1 cxA66E00 12 2 cxBF8F30 12 3 cxFFAA00 12 4 cxFFBF40 12 5 cxFFD073
13 1 cxA64B00 13 2 cxBF7130 13 3 cxFF7400 13 4 cxFF9640 13 5 cxFFB273
;
run;

%macro palet(color=, depth=);
data _null_;
  format list $1000.; retain list ""; set palet;
  %if "&color." ^= "" & "&depth." ^= "" %then where &color. & &depth.;
  %else %if "&color." ^= "" %then where &color.;
  %else %if "&depth." ^= "" %then where &depth.;;
  list = cat('"', code, '" ', list);
  call symput("clist", list);
run;
goptions colors = (&clist.);
%mend palet;

/* red, red-violet, purple, depth = 1, 2, 3 */
%palet(color=color in (1,2,3), depth=depth in (1,2,3));

/* all color, all depth */
%palet();

マクロ変数 color が色の種類、depth が色の濃さ(depth が大きいほど明るい)に対応しています。
マクロ変数 color, depth が空白の場合、全ての色と濃さを選択します。

 color 一覧
 1 = red, 赤
 2 = red-violet, 赤紫
 3 = purple, 紫
 4 = indigo, 藍
 5 = blue, 青
 6 = cyan, シアン
 7 = green, 緑
 8 = yellow-green, 黄緑
 9 = yellow, 黄
10 = dark yellow, 暗い黄
11 = flesh color, 肌
12 = orange-yellow, 明るい橙
13 = orange, 橙

 depth
 1    <--> 5
 dark <--> light


サンプル画像を出力してみました。

  • %palet(color=color in (5));

  • %palet(depth=depth in (5)); キモ色

  • %palet(color=color in (1,2,3), depth=depth in (3));

  • %palet();


5個ずつで color を変えている、全65色。


おまけ

data graph;
  retain grade 0;
  call streaminit(985125467);
  do id = 1 to 50;
    do time = 1 to 24;
      time = time + (rand('UNIFORM') - 0.5 )* 0.1;
      if time <= 12 then r = rand('Table', 0.02, 0.03, 0.95);
      if time > 12 then r = rand('Table', 0.06, 0.09, 0.85);
      if r = 1 then grade = grade - 1 - (rand('UNIFORM')) * 0.01;
      if r = 2 then grade = grade + 1 - (rand('UNIFORM')) * 0.01;
      if grade < 0 then grade = 0;
      if grade > 3.5 then grade = 4;
      output;
    end;
    grade = 0;
  end;
  keep id time grade;
run;

goptions reset = all;
goptions ftext = "Times New Roman/bo" ftitle = "Times New Roman/bo";
goptions vsize = 12 in hsize = 19 in htitle = 4 htext = 4;
%palet(color=color in (5));

proc gplot data = graph;
  plot grade * time = id / autovref cautovref = "cxE9DECA"
        skipmiss noframe nolegend haxis = axis1 vaxis = axis2;
  axis1 label = ('hour') major=(w=2 height=0.7) w=2 minor = none
    order = (1 to 24 by 4) offset = (7, 7) c=black;
  axis2 label = (a=90 'grade') major=(w=2 height=1) w=2 minor = none
    offset = (2, 2) order = (0 to 4 by 1) c=black;
  symbol1 i = joins l = 2 w = 5 r = 65;
run; quit;

システムで使用するカラーパレットをいじくるので、axis等で軸の色等を指定しないと変な色になります。