Data visualization with SAS programming

Color difference between two variables PROC SGPLOT

Reply
Learner
Posts: 1

Color difference between two variables PROC SGPLOT

Dear community,

I'm trying to color the gap between two series charts depending on which of the two y-values is greater.

I colored an example plot in MS Paint, to visualize the idea.

So far I've tried various options in the band statement, but failed. My hope was that the band plot would vanish when the lower values is greater than the upper value. Then I could use two band plots, each with a different filling color. But also that does not work.

Do you have any idea how I could get such a chart in SAS?

Thanks a lot,

Paul

Ex2.png

data temp;
input x y1 y2;
datalines;
1 50 70
2 60 65
3 70 50
4 60 100
;;
run;

proc sgplot data=temp;
series x=x y=y1;
series x=x y=y2;
run;

Super User
Super User
Posts: 7,668

Re: Color difference between two variables PROC SGPLOT

Hi,

 

I would probably go down the route of a polygon plot:

http://blogs.sas.com/content/graphicallyspeaking/?s=polygon

 

You can draw each polygon with its own color.

SAS Super FREQ
Posts: 925

Re: Color difference between two variables PROC SGPLOT

I've actually done something like this before. In my paper, "Secrets of the SG Procedures", I did this plot of a sine wave:

 

%let THRESHOLD = 0;
data wave;
do x=-6 to 6 by .05;
y=sin(x);
if (y >= &THRESHOLD) then do;
positive = y;
negative = &THRESHOLD;
end;
else do;
positive = &THRESHOLD;
negative = y;
end;
output;
end;
run;


proc sgplot data=wave noautolegend;
   yaxis grid label="y";
   xaxis grid;
   band x=x lower=&THRESHOLD
            upper=positive / transparency=0.5;
   band x=x lower=negative
            upper=&THRESHOLD /
            transparency=0.5;
run;

 

The key to this example is the data manipulation. In a BAND plot, if the upper and lower values are the same, then NOTHING is drawn in that segment. What's trickier about your example is that the y1/y2 invert in places where there is no data. You would need to do some extra work to put data values at those locations.

 

Hope this helps!

Dan

SAS Super FREQ
Posts: 1,134

Re: Color difference between two variables PROC SGPLOT

[ Edited ]

RW9's proposal to use POLYGON will work, but you will need to compute the intersection of the two lines, which may not occur at exact axis value.  A partial result is obtained by using one band to wrap back over itself as shown below.  You will get the correct filled area.  But, now we need to figure out how to color each area differently. :-)

 

data temp;
input x y;
datalines;
1 50
2 60
3 70
4 60
4 100
3 50
2 65
1 70
;
run;

proc sgplot data=temp;
band x=x upper=y lower=0;
run;

 

Band.png

Ask a Question
Discussion stats
  • 3 replies
  • 340 views
  • 0 likes
  • 4 in conversation