Data visualization with SAS programming

How do I control color of individual boxes in a box plot in gplot?

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 7
Accepted Solution

How do I control color of individual boxes in a box plot in gplot?

Hello,

I am relatively new to graphing in SAS, but am finding it great to have the level of control it provides.  I have succeeded in making a box and whisker plot that meets all my specifications, but one.  I have 8 groups (4 classes of each of two groups).  I want the boxes of one group to be red, the other to be blue.  Group 1 is values 1.0, 2.0, etc.  Group 2 is 1.3, 2.3 etc.  I want group 1 to be red, group 2 to be blue.  I have searched, but have not seen a way to change color of individual boxes.  I'm sure there is a more elegant way to do what I want, but would appreciate knowing how to control color of individual box and whisker displays, given the code I have thus far.

Thank you.

data temp;

set grant.ayp2010w2009_2;

label ma_rg_ach_level_mb09 = "Math 2009 Achievement Level"

new_abc_math_g = "Math 2010 Growth";

if grade = '04' or grade = '05';

if ma_rg_ach_level_mb09 = 'NULL' then ma_rg_ach_level_mb09 = ' ';

if ma_rg_ach_level_mb09 = 1 and SWD = 'N' then group = 1.0;

if ma_rg_ach_level_mb09 = 1 and SWD = 'Y' then group = 1.3;

if ma_rg_ach_level_mb09 = 2 and SWD = 'N' then group = 2.0;

if ma_rg_ach_level_mb09 = 2 and SWD = 'Y' then group = 2.3;

if ma_rg_ach_level_mb09 = 3 and SWD = 'N' then group = 3.0;

if ma_rg_ach_level_mb09 = 3 and SWD = 'Y' then group = 3.3;

if ma_rg_ach_level_mb09 = 4 and SWD = 'N' then group = 4.0;

if ma_rg_ach_level_mb09 = 4 and SWD = 'Y' then group = 4.3;

proc sort;

by group;

ods graphics on;

title ;

symbol

value = dot

height = 0.4

interpol = boxtf

width = 3

bwidth = 5

co = BL

cv = VLIGB

mode = include;

axis1

order = (-3, -2, -1, 0, 1, 2, 3)

label = (f= "Arial/Bold" height = 2.25 angle = 90 'Mathematics Growth Score 2010')

minor = (number = 1)

value = (f= "Arial" height = 1.75)

width = 2

color = black;

axis2

label = (f= "Arial/Bold" height = 2.25 'Math Proficiency Level 2009')

offset = (5, 5)

value = (f= "Arial" height = 1.75);

proc gplot data = temp

uniform ;

plot new_abc_math_g*group /

haxis = axis2

vaxis = axis1

hminor = 0

skipmiss ;

run;

quit;


Accepted Solutions
Solution
‎06-24-2014 12:47 PM
Grand Advisor
Posts: 9,699

Re: How do I control color of individual boxes in a box plot in gplot?

You might try adding a variable for colorgroup or some such.

Then the plot statement becomes

plot new_abc_math_gr*group=colorgroup

And add a symbol2 statement with the color changes.

View solution in original post


All Replies
Solution
‎06-24-2014 12:47 PM
Grand Advisor
Posts: 9,699

Re: How do I control color of individual boxes in a box plot in gplot?

You might try adding a variable for colorgroup or some such.

Then the plot statement becomes

plot new_abc_math_gr*group=colorgroup

And add a symbol2 statement with the color changes.

Occasional Contributor
Posts: 7

Re: How do I control color of individual boxes in a box plot in gplot?

Thank you, those changes did indeed produce two different colored box plots.  Because both groups are represented in the four categories on the abscissa, the two sets of box plots sit right on top of one another with these changes.  I need to offset them from the tick marks so one can see both sets of boxes.  I am now playing with my options to get that done.  I've tried switching to a numeric variable on the abscissa and changing values of two groups slightly (e.g., 1 becomes .8 for one group and 1.2 for the other group).  I guess my other option is to overlay one graph on top of the other with an offset. 

SAS Super FREQ
Posts: 863

Re: How do I control color of individual boxes in a box plot in gplot?

I'm not sure of your SAS version, but the boxplot you describe it simple to do with PROC SGPLOT. Try this example and see if it works for you:

proc sgplot data=sashelp.cars;

vbox mpg_city / category=origin group=type;

run;

Grand Advisor
Posts: 9,699

Re: How do I control color of individual boxes in a box plot in gplot?

I would try making more noticeable changes in the horizontal axis values and possibly adjust the AXIS statement to control tickmarks and such. Also you don't have a units specified for Width or Bwidth in the symbol statement. You may want to use a unit like bwidth=.2in or similar to get finer control of actual widths as the default is Cells which can be largish for some displays.

Post a Question
Discussion Stats
  • 4 replies
  • 379 views
  • 0 likes
  • 3 in conversation