BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
SASuserlot
Barite | Level 11

I am looking to create a scatter plot. when I run my program its giving the default colors and symbols for markers. I would like to know how can I control the colors   and symbols of the two different groups in the graph. In the below picture I am getting the red and blue colors by default. I have the following questions.

1. How I can control the color of the each group?

2. In the picture, both have the marker Circle filled, Can I have different symbol for each group?

3. How I can make the dotted line to regular line and regular line into dotted line?

4. In my Template procedure "megedleged" statment  staying in red, but no errors in log, do I miss any thing n the code. Please let me know

SASuserlot_2-1612903462077.png

 

 

SASuserlot_0-1612903252787.png

data ScatCurve;    /* example data: scatter plot and curves */
call streaminit(1);
do Group = 1 to 2;
   do x = 0 to 5 by 0.2;
      Pred = Group + (1/Group)*x - (0.2*x)**2;
      y = Pred + rand("Normal",0,0.5);
      output;
   end;
end;
run;
proc template;
  define statgraph ScatCurveLegend;
  dynamic _X _Y _Curve _Group _Title;       /* dynamic variables */
  begingraph;
  entrytitle _Title;  
   layout overlay; 
     scatterplot x=_X y=_Y / group=_Group name="obs" markerattrs=(symbol=CircleFilled);
     seriesplot x=_X y=_Curve / group=_Group name="curves" ;
     /* specify exactly two names for the MERGEDLEGEND statement */
     mergedlegend "obs" "curves" / border=true title=_Group;
   endlayout;
  endgraph;
  end;
run;
ods rtf file = "C:\Users\xxu\Desktop\New folder\xx_sample.rtf"  ; 
 
proc sgrender data=ScatCurve template=ScatCurveLegend;
   dynamic _Title="Overlay Curves, Merged Legend"
           _X="x" _Y="y" _Curve="Pred" _Group="Group";
run;
ods rtf close;

 

Thank you for your Reponses.

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

So, expanding on my suggestion, using STYLEATTRS gets you this code via SGPLOT

 

proc sgplot data=scatCurve tmplout="/home/fkhurshed/graph_code.sas";
styleattrs  datalinepatterns=(dot solid) datasymbols=(trianglefilled circlefilled) ;
scatter x= x y=y / group = group ;
series x= x y = Pred / group = group;
run;

The template then looks like this, with the datasymbols and dataLinePatterns options specified. This is ONE way to do this. Using a data attribute map is another option, and you can replicate this methodolgy to see how to implement it, using this similar question.

proc template;
define statgraph sgplot;
begingraph / collation=binary subpixel=on dataSymbols=( TRIANGLEFILLED CIRCLEFILLED ) dataLinePatterns=( 34 1 );
layout overlay / yaxisopts=(labelFitPolicy=Split) y2axisopts=(labelFitPolicy=Split);
   ScatterPlot X='x'n Y='y'n / subpixel=off primary=true Group='Group'n LegendLabel="y" NAME="SCATTER";
   SeriesPlot X='x'n Y='Pred'n / Group='Group'n LegendLabel="Pred" NAME="SERIES";
   DiscreteLegend "SCATTER"/ title="Group";
endlayout;
endgraph;
end;
run;

You could then modify your template code, the BEGINGRAPH statement to mimic this (and remove the marker option on the SCATTER) and it seems to work.

 


proc template;
  define statgraph ScatCurveLegend;
  dynamic _X _Y _Curve _Group _Title;       /* dynamic variables */
  begingraph / dataSymbols=( TRIANGLEFILLED CIRCLEFILLED ) dataLinePatterns=( 34 1 );
  entrytitle _Title;  
   layout overlay; 
     scatterplot x=_X y=_Y / group=_Group name="obs" ;
     seriesplot x=_X y=_Curve / group=_Group name="curves" ;
     /* specify exactly two names for the MERGEDLEGEND statement */
     mergedlegend "obs" "curves" / border=true title=_Group;
   endlayout;
  endgraph;
  end;
run;

proc sgrender data=ScatCurve template=ScatCurveLegend;
   dynamic _Title="Overlay Curves, Merged Legend"
           _X="x" _Y="y" _Curve="Pred" _Group="Group";
run;

View solution in original post

8 REPLIES 8
Reeza
Super User
My *lazy trick* to figuring out what options I need to change is to use SGPLOT and specify what I want using the STYLEATTRS statement and use the TMPLOUT option on the SGPLOT statement to pipe the code to a file and see how it was specified in their template.

STYLEATTRS statement
https://documentation.sas.com/?cdcId=pgmsascdc&cdcVersion=9.4_3.5&docsetId=grstatproc&docsetTarget=p...

TMPLOUT option
https://documentation.sas.com/?cdcId=pgmsascdc&cdcVersion=9.4_3.5&docsetId=grstatproc&docsetTarget=p...

SASuserlot
Barite | Level 11

Thanks. I am new to graphs. I will play with the code and the references you provided

ballardw
Super User

Have you looked at the DATTRMAP option for Proc Sgrender?

That uses a data set to control attributes of lines and markers based on values you provide.

SASuserlot
Barite | Level 11

Thanks for your reply,  I am new to graphs, I will search for what you mentioned. Thanks for giving me the direction.

Reeza
Super User

So, expanding on my suggestion, using STYLEATTRS gets you this code via SGPLOT

 

proc sgplot data=scatCurve tmplout="/home/fkhurshed/graph_code.sas";
styleattrs  datalinepatterns=(dot solid) datasymbols=(trianglefilled circlefilled) ;
scatter x= x y=y / group = group ;
series x= x y = Pred / group = group;
run;

The template then looks like this, with the datasymbols and dataLinePatterns options specified. This is ONE way to do this. Using a data attribute map is another option, and you can replicate this methodolgy to see how to implement it, using this similar question.

proc template;
define statgraph sgplot;
begingraph / collation=binary subpixel=on dataSymbols=( TRIANGLEFILLED CIRCLEFILLED ) dataLinePatterns=( 34 1 );
layout overlay / yaxisopts=(labelFitPolicy=Split) y2axisopts=(labelFitPolicy=Split);
   ScatterPlot X='x'n Y='y'n / subpixel=off primary=true Group='Group'n LegendLabel="y" NAME="SCATTER";
   SeriesPlot X='x'n Y='Pred'n / Group='Group'n LegendLabel="Pred" NAME="SERIES";
   DiscreteLegend "SCATTER"/ title="Group";
endlayout;
endgraph;
end;
run;

You could then modify your template code, the BEGINGRAPH statement to mimic this (and remove the marker option on the SCATTER) and it seems to work.

 


proc template;
  define statgraph ScatCurveLegend;
  dynamic _X _Y _Curve _Group _Title;       /* dynamic variables */
  begingraph / dataSymbols=( TRIANGLEFILLED CIRCLEFILLED ) dataLinePatterns=( 34 1 );
  entrytitle _Title;  
   layout overlay; 
     scatterplot x=_X y=_Y / group=_Group name="obs" ;
     seriesplot x=_X y=_Curve / group=_Group name="curves" ;
     /* specify exactly two names for the MERGEDLEGEND statement */
     mergedlegend "obs" "curves" / border=true title=_Group;
   endlayout;
  endgraph;
  end;
run;

proc sgrender data=ScatCurve template=ScatCurveLegend;
   dynamic _Title="Overlay Curves, Merged Legend"
           _X="x" _Y="y" _Curve="Pred" _Group="Group";
run;
SASuserlot
Barite | Level 11

Thanks for you detailed code, however I have a question extension to previous one, your code give default blue and red colors. Where we can control these?

Reeza
Super User
Colour of lines or markers?

Use the exact same approach to find the options, you should be able to extend my example. Are you having difficulty with some part of the approach?

I didn’t specifically ask, but is there a reason you’re using a template instead of SGPLOT directly?
SASuserlot
Barite | Level 11
thanks I figured out after reading your code further, I understand it better. I am learning the graphs so I was trying to do in both in procedures. Can you suggest which one easy and better if same table can achieve by both procedures! also I have question How we can merge the key legends in both procedures like, on the bottom of the graph ( placebo---------------, placebo: # then key legend should come as placebo ;-----#------

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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
  • 8 replies
  • 4213 views
  • 5 likes
  • 3 in conversation