SGPlot Vbox: Changing data order on X-axis

Reply
N/A
Posts: 1

SGPlot Vbox: Changing data order on X-axis


Hi,

I am trying to create a box and whiskers plot using sgplot but am having problems getting the x-axis data ordered in the way I want.  My data takes the following form:

data test;

     infile datalines delimiter=',';

     input trtgrp $ value;

     75ug,3

     75ug,6

     75ug,2

     300ug,6

     300ug,4

     300ug,12

     900ug,12

     900ug,24

     900ug,8

;

and the procedure I am using is:

proc sgplot data=work.test;

     vbox value/category = trtgrp;

run;

The issue is that, because "trtgrp" is in character format, the x-axis order is 300ug, 75ug, 900ug instead of ascending dose.  I am not sure how to correct this.  I tried to create another data column which converted the character "75ug" to a numeric 75. but this did not help either.  My last option is to assign each trtgrp a number 1-4 with 75ug being 1 and then adding a legend describing what the value is but this is not exactly clean.

Any help would be much appreciated!

Respected Advisor
Posts: 4,641

Re: SGPlot Vbox: Changing data order on X-axis

Add the statement :


xaxis discreteorder=data;


to sgplot.

PG

PG
Super User
Posts: 10,483

Re: SGPlot Vbox: Changing data order on X-axis

Or modify the value by adding space to the front so the sort sequence works.

 

data test;

infile datalines delimiter=',';

input trtgrp $ value;

if trtgrp ='75ug' then trtgrp=' 75ug';

datalines;

75ug,3

75ug,6

75ug,2

300ug,6

300ug,4

300ug,12

900ug,12

900ug,24

900ug,8

;

run;

Though this is a tad clunky if you have lots of different values. The reason you conversion to numeric may not have worked was you have defined trtgrp as text, so it will maintain the behavior.

Another approach to read as numeric:

 

data test;

infile datalines delimiter=',ug';

input trtgrp value;

label trtgrp = 'Treatment group (ug)';

datalines;

75ug,3

75ug,6

75ug,2

300ug,6

300ug,4

300ug,12

900ug,12

900ug,24

900ug,8

;

run;

Ask a Question
Discussion stats
  • 2 replies
  • 517 views
  • 0 likes
  • 3 in conversation