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

I am currently having a nightmare with some kernel density plots I am trying to produce. For some reason in the plots where I am plotting two kernel density lines, despite showing correctly in the SAS output window, the RTF file I am creating show both lines as the same colour. I do not have the same issue in the plots with 3 or more kernel density plots overlaid. These are showing fine as different colours in both the SAS output window and the output RTF file.

 

Code below:

 

proc sgplot data=graph CYCLEATTRS;
%do r=1 %to &n;
density &endpoint&r / type=kernel freq=frequency name="&r" legendlabel="&&class&r (n=&&count&r)" lineattrs=(pattern=solid thickness=2);
%end;
keylegend /
valueattrs=(Size=7);
yaxis label='Density';
run;

 

Any help would be greatly appreciated!

1 ACCEPTED SOLUTION

Accepted Solutions
Rick_SAS
SAS Super FREQ

Yes, what you report is true. It has to do with the RTF style being different than modern styles such as the HTML styles. What happens is

  • When there are two curves, the plot uses the GRAPHFIT and GRAPHFIT2 attributes for the curves.
  • In an HTML style, GRAPHFIT and GRAPHFIT2 have different colors. But in the RTF style, the colors are similar, but one curve is drawn by using a dashed line. Since you have explicitly set PATTERN=SOLID, the two curves look the same in RTF.
  • When there are more than two curves, the procedure uses the GRAPHDATA1, GRAPHDATA2, etc, attributes, which give the curves different colors.

There are two workaround. You could delete the PATTERN=SOLID option, but I am assuming you don't want to do this. The second workaround is to modify your macro loop to explicitly use the GRAPHDATA family of attributes, like this:

proc sgplot data=graph CYCLEATTRS;
%do r=1 %to &n;
density &endpoint&r / type=kernel freq=frequency name="&r" legendlabel="&&class&r (n=&&count&r)" 
        lineattrs=GraphData&r.(pattern=solid thickness=2);
%end;
keylegend /
valueattrs=(Size=7);
yaxis label='Density';
run;

View solution in original post

4 REPLIES 4
FreelanceReinh
Jade | Level 19

Hello @SamR and welcome to the SAS Support Communities!

 

This is interesting. I have reproduced the issue and found a difference in the GTL (Graph Template Language) code created by adding the TMPLOUT= option to the PROC SGPLOT statement: With only two density curves the corresponding DensityPlot statements use the following LINEATTRS= options:

Lineattrs=GraphFit( Pattern=1 Thickness=2)
Lineattrs=GraphFit2( Pattern=1 Thickness=2)

By adding a third density curve the GraphFit style elements are replaced by GraphData style elements:

Lineattrs=GraphData1( Pattern=1 Thickness=2)
Lineattrs=GraphData2( Pattern=1 Thickness=2)
Lineattrs=GraphData3( Pattern=1 Thickness=2)

Which makes sense because there are about 15 GraphData style elements, but only two GraphFit style elements. Apparently, GraphData work better with your line pattern and thickness specifications.

 

So I suggest that you enforce the use of GraphData by adding that to your existing LINEATTRS= option of the DENSITY statement:

... lineattrs=GraphData&r(pattern=solid thickness=2);

Does this work in your environment?

DanH_sas
SAS Super FREQ

@FreelanceReinh is absolutely correct. For the RTF style used by ODS RTF by default, the GraphFit and GraphFit2 colors are the same color -- differentiable only by the line pattern and thickness. When you overrode those attributes, the lines looked the same.

 

You have two options:

1) Use a different ODS style. The available styles will vary based on your SAS version.

2) Change your macro to set GraphDataN ("N" replaced by your loop index) on each of your density plots. For example, iineattrs=GraphData1(pattern=solid thickness=2)

 

Hope this helps!

Rick_SAS
SAS Super FREQ

Yes, what you report is true. It has to do with the RTF style being different than modern styles such as the HTML styles. What happens is

  • When there are two curves, the plot uses the GRAPHFIT and GRAPHFIT2 attributes for the curves.
  • In an HTML style, GRAPHFIT and GRAPHFIT2 have different colors. But in the RTF style, the colors are similar, but one curve is drawn by using a dashed line. Since you have explicitly set PATTERN=SOLID, the two curves look the same in RTF.
  • When there are more than two curves, the procedure uses the GRAPHDATA1, GRAPHDATA2, etc, attributes, which give the curves different colors.

There are two workaround. You could delete the PATTERN=SOLID option, but I am assuming you don't want to do this. The second workaround is to modify your macro loop to explicitly use the GRAPHDATA family of attributes, like this:

proc sgplot data=graph CYCLEATTRS;
%do r=1 %to &n;
density &endpoint&r / type=kernel freq=frequency name="&r" legendlabel="&&class&r (n=&&count&r)" 
        lineattrs=GraphData&r.(pattern=solid thickness=2);
%end;
keylegend /
valueattrs=(Size=7);
yaxis label='Density';
run;
SamR
Calcite | Level 5

Thank you so much for the responses. Added in lineattrs=GraphData&r. works!

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 4 replies
  • 529 views
  • 4 likes
  • 4 in conversation