Announcement

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

  • sorting in graph dot

    I want to create dot graph where for each variable i see two different means. Below is sample how i managed to recreate tempvars just for purpose of making graph dot. However, now i have a problem of sorting these variables, i.e. sort SD and gsort SD both sort my tempvar, but when i create graph with it, it still shows as if it is not sorted. Also, sort option in graph dot is not making it sorted. What might be a problem?

    Also i am struggling to find right option in order to change legend labels from "mean of _var1 / mean of _var2" to something different and to make them one one above the other, rather than one next to the other horizontally. Thanks for any suggestions

    Code:
    tempvar graphcat SD SDw
    gen `graphcat'=""
    gen `SD' =.
    gen `SDw' =.
    local graphvars var1 var2 var3 var4 var5
    local i = 1
    foreach gv of local graphvars{
    replace `graphcat' = "`gv'" in `i'/`i'
    local i=`i'+1
    }
    foreach gv of local graphvars{
    replace `SD' = abs_`gv'_STD if `graphcat' == "`gv'"
    replace `SDw' = abs_`gv'_STDw if `graphcat' == "`gv'"
    }
    gsort -`SD'
    graph dot `SD' `SDw', over(`graphcat', sort( `SD') descending) yline(10)
    drop `graphcat' `SD' `SDw'

  • #2
    Needs a self-contained data example, I suggest. http://www.statalist.org/forums/help#stata 12.2

    Comment


    • #3
      Thanks for reply. Here is small example of what i am doing. First i created set of scalars and then tempvars filled in with these scalars. What i dont understand is that graph has its own order of categories on y-axis and i dont understand why order of cathegories is not changing with sorting or with sort option in graph dot but at the same time when i list those vars i see that they are sorted..

      Code:
      sysuse auto
      
      local graphvars mpg rep78 headroom gear_ratio
      local i = 1
      foreach gv of local graphvars{
          qui sum `gv'
          scalar abs_`gv'_STD = r(mean)
          scalar abs_`gv'_STDw = r(mean)*r(sd)/10
          di abs_`gv'_STD
          di abs_`gv'_STDw
      }
      
      tempvar graphcat SD SDw
      gen `graphcat'=""
      gen `SD' =.
      gen `SDw' =.
      local i = 1
      foreach gv of local graphvars{
      replace `graphcat' = "`gv'" in `i'/`i'
      local i=`i'+1
      }
      
      foreach gv of local graphvars{
      replace `SD' = abs_`gv'_STD if `graphcat' == "`gv'"
      replace `SDw' = abs_`gv'_STDw if `graphcat' == "`gv'"
      }
      list `graphcat'  in 1/5
      list `SD'  in 1/5
      list `SDw'  in 1/5
      gsort -`SD'
      list `graphcat'  in 1/5
      list `SD'  in 1/5
      list `SDw'  in 1/5
      graph dot `SD' `SDw', over(`graphcat', sort( `SD') descending) yline(10)
      drop `graphcat' `SD' `SDw'
      Last edited by Evo Kurtz; 12 Jul 2016, 02:24.

      Comment


      • #4
        Thanks for the extra detail. I suggest that something like this will be easier to adopt and to adapt.


        Code:
        sysuse auto, clear
        
        local call 
        
        foreach v in mpg rep78 headroom gear_ratio { 
            local call `call' (mean) mean`v' = `v' (sd) sd`v'=`v' 
        }
        
        collapse `call' 
        
        list 
        
             +-----------------------------------------------------------------------------------+
             | meanmpg    sdmpg   meanr~78   sdrep78   meanhe~m   sdhead~m   meange~o   sdgear~o |
             |-----------------------------------------------------------------------------------|
          1. | 21.2973   5.7855     3.4058   .989932        3.0        0.8       3.01       0.46 |
             +-----------------------------------------------------------------------------------+
        
        
        gen id = 1 
        reshape long mean sd, i(id) j(which) string 
        
        list 
        
             +-------------------------------------+
             | id        which      mean        sd |
             |-------------------------------------|
          1. |  1   gear_ratio   3.01486   .456287 |
          2. |  1     headroom   2.99324   .845995 |
          3. |  1          mpg   21.2973    5.7855 |
          4. |  1        rep78    3.4058   .989932 |
             +-------------------------------------+
        
        
        graph dot (asis) mean sd, over(which, sort(1))
        I don't recognise r(mean)*r(sd)/10 as anyone's standard descriptive statistic!

        Comment


        • #5
          Cross-posted at http://stackoverflow.com/questions/3...h-dot-in-stata

          Our policy is quite explicit in the FAQ Advice you were asked to read before posting; http://www.statalist.org/forums/help#crossposting

          8. May I cross-post to other forums?

          People posting on Statalist may also post the same question on other listservers or in web forums. There is absolutely no rule against doing that.
          But if you do post elsewhere, we ask that you provide cross-references in URL form to searchable archives. That way, people interested in your question can quickly check what has been said elsewhere and avoid posting similar comments. Being open about cross-posting saves everyone time.
          If your question was answered well elsewhere, please post a cross-reference to that answer on Statalist.

          Comment


          • #6
            Also, without getting into the soundness of the code in #3, for the second to last line

            graph dot `SD' `SDw', over(`graphcat', sort( `SD') descending) yline(10)
            you can just specify

            Code:
            graph dot `SD' `SDw', over(`graphcat', sort(1) descending) yline(10)
            graph dot `SD' `SDw', over(`graphcat', sort(2) descending) yline(10)
            to sort in terms of `SD' and `SDw', respectively. To change legend labels from "mean of _var1 / mean of _var2" from #1, add

            Code:
            yvaroptions( relabel(1 "my label 1" 2 "my label 2" ))

            Comment


            • #7
              Thank you both for the answers! Sorry for cross posting, i will keep it in mind for the future ( r(mean)*r(sd)/10 i putted just as an example because i get scalars from separate longer procedure that i was thinking isnt necessary to put in here, but is kind of similar in values to it)
              Last edited by Evo Kurtz; 12 Jul 2016, 09:24.

              Comment

              Working...
              X