Data visualization with SAS programming

Removing one of the legends from a heatmap

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 10
Accepted Solution

Removing one of the legends from a heatmap

[ Edited ]

I'm not an avid SAS user and have been stuck at this step: I have a working heatmap

ods output PearsonCorr=Corr_P;
PROC CORR DATA=sashelp.cars;
   VAR MSRP Horsepower MPG_City MPG_Highway Weight;
RUN;

proc sort data=Corr_P;
  by VARIABLE;
run;

proc transpose data=Corr_P out=CorrLong1(rename=(COL1=Correlation)) name=CorrelationID;
var MSRP Horsepower MPG_City MPG_Highway Weight;
by Variable;
run;

proc transpose data=Corr_P out=CorrLong2(rename=(COL1=p_value)) name=PvalueID;
var PMSRP PHorsepower PMPG_City PMPG_Highway PWeight;
by Variable;
run;
data CorrLong;
merge CorrLong1 CorrLong2(drop=PvalueID _LABEL_));
   by Variable;
   drop _name_;
   LABEL _NAME_=' '
run;

proc sort data=CorrLong;
  by VARIABLE CorrelationID;
run;

proc sgplot data=CorrLong;
heatmap x=Variable y=CorrelationID / colorresponse=Correlation discretex discretey x2axis;
yaxis reverse display=(nolabel);
x2axis display=(nolabel);
run;

 

Everything works fine above, but as soon as I add a text overlay, I get an x axis label that I'm unable to get rid of:

proc sgplot data=CorrLong;
heatmap x=Variable y=CorrelationID / colorresponse=Correlation discretex discretey x2axis colormodel=ThreeColorRamp; *Colorresponse allows discrete squares for each correlation. x2axis bring the label to the top;
text x=Variable y=CorrelationID text=p_value  / textattrs=(size=10pt) x2axis; /*To overlay significance, create a variable that contans that info and set text=VARIABLE */
label correlation='Pearson Correlation';
yaxis reverse display=(nolabel);
x2axis display=(nolabel);
run;

I've already tried noautolegend + gradlegend, keylegend, colormodel with various combinations and cannot resolve this stubborn issue. I know it's a keylegend item because I can move it around with that option, but keylegen exclude option did not get rid of it. My goal is to keep the gradient legend on the right side. I'm sure someone can tell me what's the obvious code that I'm missing.

 

Thank you.

 

 


Accepted Solutions
Solution
a month ago
Super User
Posts: 22,818

Re: Removing one of the legends from a heatmap

[ Edited ]
Posted in reply to Excelsius

Note: this was edited to make sure the gradient legend is included. 

NOAUTOLEGEND + GRADLEGEND works for me. What version of SAS are you using? You can check your status with the following:

 

proc product_status;run;

This is the working code, your sample didn't run so I'm including the full code after I modified it. 

 

ods output PearsonCorr=Corr_P;

PROC CORR DATA=sashelp.cars;
    VAR MSRP Horsepower MPG_City MPG_Highway Weight;
RUN;

proc sort data=Corr_P;
    by VARIABLE;
run;

proc transpose data=Corr_P out=CorrLong1(rename=(COL1=Correlation)) 
        name=CorrelationID;
    var MSRP Horsepower MPG_City MPG_Highway Weight;
    by Variable;
run;

proc transpose data=Corr_P out=CorrLong2(rename=(COL1=p_value)) name=PvalueID;
    var PMSRP PHorsepower PMPG_City PMPG_Highway PWeight;
    by Variable;
run;

data CorrLong;
    merge CorrLong1 CorrLong2(drop=PvalueID );
    by Variable;
    LABEL CorrelationID="Correlations"; run;

proc sort data=CorrLong;
    by VARIABLE CorrelationID;
run;



proc sgplot data=CorrLong noautolegend;
heatmap x=Variable y=CorrelationID / colorresponse=Correlation name="nope1" discretex discretey x2axis colormodel=ThreeColorRamp; *Colorresponse allows discrete squares for each correlation. x2axis bring the label to the top;
text x=Variable y=CorrelationID text=p_value  / textattrs=(size=10pt) x2axis name='nope2'; /*To overlay significance, create a variable that contans that info and set text=VARIABLE */
label correlation='Pearson Correlation';
yaxis reverse display=(nolabel);
x2axis display=(nolabel);
gradlegend;

run;
Spoiler

@Excelsius wrote:

I'm not an avid SAS user and have been stuck at this step: I have a working heatmap

ods output PearsonCorr=Corr_P;
PROC CORR DATA=sashelp.cars;
   VAR MSRP Horsepower MPG_City MPG_Highway Weight;
RUN;

proc sort data=Corr_P;
  by VARIABLE;
run;

proc transpose data=Corr_P out=CorrLong1(rename=(COL1=Correlation)) name=CorrelationID;
var MSRP Horsepower MPG_City MPG_Highway Weight;
by Variable;
run;

proc transpose data=Corr_P out=CorrLong2(rename=(COL1=p_value)) name=PvalueID;
var PMSRP PHorsepower PMPG_City PMPG_Highway PWeight;
by Variable;
run;
data CorrLong;
merge CorrLong1 CorrLong2(drop=PvalueID _LABEL_));
   by Variable;
   drop _name_;
   LABEL _NAME_=' '
run;

proc sort data=CorrLong;
  by VARIABLE CorrelationID;
run;

proc sgplot data=CorrLong;
heatmap x=Variable y=CorrelationID / colorresponse=Correlation discretex discretey x2axis;
yaxis reverse display=(nolabel);
x2axis display=(nolabel);
run;

 

Everything works fine above, but as soon as I add a text overlay, I get an x axis label that I'm unable to get rid of:

proc sgplot data=CorrLong;
heatmap x=Variable y=CorrelationID / colorresponse=Correlation discretex discretey x2axis colormodel=ThreeColorRamp; *Colorresponse allows discrete squares for each correlation. x2axis bring the label to the top;
text x=Variable y=CorrelationID text=p_value  / textattrs=(size=10pt) x2axis; /*To overlay significance, create a variable that contans that info and set text=VARIABLE */
label correlation='Pearson Correlation';
yaxis reverse display=(nolabel);
x2axis display=(nolabel);
run;

I've already tried noautolegend + gradlegend, keylegend, colormodel with various combinations and cannot resolve this stubborn issue. I know it's a keylegend item because I can move it around with that option, but keylegen exclude option did not get rid of it. My goal is to keep the gradient legend on the right side. I'm sure someone can tell me what's the obvious code that I'm missing.

 

Thank you.

 

 


 

View solution in original post


All Replies
Solution
a month ago
Super User
Posts: 22,818

Re: Removing one of the legends from a heatmap

[ Edited ]
Posted in reply to Excelsius

Note: this was edited to make sure the gradient legend is included. 

NOAUTOLEGEND + GRADLEGEND works for me. What version of SAS are you using? You can check your status with the following:

 

proc product_status;run;

This is the working code, your sample didn't run so I'm including the full code after I modified it. 

 

ods output PearsonCorr=Corr_P;

PROC CORR DATA=sashelp.cars;
    VAR MSRP Horsepower MPG_City MPG_Highway Weight;
RUN;

proc sort data=Corr_P;
    by VARIABLE;
run;

proc transpose data=Corr_P out=CorrLong1(rename=(COL1=Correlation)) 
        name=CorrelationID;
    var MSRP Horsepower MPG_City MPG_Highway Weight;
    by Variable;
run;

proc transpose data=Corr_P out=CorrLong2(rename=(COL1=p_value)) name=PvalueID;
    var PMSRP PHorsepower PMPG_City PMPG_Highway PWeight;
    by Variable;
run;

data CorrLong;
    merge CorrLong1 CorrLong2(drop=PvalueID );
    by Variable;
    LABEL CorrelationID="Correlations"; run;

proc sort data=CorrLong;
    by VARIABLE CorrelationID;
run;



proc sgplot data=CorrLong noautolegend;
heatmap x=Variable y=CorrelationID / colorresponse=Correlation name="nope1" discretex discretey x2axis colormodel=ThreeColorRamp; *Colorresponse allows discrete squares for each correlation. x2axis bring the label to the top;
text x=Variable y=CorrelationID text=p_value  / textattrs=(size=10pt) x2axis name='nope2'; /*To overlay significance, create a variable that contans that info and set text=VARIABLE */
label correlation='Pearson Correlation';
yaxis reverse display=(nolabel);
x2axis display=(nolabel);
gradlegend;

run;
Spoiler

@Excelsius wrote:

I'm not an avid SAS user and have been stuck at this step: I have a working heatmap

ods output PearsonCorr=Corr_P;
PROC CORR DATA=sashelp.cars;
   VAR MSRP Horsepower MPG_City MPG_Highway Weight;
RUN;

proc sort data=Corr_P;
  by VARIABLE;
run;

proc transpose data=Corr_P out=CorrLong1(rename=(COL1=Correlation)) name=CorrelationID;
var MSRP Horsepower MPG_City MPG_Highway Weight;
by Variable;
run;

proc transpose data=Corr_P out=CorrLong2(rename=(COL1=p_value)) name=PvalueID;
var PMSRP PHorsepower PMPG_City PMPG_Highway PWeight;
by Variable;
run;
data CorrLong;
merge CorrLong1 CorrLong2(drop=PvalueID _LABEL_));
   by Variable;
   drop _name_;
   LABEL _NAME_=' '
run;

proc sort data=CorrLong;
  by VARIABLE CorrelationID;
run;

proc sgplot data=CorrLong;
heatmap x=Variable y=CorrelationID / colorresponse=Correlation discretex discretey x2axis;
yaxis reverse display=(nolabel);
x2axis display=(nolabel);
run;

 

Everything works fine above, but as soon as I add a text overlay, I get an x axis label that I'm unable to get rid of:

proc sgplot data=CorrLong;
heatmap x=Variable y=CorrelationID / colorresponse=Correlation discretex discretey x2axis colormodel=ThreeColorRamp; *Colorresponse allows discrete squares for each correlation. x2axis bring the label to the top;
text x=Variable y=CorrelationID text=p_value  / textattrs=(size=10pt) x2axis; /*To overlay significance, create a variable that contans that info and set text=VARIABLE */
label correlation='Pearson Correlation';
yaxis reverse display=(nolabel);
x2axis display=(nolabel);
run;

I've already tried noautolegend + gradlegend, keylegend, colormodel with various combinations and cannot resolve this stubborn issue. I know it's a keylegend item because I can move it around with that option, but keylegen exclude option did not get rid of it. My goal is to keep the gradient legend on the right side. I'm sure someone can tell me what's the obvious code that I'm missing.

 

Thank you.

 

 


 

Occasional Contributor
Posts: 10

Re: Removing one of the legends from a heatmap

Reeza, thank you very much. That works (9.4 M5 14.3). The reason it didn't work for me was because I was using gradlegend 'heatmap'; It didn't occur to me to just use gradlegend alone. This is why SAS syntax can be so frustrating sometimes. I was able to make this same graph in R within 20 min despite that I know R less than SAS!

 

Honestly, I'm still not sure what's the difference between gradlegend, keylegend, legenditem, etc. Some of this stuff is easiest to learn with examples, but I didn't come across that many. If you have any suggestions for good sources, other than the SAS manual online, let me know. I've already gone through the Little SAS book.

 

I'm also curious if there is a way to make half of the correlation matrix into a different shape, such as circles based on the correlation value (without messing with SAS/IML).

 

Thanks again.

Super User
Posts: 22,818

Re: Removing one of the legends from a heatmap

Posted in reply to Excelsius
Regarding the circles, that's a different viz type, at the point, I'd almost consider a scatter plot controlling the symbols to get the shapes you want and putting the values in as text or marker char values. You'd have to determine the size/spacing which would be a bit of a guess and test procedure. For any topic you're having issues with in SAS search lexjansen.com, especially if it's topic specific. Otherwise, I pretty much rely on the docs but I also use R and Tableau for graphics Smiley Happy
Community Manager
Posts: 3,344

Re: Removing one of the legends from a heatmap

Posted in reply to Excelsius

@Sanjay_SAS and @Rick_SAS often write blog examples that demonstrate SGPLOT features like this.  Here's a gradlegend example from Rick.  And another from Sanjay is here.

Occasional Contributor
Posts: 10

Re: Removing one of the legends from a heatmap

[ Edited ]
Posted in reply to ChrisHemedinger

Thanks. I'll read these blogs over time. I have already read some of Rick's blogs.

Question: the gradlegend in the example above has a scale that only includes -0.5 and +0.5. Is it possible to customize this scale to display numbers with 0.2 increments (-0.4, -0.2, 0, 0.2, etc)? The extractscale option doesn't seem to do anything.

SAS Super FREQ
Posts: 1,042

Re: Removing one of the legends from a heatmap

Posted in reply to Excelsius

For legends (keylegend, gradlegend, etc), you should refer to the plot's NAME value -- not the statement name. In Reeza's example, the heatmap has the name of "nope1". So, in this case, you would say:

 

gradlegend "nope1";

 

Hope this helps!

Dan

☑ This topic is solved.

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

Discussion stats
  • 6 replies
  • 309 views
  • 3 likes
  • 4 in conversation