Announcement

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

  • Sorting box plots

    Hi All,

    I would like to sort a set of box plots by interquartile range or some other measure corresponding to dispersion. I am currently using simple code below:

    Code:
    graph box var1- var10, asyvars showyvars yvaroptions(label(angle(ninety) labsize(vsmall))) legend(off)
    Kind regards,
    Konrad
    Version: Stata/IC 13.1

  • #2
    The option that sorts the box in the order you want is a option of over (sort(varname). while yvaroptions support some over() options, not apparently this one.

    The problem is that your data appears to be wide as opposed to long which makes the calculation of a variable reflecting the variation difficult as it would be the same for each of the observations.

    If your doing a small and quick job, perhaps just hardcode the varlist in the order of the measure, otherwise for a larger job i would convert to long (reshape) and use the over option.

    Sorry it is not the most useful solution

    Comment


    • #3
      Originally posted by Robert Wells View Post

      If your doing a small and quick job, perhaps just hardcode the varlist in the order of the measure, otherwise for a larger job i would convert to long (reshape) and use the over option.
      Robert,

      thank you for showing the interest, this is what I did in this case.
      Kind regards,
      Konrad
      Version: Stata/IC 13.1

      Comment


      • #4
        There is absolutely no need to reshape as you can always automate the ordering of the variables. Here I use foreach as something more generalisable than would be a forval over 1 to 10 exploiting the structure of your variable names.

        Code:
         
        gen name = ""
        gen iqr = . 
        local i = 0 
        qui foreach v of var var1-var10 { 
            local ++i  
            su `v', detail 
            replace name = "`v'" in `i' 
            replace iqr = r(p75) - r(p25) in `i' 
        } 
        
        sort iqr  
        
        forval j = 1/`i' { 
            local names `names' `=name[`i']' 
        } 
        
        graph box `names'
        The assumption is that you have fewer variables for which you want box plots than observations. At worst, set obs fixes the last.

        Comment


        • #5
          Nick, this is excellent. Thank you very much.
          Kind regards,
          Konrad
          Version: Stata/IC 13.1

          Comment


          • #6
            I have the same need and fortunately come across this thread. Nick's solution is fine, however, in the last line but two, the code should be typed as `=name[`j']'.

            Originally posted by Nick Cox View Post
            There is absolutely no need to reshape as you can always automate the ordering of the variables. Here I use foreach as something more generalisable than would be a forval over 1 to 10 exploiting the structure of your variable names.

            Code:
            gen name = ""
            gen iqr = .
            local i = 0
            qui foreach v of var var1-var10 {
            local ++i
            su `v', detail
            replace name = "`v'" in `i'
            replace iqr = r(p75) - r(p25) in `i'
            }
            
            sort iqr
            
            forval j = 1/`i' {
            local names `names' `=name[`i']'
            }
            
            graph box `names'
            The assumption is that you have fewer variables for which you want box plots than observations. At worst, set obs fixes the last.

            Comment


            • #7
              Correct. Thanks for the fix.

              Comment

              Working...
              X