I want to annotate a boxplot to have some bars in one color and some in another. I try with the code below but it's not working. Why? What I want is to have last two bars (for year > 2018) red, while all other green
/* Sample dataset*/
data test;
call streaminit(123);
do year= 2010 to 2020;
do i = 1 to 100;
year = year;
var1 = RAND('UNIForm', 30, 100);
output;
end;
end;
run;
/* Create groups based on year*/
data test;
set test;
group = (year > 2018);
run;
/* Specify colors*/
data my_colors;
input id $ value fillcolor $;
datalines;
myid 0 CX96ceb4
myid 1 CXff6f69
;
run;
/* Annotated box plot*/
proc sgplot data = test dattrmap=my_colors;
vbox var1/ category = year nooutliers nomean attrid = myid;
run;
When I try to set colors "by hand" for all bars, it works OK:
proc sgplot data = test;
vbox var1/ category = year nooutliers nomean fillattrs=(color = "CX96ceb4");
run;
Hello @chris2377,
Use the GROUP= option to tell SAS that the values 0, 1 in the attribute map dataset refer to variable group of dataset TEST:
vbox var1/ category = year group = group nooutliers nomean attrid = myid;
Hello @chris2377,
Use the GROUP= option to tell SAS that the values 0, 1 in the attribute map dataset refer to variable group of dataset TEST:
vbox var1/ category = year group = group nooutliers nomean attrid = myid;
You're missing the GROUP=group on the VBOX statement needed to look up the values in the attrmap. I also had to retype the "myid" values in the attrmap, as it appeared to have hidden characters that caused a mismatch. But, that could have been a cut-and-paste artifact.
Hope this helps!
Dan
Thanks for your reply. After adding "group" option everything works as expected, so it may be the copy-paste issue.
@DanH_sas wrote:
I also had to retype the "myid" values in the attrmap, as it appeared to have hidden characters that caused a mismatch.
Indeed, the indentation of that data step was achieved by a leading tab character. I hadn't even noticed these tabs as the Enhanced Editor (unlike the SAS Studio interface, I think) treats them as blanks.
@chris2377: It's better to avoid tab characters in SAS code. There are option settings like "Insert spaces for tabs" available both in the Enhanced Editor and in SAS Studio to create a specified number of space characters when you press the Tab key.
Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!
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.