Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Sort in ascending order in twoway

    Hi

    I have a seemingly easy question. I am examining the rate of a disease by hospital areas. I want to plot the percent of the population with the disease with CI 95 % by hospital. The problem is that I want to sort my graph from the lowest to the highest levels of the disease and I can't figure out how to do this. Hopefully some of you may have input on this.
    Code:
    * CI 95 %
    gen se = (((disease_prop)*(1-disease_prop)/n)^0.5
    gen lb = (disease_prop-1.96*se)*100
    gen ub = (disease_prop+1.96*se)*100
    
    twoway (rspike lb ub hospital, horizontal xline(1.6)) (scatter hospital disease_percent)
    I obtain the following figure which is ok, but I want it sorted from low to high. Moreover, I am working on a secure server so graphs from -ssc- won't help me in this instance.


    Click image for larger version

Name:	twoway_plot.png
Views:	1
Size:	99.5 KB
ID:	1604385


  • #2
    There was, of course, no possibility to present the example code WITH example data to run and free of syntax errors.

    Try something along these lines:

    Code:
    clear
    sysuse auto
    generate hospital=_n
    generate disease_prop=l/w*8
    generate disease_percent=disease_prop*100
    rename rep78 n
    replace n=1 if missing(n) 
     
    * CI 95 %
    gen se = (((disease_prop)*(1-disease_prop)/n)^0.5)
    gen lb = (disease_prop-1.96*se)*100
    gen ub = (disease_prop+1.96*se)*100
    
    centile disease_percent
    return list
    local m=r(centiles)
    
    sort disease_prop
    generate h2=_n
    label variable h2 "Hospitals"
    
    twoway (rspike lb ub h2, horizontal xline(`m')) (scatter h2 disease_percent) , scale(0.5) ylabel(none) legend( order(1))

    Click image for larger version

Name:	hospitals.png
Views:	1
Size:	25.8 KB
ID:	1604391

    Comment


    • #3
      myaxis from SSC would be relevant if you had access. Just mentioning that for anyone else.

      Otherwise sorting estimates into order is easy enough; the trick is to get the hospital codes copied over (but do you need them any way?).

      Code:
      sort disease_percent
      gen order = _n
      
      forval i = 1/`=_N' {
           local label = hospital[`i']
           label def order `i' "`label'", add
      }
      
      label val order order  
      
      twoway rspike lb ub order, horizontal xline(1.6) || scatter order disease_percent, yla(1/63, valuelabel ang(h))
      Last edited by Nick Cox; 19 Apr 2021, 05:58.

      Comment

      Working...
      X