BookmarkSubscribeRSS Feed
alexgonzalez
Quartz | Level 8

Hello,

I want to use the color of the data (i.e mean and percent) in the legend of a plot. In this case, I want the legend in 'blue' for the percent and 'brown' for the mean. Would that be possible?

Please see my code below:

 

proc sgplot data=top_sources_plots sganno=anno;
hbarparm
category=food
response=percent / datalabel
datalabelattrs=(color=blue)
fillattrs =(color=bwh)
outlineattrs =(color=bwh); /*variable with higher values should appear first*/
yaxis display=(nolabel) ;
hbarparm
category=food
response=mean / datalabel
datalabelattrs=(color=brown)
fillattrs =(color=bwh)
outlineattrs =(color=bwh);
xaxis display=(nolabel) ;
yaxis display=(nolabel);
keylegend /location=inside position=bottomright across=1;
run;

Thanks,

AG

 
9 REPLIES 9
DanH_sas
SAS Super FREQ

Did you intentionally set the fill color for both bar charts to BWH?

alexgonzalez
Quartz | Level 8

I did, otherwise I'd get colors in the bars that do not accurately represent the data. See in the attachment that in the first plot each bar contains the mean and percent. If you look at the top one, for example, 11.8 is the mean (filled in red) and the percent is 21.7. Only some part of the bar is in blue, which is not accurate. That's why I think that assigning colors to the data using those colors in the legend in more accurate (see second plot).

Thanks,

AG

 
DanH_sas
SAS Super FREQ

Why not use DISCRETEOFFSET instead of overlaying the bars? Give this a try:

 

proc sgplot data=top_sources_plots sganno=anno;
hbarparm
category=food
response=percent / datalabel
datalabelattrs=(color=blue)
fillattrs =(color=blue)
outlineattrs =(color=blue); /*variable with higher values should appear first*/
yaxis display=(nolabel) 
discreteoffset=-0.2
barwidth=0.4;
hbarparm
category=food
response=mean / datalabel
datalabelattrs=(color=brown)
fillattrs =(color=brown)
outlineattrs =(color=brown)
discreteoffset=0.2
barwidth=0.4;
xaxis display=(nolabel) ;
yaxis display=(nolabel);
keylegend /location=inside position=bottomright across=1;
alexgonzalez
Quartz | Level 8

This is a good alternative, although the plot does not look as nice as with the overlaid bars.

Thank you!

Rick_SAS
SAS Super FREQ

What is 'bwh' in your code?

 

Regardless, the answer is 'yes' if you are running SAS 9.4M5. See the last section in the article "5 tips for customizing legends in PROC SGPLOT in SAS"

for examples of how to use the LEGENDITEM statement. You will have to specify what "shape"  (marker, line, filled area,...) to appear in the legend. In the following example, I use TYPE=LINE to plot a line, but you can change that option if you want a different choice.

 


data top_sources_plots;
length food $10;
input food percent mean;
datalines;
bread     20 15
cheese    15 8
pasta     12 10
veggies   10 7
;

proc sgplot data=top_sources_plots ;
hbarparm
category=food response=percent / datalabel
       datalabelattrs=(color=blue)
       fillattrs =(color=bwh)
       outlineattrs =(color=bwh); /*variable with higher values should appear first*/
yaxis display=(nolabel) ;
hbarparm category=food response=mean / datalabel
      datalabelattrs=(color=brown)
      fillattrs =(color=bwh)
      outlineattrs =(color=bwh);
xaxis display=(nolabel) ;
yaxis display=(nolabel);
legenditem type=line name="Percent" /
      label="Percent" lineattrs=(color=blue); 
legenditem type=line name="Mean" /
      label="Mean" lineattrs=(color=brown); 
keylegend "Percent" "Mean" /location=inside position=bottomright across=1;
run;
alexgonzalez
Quartz | Level 8

Hello Rick,

I'm assigning the same color 'bwh' to both overlaid bars (i.e. mean and percent). I'm using SAS EG 5.1, so it seems the 'legenditem' option is not supported.

Thanks,

AG. 

PeterClemmensen
Tourmaline | Level 20

Just to be clear. You want to control the color of the text in the legend, correct?

Rick_SAS
SAS Super FREQ

Thanks for the clarification, Draycut.

 

The LEGENDITEM statement supports text as well. Here's a way to get colored text for the blue/brown labels:

 

legenditem type=text name="Percent" /
      label="Percent" text="nn%" textattrs=(color=blue); 
legenditem type=text name="Mean" /
      label="Mean" text="nn" textattrs=(color=brown); 
alexgonzalez
Quartz | Level 8

Not exactly, in the plot I assigned blue color to the values of mean and brown to percents using datalabelattrs. What I want is that the legend specify that blue values represent mean and brown represent percent. Hope this is more clear.

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 9 replies
  • 1201 views
  • 0 likes
  • 4 in conversation