Announcement

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

  • Sorting box plots

    Is there a way I could quickly sort box plots by interquartile range or some other measure reflecting dispersion? For presentational purposes, I have to generate a set of box plots that are sorted in a manner corresponding to dispersion. There are no sub-categories, I am using the simple code below:
    Code:
    graph box var1- var2, asyvars showyvars yvaroptions(label(angle(ninety) labsize(vsmall))) legend(off)
    Kind regards,
    Konrad
    Version: Stata/IC 13.1

  • #2
    If you are showing different variables, the quickest way is just to do something like

    Code:
     
    tabstat price-foreign, s(iqr) c(s)
    and look at the results.

    Comment


    • #3
      Nick, I was looking at tabstat results but I have over 50 indicators to graph. Is there a way to do it more automatically? In effect, sorted values in the matrix saved by tabstat correspond to my desired order variables. I just do not know how to impose one on another.
      Kind regards,
      Konrad
      Version: Stata/IC 13.1

      Comment


      • #4
        You'd have to write a program, I think. I'd reshape the data to long, work out group iqr with egen and reshape back to wide.

        Comment


        • #5
          Nick, that would be neat solution. In the meanwhile, I primitively exported the tabstat results to Excel, sorted by value, joined lines and exported sorted variable list to Stata.
          Kind regards,
          Konrad
          Version: Stata/IC 13.1

          Comment


          • #6
            Nick, that would be neat solution. In the meanwhile, I primitively exported the tabstat results to Excel, sorted by value, joined lines and exported sorted variable list to Stata, which I passed to graph box command.
            Kind regards,
            Konrad
            Version: Stata/IC 13.1

            Comment


            • #7
              Thinking about it showed a quick-and-dirty fix

              Code:
              sysuse auto, clear
              
              graph box price-foreign, name(g1)  
              
              gen name = "" 
              gen iqr = . 
              
              local J = 0 
              
              quietly foreach v of var price-foreign { 
                   local ++J 
                   su `v', detail 
                   replace iqr = r(p75) - r(p25) in `J' 
                   replace name = "`v'" in `J' 
              } 
              
              sort iqr 
              
              forval j = 1/`J' { 
                  local names `names' `=name[`j']' 
              } 
              
              graph box `names', name(g2)
              This assumes that number of observations >= number of variables you want to show. If not, there is a cleaner Mata-based way of doing this.

              Comment


              • #8
                Thank you very much for posting the code. As a matter of fact, I have 30 something observations and 50 something variables.
                Kind regards,
                Konrad
                Version: Stata/IC 13.1

                Comment


                • #9
                  You can fudge this by

                  Code:
                  set obs 60
                  Last edited by Nick Cox; 09 May 2014, 11:36.

                  Comment


                  • #10
                    Thanks, I'll fudge it accordingly.
                    Kind regards,
                    Konrad
                    Version: Stata/IC 13.1

                    Comment

                    Working...
                    X