| 
 | 
楼主
 
 
 楼主 |
发表于 2013-12-23 10:16:40
|
只看该作者
 
 
 
用 IML 和 Heatmap 画圣诞树
[code:2oen02v3]proc iml; 
/* define helper functions ROW and COL */ 
start row(x);       /* return matrix m such that m[i,j] = i */ 
   return( repeat( T(1:nrow(x)), 1, ncol(x) )); 
finish; 
start col(x);       /* return matrix m such that m[i,j] = j */ 
   return( repeat(1:ncol(x), nrow(x)) ); 
finish; 
  
/* parameters for the tree dimensions */ 
h = 100; w = h+1; b = int(h/10); 
M = j(w, h, .);         /* initialize h x w matrix to missing */ 
x = col(M);             /* column numbers */ 
y = w + 1 - row(M);     /* reverse Y axis */ 
  
/* define the leafy portion of the tree */ 
TreeIdx = loc(y>b & y<=2*x & y<=-2*x+2*h); /* a triangle */ 
M[TreeIdx] = 0; 
  
/* place lights randomly within tree */ 
N = int(0.12*ncol(TreeIdx)); /* use 12% of tree area */ 
call randseed(1225); 
do i = 1 to 3;               /* 3 colors */ 
   idx = sample(TreeIdx, N, "WOR"); /* sample without replacement */ 
   M[idx] = i;                
end; 
  
/* define the trunk */ 
width = int(b/2); 
TrunkIdx = loc( y<= b & abs(x-nrow(M)/2)<width ); 
M[TrunkIdx] = 4; 
  
/* write matrix in "long form" to SAS data set */ 
Row = row(M);                    /* row index */ 
Col = col(M);                    /* col index */ 
create Tree var {"Row" "Col" "M"};  append;  close Tree; 
quit; 
 
proc template; 
define statgraph HeatmapTree; 
dynamic _X _Y _Z; 
begingraph; 
   discreteattrmap name="christmastree"; 
      value '.' / fillattrs=(color=WHITE);    /* background color */ 
      value '0' / fillattrs=(color=GREEN);    /* tree color       */ 
      value '1' / fillattrs=(color=RED);      /* ornament color 1 */ 
      value '2' / fillattrs=(color=BLUE);     /* ornament color 2 */ 
      value '3' / fillattrs=(color=YELLOW);   /* ornament color 3 */ 
      value '4' / fillattrs=(color=BROWN);    /* tree trunk color */ 
   enddiscreteattrmap; 
   discreteattrvar attrvar=Alias var=_Z attrmap="christmastree"; 
   layout overlay / xaxisopts=(type=discrete display=none) 
                    yaxisopts=(type=discrete display=none reverse=true); 
      heatmapparm x=_X y=_Y colorgroup=Alias /  
                    xbinaxis=false ybinaxis=false primary=true; 
   endlayout; 
endgraph; 
end; 
run; 
  
ods graphics / width=500 height=800; 
proc sgrender data=Tree template=HeatmapTree; 
   dynamic _X="Col" _Y="Row" _Z="M"; 
run;[/code:2oen02v3] |   
 
 
 
 |