KH_
KH_

Reputation: 325

Styling different groups in SAS SGPLOT

I am trying to produce a graph with multiple groupings. The sample data code is:

proc sort data=sashelp.cars out=cars;
  by DriveTrain;
  where n(Cylinders);
run;

I used dattrmap to add distinct colors to the different groups as follow:

  data MyAttrMap;
   length MARKERCOLOR CONTRASTCOLOR color $25;
   ID='myreg'; value='All' ; MARKERCOLOR='red'; color='red'; MARKERSYMBOL = 'circle'; output;
   ID='myreg'; value='Front'; MARKERCOLOR='blue'; color='blue'; MARKERSYMBOL = 'circle'; output;
   ID='myreg1'; value='USA'; CONTRASTCOLOR='yellow'; color='yellow'; output;
   ID='myreg1'; value='Europe'; CONTRASTCOLOR='black'; color='black'; output;
   ID='myreg1'; value='Asia'; CONTRASTCOLOR='green'; color='green'; >       >       output;
  run;

  proc sgplot data=work.cars
   dattrmap=MyAttrMap;
   hbarparm category=enginesize response=horsepower/group=DriveTrain barwidth=.5 attrid=myreg name='dt';
  scatter X=MPG_City Y=enginesize /group=origin name='origin' attrid=myreg1;
   keylegend 'dt' / title='Drive Train' location=outside position=bottom;
  keylegend 'origin' / title='Origin' location=outside position=bottom;
  where DriveTrain in ('All' 'Front');
  run; 

The Attrmap was created with the intention of having different colors for Origin and DriveTrain however, when the output is created the same colors applied to Origin are applied to DriveTrain.

I also tried using Proc template to change the style as follow:

  /*Different colors from the ones used above*/
  proc template;
   define style MyStyle;
  parent = Styles.Listing;
   STYLE graphdata1 /
                MARKERSYMBOL = 'circle'
                 LINESTYLE = 1
                 CONTRASTCOLOR = liypk
                 COLOR = red
          ;
          STYLE graphdata2 /
                MARKERSYMBOL = 'circle'
                 LINESTYLE = 1
                 CONTRASTCOLOR = stybr
                 COLOR = yellow
          ;
          STYLE graphdata3 /
                MARKERSYMBOL = 'circle'
                 LINESTYLE = 1
               CONTRASTCOLOR = mog
                COLOR = green
         ;
          STYLE graphdata4 /
                MARKERSYMBOL = 'circle'
                LINESTYLE = 1
                CONTRASTCOLOR = brown
                COLOR = pink
         ;
          STYLE graphdata5 /
                MARKERSYMBOL = 'circle'
                LINESTYLE = 1
                CONTRASTCOLOR = black
                 COLOR = grey
          ;
   end;
  run;

But still the same results were obtained. Could anyone please tell me what I'm doing wrong or how to get this to work? I'm using SAS 9.3.

Another issue I'm encountering is the sorting. I want to sort the bars so that the same origins appear together and by the horsepower. I sorted using sortkey=national and used grouporder=data as recommended by SAS but this didn't change the ordering in the output. Any help is appreciated.

Here is the output.

Thanks.

Upvotes: 0

Views: 4745

Answers (2)

Reeza
Reeza

Reputation: 21274

Check your attribute map data set. Because you haven't specified the lengths for the Value and ID column they're truncated and don't match your data so they don't get assigned correctly.

Simplifying your problem, I assigned all the elements for testing:

I also assumed this was mocked up because of the errors in the log.

proc sort data=sashelp.cars out=cars;
    by DriveTrain;
    where n(Cylinders);
run;

data MyAttrMap;
    length ID $10. linecolor MARKERCOLOR CONTRASTCOLOR fillcolor color value $25;
    ID='myreg1';
    value='USA';
    contrastcolor='cxaf8dc3';
    LINECOLOR='cxaf8dc3';
    MARKERCOLOR='cxaf8dc3';
    fillcolor='cxaf8dc3';
    output;
    ID='myreg1';
    value='Europe';
    contrastcolor='cx7fbf7b';
    LINECOLOR='cx7fbf7b';
    MARKERCOLOR='cx7fbf7b';
    fillcolor='cx7fbf7b';
    output;
    ID='myreg1';
    value='Asia';
    contrastcolor='cx91bfdb';
    LINECOLOR='cxfc8d59';
    MARKERCOLOR='cxfc8d59';
    fillcolor='cxfc8d59';
    output;
run;

ods graphics / attrpriority=none;

proc sgplot data=work.cars dattrmap=MyAttrMap;
    scatter X=MPG_City Y=enginesize /group=origin name='origin' attrid=myreg1;
    where DriveTrain in ('All' 'Front');
run;

Upvotes: 0

Richard
Richard

Reputation: 27508

You might find SGPANEL a better option for visually presenting the distributions of different groups.

ods html style=normal;
ods graphics / height=900px;

proc sgpanel data=sashelp.cars;
  panelby origin 
  / columns=3
  ;

  hbar enginesize 
  / group=drivetrain
    groupdisplay=cluster
  ;

  where 
    DriveTrain in ('Front', 'All')
    and not missing(cylinders)
  ;
run;

enter image description here

Upvotes: 2

Related Questions