DATA Step, Macro, Functions and more

Change the x-axis order in SGPLOT

Accepted Solution Solved
Reply
Contributor
Posts: 37
Accepted Solution

Change the x-axis order in SGPLOT

Hi all,

 

My data contains patients' ID (idd) and charateristic of that patient (TroughHour) as follows:

                                                          Trough
                                          Obs     idd      Hour

                                            1    10002       1
                                            2    10003      23
                                            3    10005       2
                                            4    10006       2
                                            5    10007       4
                                            6    10008       0
                                            7    10009       4
                                            8    10010       3
                                            9    10011       0
                                           10    10012      22
                                           11    10013       4
                                           12    10014       2
                                           13    10015       4
                                           14    10017       2
                                           15    10018       4
                                           16    10023       4
                                           17    10024      22
                                           18    10025       2
                                           19    10026       3
                                           20    10027       0
                                           21    10028      22
                                           22    10029      22
                                           23    10030       3
                                           24    10033       3
                                           25    10034       5
                                           26    10035       3
                                           27    10036      23
                                           28    10037       4
                                           29    10038       1
                                           30    10040      23
                                           31    10041      23
                                           32    10042      23
                                           33    10044       4
                                            .       .        .
                                            .       .        .
                                            .       .        .

 

What I want to do is to draw the frequency or count percentage over the variable "TroughHour".

When we want to draw this, SGPLOT is a very poweful tool, so I use it to do so:

PROC SGPLOT DATA=Have;
	HISTOGRAM TroughHour;
RUN;

The result goes like this:

 

SGPlot1.png

 

Though it does show the percentage, the presentation isn't what I expected exactly.

#1 The range isn't right: I only want to show TroughHour= 22, 23, 0, 1, 2, 3, 4, and 5, but it shows from 0 to 23.

#2 The order isn't correct either: I want it to be presented in the order of 22, 23, 0, 1, 2, 3, 4, 5, but now its order is 0, 1, ..., 23

 

While searching for some useful information by myself, hope you guys can kindly give me a hand.

Thanks in advance!


Accepted Solutions
Solution
‎06-21-2017 04:03 AM
Super User
Posts: 17,960

Re: Change the x-axis order in SGPLOT

1. This is one of the few cases where Radar Plots are useful, showing different levels over time. 

2. The fastest solution, recode data and use formats to display what you want using a VBAR plot rather than a histogram plot. Though if you recode it probably won't matter which one you use. But since you're binning by the hour it likely doesn't matter anyways. 

3. If you are intersted in looking at your full 24 hour period, I recommend a radar chart. The 24 hour clock works well on that type of graph.

 

proc format;
 value time_fmt
 22 = 1
 23 = 2
 0 = 3
 1 = 4
 2 = 5
 3 = 6
 4 = 7
 5 = 8
 6 = 9
 ;
 
 value $ time2_fmt
 '1' = 22
 '2' = 23
 '3' = 0
'4' = 1
'5' = 2
'6' = 3
'7' = 4
'8' = 5
'9' = 6;
 run;
 
 proc sgplot data=have;
 where hour in (22, 23, 0:5);
 vbar hour2 / stat=freq;
 format hour2 $time2_fmt.;
 run;
 

View solution in original post


All Replies
PROC Star
Posts: 1,567

Re: Change the x-axis order in SGPLOT

Like this?


proc sgplot data=HAVE;
  vbar HOUR;
  xaxis values=(22,23,0,1,2,3,4,5) ;
  format HOUR z2.;
run;

aaa1.PNG

 

PROC Star
Posts: 1,567

Re: Change the x-axis order in SGPLOT

Oh you want percentages.

If you want no intermediate table (but why not?) , how about this?

 


axis1  order=(22,23,0,1,2,3,4,5) ;
proc gchart;
	vbar HOUR/ maxis=axis1 percent discrete;
  format HOUR z2.;
run;
quit;

aaa1.PNG

 

Contributor
Posts: 37

Re: Change the x-axis order in SGPLOT

Thanks Chris, 

 

Though I adapt other method, knowing how to put percentage on the bar and change the order are very helpful!

 

Solution
‎06-21-2017 04:03 AM
Super User
Posts: 17,960

Re: Change the x-axis order in SGPLOT

1. This is one of the few cases where Radar Plots are useful, showing different levels over time. 

2. The fastest solution, recode data and use formats to display what you want using a VBAR plot rather than a histogram plot. Though if you recode it probably won't matter which one you use. But since you're binning by the hour it likely doesn't matter anyways. 

3. If you are intersted in looking at your full 24 hour period, I recommend a radar chart. The 24 hour clock works well on that type of graph.

 

proc format;
 value time_fmt
 22 = 1
 23 = 2
 0 = 3
 1 = 4
 2 = 5
 3 = 6
 4 = 7
 5 = 8
 6 = 9
 ;
 
 value $ time2_fmt
 '1' = 22
 '2' = 23
 '3' = 0
'4' = 1
'5' = 2
'6' = 3
'7' = 4
'8' = 5
'9' = 6;
 run;
 
 proc sgplot data=have;
 where hour in (22, 23, 0:5);
 vbar hour2 / stat=freq;
 format hour2 $time2_fmt.;
 run;
 
Contributor
Posts: 37

Re: Change the x-axis order in SGPLOT

Thanks Reeza, 

 

I agree that this is the fastest solution!

 

 

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 5 replies
  • 136 views
  • 3 likes
  • 3 in conversation