Announcement

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

  • Changing the sorting order when using by() in a bar graph

    Dear all,

    Maybe this is an extremely simple question, but I haven't been able to solve the following problem.

    I am comparing two variables "data" and "fitted" in a bar graph using the following command:

    graph bar (mean) data fitted, over(quintile) by(ccode)

    which produces the attached graph.

    As you can see, bar plots are sorted alphabetically by ccode: ALB, ARG, ARM and BGR.

    What I would like to do, however, is to have bar graphs for each country, but sorting them by the value of a different variable, call it x. As you can see in the graph, the value of x for each country is 4 for ALB, .18 for ARG, .53 for ARM and 1.5 for BGR.

    Thus, I would like to produce the same graph as above but sorted by x, so the order would look like: ALB, BGR, ARM and ARG.

    Thanks for your help.

    Alejandro

  • #2
    You need a new grouping variable with the order of x that looks like ccode.

    Code:
    egen ccode2 = group(x ccode)
    labmask ccode2, values(ccode)
    where labmask is from the Stata Journal files. search labmask, sj and then download.

    Then use this new variable in your graphs.

    Note: if there are no ties on x, then group(x) will be sufficient. The point of group(x ccode) is to break any ties.

    P.S. Post some data and I will suggest what I think is a much better graph.
    Last edited by Nick Cox; 24 Jun 2015, 05:16.

    Comment


    • #3
      Dear Nick,

      Thank you very much for your prompt answer. labmask did the trick (there were indeed some ties between countries with the x variable). I'm attaching the data that I used to create the graph in the original post -- I'm curious about your suggestion to improve the graph.

      Alejandro
      Attached Files

      Comment


      • #4
        Thanks; I am away from Stata at present but will post later today.

        Comment


        • #5
          I have two suggestions, using graph dot not graph bar, and using some kind of line plot.

          Here's a script for both:

          Code:
          clear
           
          input id str3 ccode  data  fitted  quintile  x
            1.    ALB   .087379   .069976          3         4
            2.    ALB   .019418   .126536          4         4
            3.    ALB   .504854        .5          5         4
            4.    ALB   .126214   .057926          2         4
            5.    ALB   .262136   .074655          1         4
            6.    ARG   .532623   .646481          1   .176471
            7.    ARG   .065246   .094678          4   .176471
            8.    ARG   .067909   .072686          5   .176471
            9.    ARG   .231691   .232221          2   .176471
           10.    ARG    .10253   .140176          3   .176471
           11.    ARM   .292857   .163006          5   .538462
           12.    ARM   .264286   .223139          2   .538462
           13.    ARM   .242857   .374735          1   .538462
           14.    ARM   .085714   .154371          4   .538462
           15.    ARM   .114286   .177631          3   .538462
           16.    BGR   .134703   .173823          4       1.5
           17.    BGR   .123288   .140958          3       1.5
           18.    BGR   .390411   .309983          5       1.5
           19.    BGR    .20548   .177051          1       1.5
           20.    BGR   .146119   .135629          2       1.5
          end
          
          egen ccode2 = group(x ccode)
          labmask ccode2, values(ccode)
          
          graph dot (asis) data fitted, over(quintile) over(ccode2) ///
          marker(1, ms(Oh)) marker(2, ms(+)) linetype(line) line(lcolor(gs12) lw(vvthin))
          
          gen wherex = 4
          gen wherey = .6
          gen x2 = string(x, "%3.2f")
          
          twoway connected data fitted quintile, by(ccode2, row(1) note("")) sort ms(Oh +) ///
           || scatter wherey wherex, ms(none) mla(x2) mlabpos(0) mlabsize(*1.5) legend(order(1 2)) ///
          yla(, ang(h)) ytitle(data and fitted) xtitle(quintile)
          Here are the graphs:

          Click image for larger version

Name:	june24_dot.png
Views:	2
Size:	9.5 KB
ID:	1299783

          Click image for larger version

Name:	june24_line.png
Views:	1
Size:	15.5 KB
ID:	1299784



          Some would feel queasy at connecting points for quintiles, but I feel comfortable about it.

          Naturally, you have more countries, but there's scope for showing more too.
          Attached Files
          Last edited by Nick Cox; 24 Jun 2015, 10:46.

          Comment

          Working...
          X