BookmarkSubscribeRSS Feed
AlexeyS
Pyrite | Level 9

Hi,

I created a histogram and added a density plots for each group.

1. I want to change the colors of each group(the colors of histogram and density have to be the same for each group)

2. I want to add some basic statistics such mean, number of observation for each group. I tried to do it using inset statement, but my problem, how can i use inset statement for each group, i.e the first row statistics will be for the first group, the second row, will be for the second group

 

ods graphics / noborder;

proc sgplot data=test;
  histogram amount   / group=id transparency=0.5;       /* SAS 9.4m2 */
  density   amount   / type=normal group=id;      /* overlay density estimates */
  legenditem type=marker name="1"  / label="Try1" markerattrs=(color=red symbol=circle) labelattrs=( Family="Arial" Size=10 Style=italic Weight=bold);
  legenditem type=marker name="2"  / label="Try2"  markerattrs=(color=blue symbol=circle) labelattrs=(Family="Arial" Size=10 Style=italic Weight=bold);
  keylegend "1" "2"/ location=inside position=topright across=1 noborder AUTOITEMSIZE;
  inset "Average = &avg" "NObs = &nobs" / border title="Try1" TITLEATTRS=(Color=Green Family=Arial Size=8 Style=Italic Weight=Bold) position=topleft TEXTATTRS = (SIZE=6 COLOR=red Family=Arial Size=8 Style=Italic);
run;

Thank you

3 REPLIES 3
ballardw
Super User

Data is nice.

 

If you want the same characteristics, such as color, line type, marker, for a specific group variable value when creating different graphs then you likely want a DATTRMAP data set which allows you to specify such for values.

I am afraid that your phrase "(the colors of histogram and density have to be the same for each group)" seems to say you want all the groups to have the same color which doesn't make much sense.

 

You may also want to investigate XAXISTABLE

 

Here's a sort of workable example with a data set you should be able to test.

data myattrmap;
  id="myid"; value="M";
  linecolor="blue"; fillcolor="blue";
  textcolor="blue"; textstyle="normal";
  textweight="normal";
output;

id="myid"; value="F";
  linecolor="red"; fillcolor="pink";
  textcolor="red"; textstyle="italic";
  textweight="bold";
output;
run;

proc sgplot data=sashelp.class dattrmap=myattrmap;
/*  vbar age / response=height group=sex stat=mean*/
/*     groupdisplay=cluster attrid=myid;*/
  histogram weight / group=sex transparency=.5 attrid=myid;
  density weight /type=normal group=sex attrid=myid;
  xaxistable height / textgroup=sex class=sex
                     textgroupid=myid;
run;

This is displaying all the values of height on separate lines for male and female aligned with the numeric value of the xaxis of the students corresponding weight, so isn't pretty when weight values are close. If you use a different variable instead of height on the Xaxistable then that value is displayed in the same positions.

 

There are lots of options on what values are displayed with the XAXISTABLE (some statistics can be calculated), what variable to align with the Xaxis values.

Jay54
Meteorite | Level 14

Grouped Histogram and Density should give you matching group colors by default.

 

proc sgplot data=sashelp.cars(where=(type ne 'Hybrid'));
histogram mpg_city / group=origin nbins=20 transparency=0.8; /* SAS 9.4m2 */
density mpg_city / type=normal group=origin;
run;

 

 

DanH_sas
SAS Super FREQ

For this use case, you might want to consider using SGPANEL instead of SGPLOT. Here is a little example of how you can create a panel that will stack your histograms and giving the ability to have inset information in each cell.

 

proc summary data=sashelp.class nway;
class sex;
var weight;
output out=temp mean=avg n=nobs;
run;

proc sort data=sashelp.class out=classtemp; by sex; run;

data class;
merge classtemp temp;
by sex;
label avg="Avg. Weight" nobs="Obs Count";
run;

proc sgpanel data=class noautolegend;
panelby sex / layout=rowlattice onepanel novarname;
histogram weight;
density weight;
inset avg nobs / separator=":";
run;

SGPanel.png

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 16. 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
  • 3 replies
  • 3928 views
  • 1 like
  • 4 in conversation