Data visualization with SAS programming

Symbols in a scatterplot with grouped data

Accepted Solution Solved
Reply
Contributor ckx
Contributor
Posts: 50
Accepted Solution

Symbols in a scatterplot with grouped data

I'm experimenting with ods graphics to create a scatterplot for a grouped variable with two categories. The values are marked by a circle and a plus sign but I'd prefer to use 'X' and 'Y'.

This is how I assumed it should be done:

proc sgscatter data=mydata;

    plot myXvar*mYvar / group=myGroup|Var MARKERATTRS=GraphData1(symbol=X) MARKERATTRS=GraphData2(symbol=Y);

run;

But this sets the symbols in both groups to "Y", the first markerattrs option is ignored.

From the SAS 9.4 documentation:

MARKERATTRS=style-element <(options)> | (options

GraphData1 ... GraphDatan style elements in the current style for grouped data.

SAS(R) 9.4 ODS Graphics: Procedures Guide, Fourth Edition

One way to do this would be to use a modified template but I'd prefer not to do it that way. I use Enterprise Guide to develop the program, using a template would complicate matters.

Does anyone know how to create separate symbols using the markerattrs options in sgscatter (or another ods graphics procedure? I'm using SAS 9.4 with EG 6.1. I'm using the "analysis" style for the ods graphics.


Accepted Solutions
Solution
‎01-12-2015 05:27 AM
Super Contributor
Posts: 252

Re: Symbols in a scatterplot with grouped data

Hi,

There are at least two ways to do this:

One of the simpler ways is to use the marker character option, as in the example below. However, the legend does not contain the X and Y. All you need to do is adapt the program so that one of your groups are assigned the letter "X" and the other group is assigned the letter "Y".

Because you have SAS 9.4 though, it might be better if you use the Attribute maps option, as also shown in the example further below. This also gives you the X and Y symbols on your legend.


data cars;
  set sashelp.cars;
  where make in ("Acura","Audi");
  if make = "Acura" then Groupmarker = "X";
  else if make = "Audi" then Groupmarker = "Y";
  keep make cylinders horsepower Groupmarker;
run;

/* Using Markercharacter option */

proc sgplot data = cars;
  scatter x = CYLINDERS y = HORSEPOWER / group = make markercharacter = Groupmarker;
run;


data myattrmap;
  input ID $ value $ markersymbol $ ;
  datalines;
  myid  Acura X
  myid  Audi Y
  ;
run;


/* Using Attribute Maps */

proc sgplot data=cars dattrmap=myattrmap;
  scatter x = CYLINDERS y = HORSEPOWER / group = make attrid=myid;
run;

Please let me know if you have any questions.

View solution in original post


All Replies
Solution
‎01-12-2015 05:27 AM
Super Contributor
Posts: 252

Re: Symbols in a scatterplot with grouped data

Hi,

There are at least two ways to do this:

One of the simpler ways is to use the marker character option, as in the example below. However, the legend does not contain the X and Y. All you need to do is adapt the program so that one of your groups are assigned the letter "X" and the other group is assigned the letter "Y".

Because you have SAS 9.4 though, it might be better if you use the Attribute maps option, as also shown in the example further below. This also gives you the X and Y symbols on your legend.


data cars;
  set sashelp.cars;
  where make in ("Acura","Audi");
  if make = "Acura" then Groupmarker = "X";
  else if make = "Audi" then Groupmarker = "Y";
  keep make cylinders horsepower Groupmarker;
run;

/* Using Markercharacter option */

proc sgplot data = cars;
  scatter x = CYLINDERS y = HORSEPOWER / group = make markercharacter = Groupmarker;
run;


data myattrmap;
  input ID $ value $ markersymbol $ ;
  datalines;
  myid  Acura X
  myid  Audi Y
  ;
run;


/* Using Attribute Maps */

proc sgplot data=cars dattrmap=myattrmap;
  scatter x = CYLINDERS y = HORSEPOWER / group = make attrid=myid;
run;

Please let me know if you have any questions.

Contributor ckx
Contributor
Posts: 50

Re: Symbols in a scatterplot with grouped data

Thanks for the quick reply, it worked brilliantly! I'm particularly happy to learn about the dattrmap option, that adds a lot of flexibiltiy.

Super Contributor
Posts: 252

Re: Symbols in a scatterplot with grouped data

You're welcome. Yes that's correct Attribute Maps add a lot of flexibility! Did you use the dattrmap option for your example?

Contributor ckx
Contributor
Posts: 50

Re: Symbols in a scatterplot with grouped data

I tried both options.The dattrmap option gave me a spot of trouble because I had a numeric grouping variable. After some experimentation I found that creating the variable VALUE from the formatted values of my group variable worked great.

Super Contributor
Posts: 252

Re: Symbols in a scatterplot with grouped data

That's great! Oh yes I remember when I initially had problems when using dattrmaps with formats...

SAS Super FREQ
Posts: 1,044

Re: Symbols in a scatterplot with grouped data

If you have SAS 9.4M1, the right way to do this is using the SYMBOLCHAR statement.  This statement allows you to define a marker from any Unicode font symbol.  The legend will also include the symbol.  See example code below.  There is also a similar SYMBOLIMAGE statement..

proc sgplot data=sashelp.class;

  symbolchar name=X  char='0058'x / scale=2;

  symbolchar name=Y  char='0059'x / scale=2;

  styleattrs datasymbols=(X Y);

  scatter x=height y=weight / group=sex;

  run;

Contributor ckx
Contributor
Posts: 50

Re: Symbols in a scatterplot with grouped data

Yes indeed, the symbolchar method works as well. I think I would still prefer the dattrmap option because the documentation seems to be clearer. But that's just a ver first impression.

SAS Super FREQ
Posts: 1,044

Re: Symbols in a scatterplot with grouped data

For future reference, with SAS 9.4, all you really need is a STYLEATTRS statement as shown below.  You can use StyleAttrs to set group symbols, colors, line patterns,etc.

proc sgplot data=cars;

  styleattrs datasymbols=(x y);

  scatter x = CYLINDERS y = HORSEPOWER / group = make;

run;

STYLEATTRS was added at SAS 9.4.  With SAS 9.3, Attrmap is the way to go, and is effectively doing the same thing.  Yes, this can be a bit confusing, but that is the price of having a system that is being actively enhanced.  :-)

However, if you need some symbol that is not included in the pre-defined list, you can define it using SYMBOLCHAR, and then use StyleAttrs or Attrmap to include it in the list of group symbols.

Super Contributor
Posts: 252

Re: Symbols in a scatterplot with grouped data

Hi Sanjay,

Out of interest, does Styleattrs work with the group = variable? If so what will happen if you use more than one group = variable please?

Many thanks

SAS Super FREQ
Posts: 1,044

Re: Symbols in a scatterplot with grouped data

Yes, it works with group case, and also for direct assignment of attributes.

STYLEATTRS is simply an easy, in-line code way to change the GraphData1-12 elements in the active style.  Instead of having to derive a new style with the changed group values, you can define the new values in the SGPLOT code itself.  This is a easy way for a quick, non persistent change.  For persistent change, it is still better to derive a corporate style.

STYLEATTRS has four bundles:

  • DATACOLORS for the fill colors,
  • DATACONTRASTCOLORS for the line and marker colors,
  • DATASYMBOLS
  • DATALINEPATTERNS. 

The list you provide REPLACES the list from the style.  In the case above, I changed the Symbols list to (X, Y).  So, now the graph will cycle through only these two marker symbols.  All other group lists will stay unchanged.  For symbols, you can include the pre-defined symbol names (like Circle, CircleFilled, etc), or a new symbol name created using the SYMBOLCHAR or SYMBOLIMAGE statements. 

Also remember, the HTMLBlue style has ATTRPRIORITY=COLOR.  So, symbols change only after the colors are exhausted. To see symbol cycling, use a different style, or set ATTRPRIORITY=NONE in the ODS Graphics statement.  Sounds like a good topic for a blog article.  :-)

Super Contributor
Posts: 252

Re: Symbols in a scatterplot with grouped data

Thank you for the explantion!

It looks so easy to use, and it appears that the Colors and Symbols can be changed simultaneously. It's a welcomed upgrade to the %modstyle macro.

I agree, it sounds like a good topic for Graphically Speaking :-)

SAS Super FREQ
Posts: 1,044

Re: Symbols in a scatterplot with grouped data

Post a Question
Discussion Stats
  • 12 replies
  • 2150 views
  • 11 likes
  • 3 in conversation