Data visualization with SAS programming

how can I have the sgplot vbars change color based on target (refline)

Accepted Solution Solved
Reply
Contributor
Posts: 74
Accepted Solution

how can I have the sgplot vbars change color based on target (refline)

Here's my code in SAS 9.04 TS1M3. I'd like to have the vbar colors change based on whether they are on or over the target line (green), coming up to the target line (lime-yellow-orange), no where near the target line (red). Currently the vbar colors are based on how close they are to the top of the graph instead of the target line. Any help is appreciated.

 

data date_visits;
input date $CHAR10. visits 8.;
datalines;

11/01/2016 136
12/01/2016 150
01/01/2017 161
02/01/2017 150
03/01/2017 160
04/01/2017 152
05/01/2017 144
06/01/2017 158
07/01/2017 165
08/01/2017 173
09/01/2017 170
10/01/2017 195
11/01/2017 201
;


proc sgplot data=date_visits NOAUTOLEGEND;
   vbar date / response=visits
   nostatlabel colorresponse=visits
   colormodel=(red yellow green);
   refline 150 / axis=y lineattrs=(pattern=2 thickness=2px) label='Target 150';
   xaxis discreteorder=data label="Month";
   yaxis label="visits" minor max=250;


run;Capture.GIF


Accepted Solutions
Solution
2 weeks ago
SAS Super FREQ
Posts: 379

Re: how can I have the sgplot vbars change color based on target (refline)

Posted in reply to WarrenKuhfeld

Something like this.  You will no doubt want to tweak my colors and value ranges associated with the colors to be more to your liking.  There is some subjectivity in this.

data date_visits;
input date $CHAR10. visits 8.;
datalines;
11/01/2016 136
12/01/2016 150
01/01/2017 161
02/01/2017 150
03/01/2017 160
04/01/2017 152
05/01/2017 144
06/01/2017 158
07/01/2017 165
08/01/2017 173
09/01/2017 170
10/01/2017 195
11/01/2017 201
;

data rangeattrmap;
   id = "myID";
   length colormodel1 colormodel2 $ 8 min 8;
   input max colormodel2;
   min = ifn(_n_ = 1, 0, lag(max));
   colormodel1 = ifc(_n_ = 1, 'red', lag(colormodel2));
   datalines;
130 red
140 orange
150 yellow
201 green
;

proc print; run;

proc sgplot data=date_visits NOAUTOLEGEND rattrmap=rangeattrmap;
   vbar date / response=visits nostatlabel colorresponse=visits rattrid=myID;
   refline 150 / axis=y lineattrs=(pattern=2 thickness=2px) label='Target 150';
   xaxis discreteorder=data label="Month";
   yaxis label="visits" minor max=250;
run;

View solution in original post


All Replies
SAS Super FREQ
Posts: 379

Re: how can I have the sgplot vbars change color based on target (refline)

Posted in reply to robby_beum
Solution
2 weeks ago
SAS Super FREQ
Posts: 379

Re: how can I have the sgplot vbars change color based on target (refline)

Posted in reply to WarrenKuhfeld

Something like this.  You will no doubt want to tweak my colors and value ranges associated with the colors to be more to your liking.  There is some subjectivity in this.

data date_visits;
input date $CHAR10. visits 8.;
datalines;
11/01/2016 136
12/01/2016 150
01/01/2017 161
02/01/2017 150
03/01/2017 160
04/01/2017 152
05/01/2017 144
06/01/2017 158
07/01/2017 165
08/01/2017 173
09/01/2017 170
10/01/2017 195
11/01/2017 201
;

data rangeattrmap;
   id = "myID";
   length colormodel1 colormodel2 $ 8 min 8;
   input max colormodel2;
   min = ifn(_n_ = 1, 0, lag(max));
   colormodel1 = ifc(_n_ = 1, 'red', lag(colormodel2));
   datalines;
130 red
140 orange
150 yellow
201 green
;

proc print; run;

proc sgplot data=date_visits NOAUTOLEGEND rattrmap=rangeattrmap;
   vbar date / response=visits nostatlabel colorresponse=visits rattrid=myID;
   refline 150 / axis=y lineattrs=(pattern=2 thickness=2px) label='Target 150';
   xaxis discreteorder=data label="Month";
   yaxis label="visits" minor max=250;
run;
SAS Super FREQ
Posts: 1,184

Re: how can I have the sgplot vbars change color based on target (refline)

Posted in reply to WarrenKuhfeld

I would also suggest creating a new variable Delta=Visits-150.  Then, you can set ColorResponse=Delta instead of Visits.  To get consistent colors based on delta amount (or %), use a RangeAttrMap as suggested by Warren.

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 3 replies
  • 105 views
  • 2 likes
  • 3 in conversation