Data visualization with SAS programming

SAS 9.3 sgplot how to make one bar a different color than all the rest of the bars,

Reply
Occasional Contributor
Posts: 5

SAS 9.3 sgplot how to make one bar a different color than all the rest of the bars,

Hello, I am using SAS 9.3.  In the code below for a non-grouped chart, I would like to color the bar for "Jane" differently than all the other bars on the graph.  If possible, I would also like to color the marker associated with "Jane" a different color than the other markers.  Also, if possible, I would like to have the tick value of  "Jane" on the xaxis to stand out somehow, maybe bold or different color. If different colors is not an option then how can I place a refline on the xaxis for "Jane".  Basically I need a way to make "Jane" stand out on the graph from all the other names on the xaxis.  My real data is not grouped and the values on the xaxis are text values.

I have tried using an attribute map to change the bar color, but that does not seem to work.  Please see my code below.  Thank you!

data attrmap;

  id='myid'; value='Jane'; fillcolor='orange'; output;

run;

proc sgplot data=sashelp.class

                 dattrmap=attrmap;

  vbar name / response = height

                    attrid = myid

                    name = 'bar'

                    legendlabel= 'Height';

  vline name /  response = weight

                     name = 'line'

                     legendlabel = 'Weight'

                     y2axis

                     markers

                     markerattrs=(symbol=circlefilled color=mor) 

                     lineattrs=(color=mor);

  xaxis display=(nolabel);

  yaxis grid

           label = 'Height'

           offsetmin=0

           offsetmax=0

           thresholdmax = 1

           thresholdmin = 1; 

  y2axis label = 'Weight'

            offsetmin=0

            offsetmax=0

            thresholdmax = 1

            thresholdmin = 1;

run;

SAS Super FREQ
Posts: 1,044

Re: SAS 9.3 sgplot how to make one bar a different color than all the rest of the bars,

You can do this one of two ways.  The simplest one is to create a group column with a name say "ID".  Assign "Jane" to have ID=1, and all others as ID=2.  Then assign GROUP=id in VBar or other statements.  This will also work with overlaid scatters. Colors will come from style.  With SAS 9.4, you can use STYLEATTRS to assign the colors.

Or, you can separate "Jane" into a different variable "Name2", and make "Jne" from initial variable to missing.  Now use two VBAR statements, one fowith Name1, one with Name2.  Now you can assign the specific color (or other) attributes. to each statement.  This will also work with overlaid scatters, you just need two VBars and two Scatters.

Attrmaps work ONLY with groups. There is no way to change just one tick value to have a different color.  But you can use SGAnnotate to do that.

Post a Question
Discussion Stats
  • 1 reply
  • 291 views
  • 0 likes
  • 2 in conversation