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
Did you intentionally set the fill color for both bar charts to BWH?
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
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;
This is a good alternative, although the plot does not look as nice as with the overlaid bars.
Thank you!
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;
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.
Just to be clear. You want to control the color of the text in the legend, correct?
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);
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.
Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.
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.
Ready to level-up your skills? Choose your own adventure.