Announcement

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

  • changing the order of x axis

    I have the following table:

    ctry start end ave
    JPN 639 229 197
    DEN 59 63 35
    ave 57 1 14
    US 56 1 13
    ESP 16 10 20
    PH 2 1 2
    CHN 1 0 5
    MYS 1 1 1



    When I graph this, I would like JPN to be the first to appear on the left in the x axis, then DEN, etc… I'm really struggling with this, could anyone help me?

  • #2
    At a guess, you want

    Code:
    gen work = -ave
    egen xaxis = group(work ctry)
    labmask xaxis, values(ctry)
    where labmask is from the Stata Journal.

    Here the negation ensures the right sort order, the country variable breaks ties and labmask assigns value labels as desired.

    See also http://www.stata-journal.com/article...article=gr0034

    Comment


    • #3
      Because ctry is a string I had to encode it to ctryx. Then I applied the answer you gave me in another post on "how to overlap bar and line in one graph" by doing this:

      . gen ctryx1 = ctryx - 0.15
      . gen ctryx2 = ctryx + 0.15
      . twoway bar start ctryx1, barw(0.3) || bar end ctryx2, barw(0.3)|| line ave ctryx

      I then followed your advise above, too (ctryx, instead of ctry), and I would now want to create a two-way graph as before, but I would hope that the country with the highest value (JPN) would appear first in the X axis. I am still not able to do this, despite labmask.

      Thanks so much for your help.

      Comment


      • #4
        Please supply a reproducible example. Otherwise I really can't see what the problem is.

        http://www.statalist.org/forums/foru...-for-statalist

        Comment


        • #5
          ctry prod start end ave
          CHN 6.40E+06 1 0 5
          DEN 6.40E+06 59 63 35
          ESP 6.40E+06 16 10 20
          JPN 6.40E+06 639 229 197
          MYS 6.40E+06 1 1 1
          PH 6.40E+06 2 1 2
          US 6.40E+06 56 1 13
          ave 6.40E+06 57 1 14
          Sorry for being unclear.

          I am trying to graph the data above. And I have followed your instructions regarding using "offset" so that I could use two-way bar to plot start and end , over ctry and then superimpose a scatter plot of ave, also over ctry. That worked very well. I have also solved my problem of ordering the x axis, by simply changing the value labels so that 1 will be JPN, 2 will be DEN etc. My problem now is that the bar+line graph has an extra 'tick': it now has 9 while I should only have 8. I think the 9th came from the
          . gen ctryx1 = ctryx - 0.15
          . gen ctryx2 = ctryx + 0.15 command?

          How do I get rid of that 9th (nonexistent in my data, but showing in the graph) label?

          Comment


          • #6
            Sorry, but this is not a reproducible example. That means complete code to create data and complete code to run the graph. Did you run the thread referred on using dataex, for example? There is here no ctryx variable, for example.

            Comment


            • #7


              this is how it looks like now.

              Comment


              • #8

                Comment


                • #9
                  This is not working very well. I ask for data and code and you show me a picture. Nevertheless, at a guess you need

                  Code:
                   
                  xla(1/9, valuelabel)
                  together with the rest of your graph command.

                  Comment


                  • #10
                    Code:
                    input str6 ctry start end ave
                    "CHN"  1 0 5 
                    "DEN"  59 63 35
                    "ESP"  16 10 20
                    "JPN" 639 229 197
                    "MYS"  1 1 1
                    "PH"  2 1 2
                    "US" 56 1 13
                    "ALL"  57 0.5 14
                    end gen ctrys1 = ctrys - 0.15
                      gen ctrys2 = ctrys + 0.15  
                      twoway bar start ctrys1, barw(0.3) || bar end ctrys2, barw(0.3)|| scatter ave ctrys 



                    Somehow, the problem of the 9th observation has disappeared. I really don't know why. The only problem I have now is that I would want the country with the highest value for 'start' to appear first in the X axis, and so on. I managed to do it in a roundabout way by changing the order of the labels when I destringed 'ctry', so that JPN, for instance, is assigned 1, etc. But it is a clumsy and also unstable (one time it works, another it doesn't!) way of doing it, and I'm sure there is a much better way of doing this.

                    Thank you so much for your help!

                    Comment


                    • #11
                      The code you provided is incomplete and therefore still not reproducible.
                      Code:
                      . gen ctrys1 = ctrys - 0.15
                      ctrys not found
                      r(111);

                      Comment


                      • #12
                        Sorry again! Here it is


                        . encode ctry, generate(ctrys)

                        Comment


                        • #13
                          Consider this, which requires labmask (SJ)

                          Code:
                          clear
                          input str6 ctry start end ave
                          "CHN"  1 0 5
                          "DEN"  59 63 35
                          "ESP"  16 10 20
                          "JPN" 639 229 197
                          "MYS"  1 1 1
                          "PH"  2 1 2
                          "US" 56 1 13
                          "ALL"  57 0.5 14
                          end
                          gen work = -ave
                          egen xaxis = group(work ctry)
                          labmask xaxis, val(ctry)
                          
                          gen x1 = xa - 0.15
                          gen x2 = xa + 0.15
                          
                          scatter ave xaxis, ms(none) ///
                          || bar start x1, barw(0.3)  ///
                          || bar end x2, barw(0.3)||  ///
                          scatter ave xaxis, xla(1/8, val noticks) ytitle(whatever) yla(, ang(h))  ///  
                           
                          legend(off) xtitle("")
                          Click image for larger version

Name:	annette.png
Views:	1
Size:	11.0 KB
ID:	1301773

                          Comment


                          • #14
                            Thank you so much for your help! It's very much appreciated!!

                            Comment


                            • #15
                              I have been trying to lessen the gaps between the bars between the countries by adding bargap option but I keep getting errors.

                              Comment

                              Working...
                              X