Search code examples
sas

SGPLOT Two Y Axes


I have a dataset that looks like the following:

  pt_fin    Admit_Type    MONTH_YEAR   BED_ORDERED_TO_DISPO (minutes)
     1        Acute             Jan              214
     2        Acute             Jan              628
     3        ICU               Jan              300
     4        ICU               Feb               99

I already have a code (see below) that produces a plot with a x (admit type grouped my month) and y axes (median bed to dispo time), but I want to add a secondary Y axes which counts the number of patients which were used to compute each respective median.

For example, I want a secondary Y axis data point that corresponds to the month and admit type, so for Jan, the secondary Y axis data point will have a 2 separate counts 1)of the patients admitted to acute and 2) of the patients admitted to ICU.

 proc sgplot data=Combined;
 title "Median Bed Order To Dispo By Month, Admit Location"; 
 vbar MONTH_YEAR   / response=BED_ORDERED_TO_DISPO stat=median
 group = Admit_Type groupdisplay=cluster ;
 run; 

I've been trying to adapt what I've found here but the plots my code produces are super messy and incorrect.

https://blogs.sas.com/content/iml/2019/01/14/align-y-y2-axes-sgplot.html

Desired output(pretend X's and *'s, respectively, are connected in a line graph corresponding to the Y axis):

     |        *                      |
  m  |   |    |              X   |   | #
  e  |   x    |              |   *   |
  d  |   |    |              |   |   |
     |-------------------------------|
      Acute ICU           Acute ICU  
        Jan                FEb

Code which I've tried that produce rubbish

proc sgplot data=Combined;
vbarbasic MONTH_YEAR/ response=Bed_Order_Hour y2axis; /*needs to be on y axis 1*/
group = Admit_Type
series x=MONTH_YEAR y=Pt_fin/ markers; *Pt_fin needs to be on y axis 2*/
run;

Solution

  • Your visualization explanation is weak. You might want to use two plotting statements in your SGPLOT, VBAR and VLINE.

    data have;
      do type = 'Acute', 'ICU';
    
        do month = '01jan2018'd to '31dec2018'd;
    
          do _n_ = 1 to floor (50 * ranuni(123));
    
            patid + 1;
            minutes = 10 + floor(1000 * ranuni(123));
    
            output;
          end;
    
          month = intnx ('month', month, 0, 'e');
        end;
      end;
    
      format month monname3.;
    run;
    
    ods html5 file="plot.html" path="c:\temp";
    
    proc sgplot data=have;
      title "Median of patient minutes by month";
      vbar  month / group=type groupdisplay=cluster response=minutes stat=median;
      vline month / group=type groupdisplay=cluster response=minutes stat=freq y2axis ;
    run;
    
    ods html5 close;
    

    enter image description here

    The vline presents the viewer a secondary focus on the frequency for each median. The same information (as an aspect) of the median could be communicated instead with just a modification of the vbar intensity. The highest freq bars (of median) would be 'strongest' shade and the lower 'freq' bars would be faded.