BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.

Hello,

I've created two graphs from my data, one an overlaid histogram (HISTOGRAM statement) and one a clustered histogram (VBAR statement). Here is my code:

 

proc format;value $race '1 White'='W' '2 Black'='B' '3 Hispanic and/or Latino'='H/L'
'4 Asian'='A' '6 Mixed'='M' '7 American Indian, Alaska Native, Hawaiian, or Pacific Islander'='AI/AN/H/PI';run;
/*****Histogram, overlaid*****/
proc sgplot data=ALIR2.Miss_Surveys;styleattrs datacolors=(Blue Green Red VLIP DEYBR Yellow);
where race~='';
format race $race.;histogram Week / group=race transparency=0.5;
xaxis label='Weeks' LABELATTRS=(size=14) VALUEATTRS=(size=13);
keylegend  / title='Race/ethnicity' TITLEATTRS=(size=16) valueattrs=(size=12); 
yaxis LABELATTRS=(size=14) VALUEATTRS=(size=13) grid GRIDATTRS=(color=LIGGR);run;
/*****Histogram, clustered*****/
proc sgplot data=ALIR2.Miss_Surveys;styleattrs datacolors=(Blue Green Red VLIP DEYBR Yellow);
where race~='';
format race $race.;vbar Week / group=race groupdisplay=cluster STAT=PCT;
xaxis label='Weeks' LABELATTRS=(size=14) VALUEATTRS=(size=13) values=(0 to 21 by 3);
keylegend  / title='Race/ethnicity' TITLEATTRS=(size=16) valueattrs=(size=12); 
yaxis LABELATTRS=(size=14) VALUEATTRS=(size=13) grid GRIDATTRS=(color=LIGGR);run;

Here are the graphs that come out:

confooseddesi89_0-1693680329288.png

confooseddesi89_1-1693680336134.png

 

The clustered VBAR (second graph) comes out the way I need, both with regard to the order of the groups and the bar colors. However, the histogram (first graph) has the groups in the wrong order (should be W first, AI/AN/H/PI last) and the wrong bar colors. I've Googled around and can't seem to find a solution. Can you help make the histogram order of groups and the bar colors to be the same as the clustered VBAR? Thanks.

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

Data really helpful to test things with.

 

You do realize that transparency makes your colors appear a bit differently don't you? With a HISTOGRAM the entire bar for each value overlays and with moderately transparent settings, like .5 the color will not have full saturation, i.e. "green" will appear to be a light green shade. Then because Histogram overlays the bars the transparency "melds" the colors.

 

Lets look at a simple example where there are only two groups using a data set you can run the code with, you should have sashelp.class available.

proc sgplot data=sashelp.class;
   styleattrs datacolors = (blue red);
   histogram height /group=sex transparency=.5;
run;

SGPlot4.png

See how the blue and red overlap to create a violet (ish) color? That is why most of your colors look quite different than you want. If you were not aware that Histogram does this sort of overlaying, not stacking or separating, then perhaps you do not want a histogram plot at all. You may get a clearer picture with Sgpanel and Panelby race with a separate histogram for each.

Or remove transparency and you will only see "last" group on top which if it has a shorter bar than another group will show sticking up above.

 

ORDER means that may need to sort your data by RACE to get them into display order.

Suggestion, if you are going to bother to have a custom format you might as well use a numeric value for the property and then it will tend to sort automatically for many purposes.

 

View solution in original post

1 REPLY 1
ballardw
Super User

Data really helpful to test things with.

 

You do realize that transparency makes your colors appear a bit differently don't you? With a HISTOGRAM the entire bar for each value overlays and with moderately transparent settings, like .5 the color will not have full saturation, i.e. "green" will appear to be a light green shade. Then because Histogram overlays the bars the transparency "melds" the colors.

 

Lets look at a simple example where there are only two groups using a data set you can run the code with, you should have sashelp.class available.

proc sgplot data=sashelp.class;
   styleattrs datacolors = (blue red);
   histogram height /group=sex transparency=.5;
run;

SGPlot4.png

See how the blue and red overlap to create a violet (ish) color? That is why most of your colors look quite different than you want. If you were not aware that Histogram does this sort of overlaying, not stacking or separating, then perhaps you do not want a histogram plot at all. You may get a clearer picture with Sgpanel and Panelby race with a separate histogram for each.

Or remove transparency and you will only see "last" group on top which if it has a shorter bar than another group will show sticking up above.

 

ORDER means that may need to sort your data by RACE to get them into display order.

Suggestion, if you are going to bother to have a custom format you might as well use a numeric value for the property and then it will tend to sort automatically for many purposes.

 

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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
  • 1 reply
  • 649 views
  • 1 like
  • 2 in conversation