Announcement

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

  • Help needed on placing bars on twoway line graph

    Hello,

    I'm struggling with a following problem:

    Here is 4 first rows of my data:

    year b lower upper maxy
    1979 -.0003847 -.0010128 .0002433 .0031357
    1980 -.0004005 -.0010922 .0002913 .0031357
    1981 -.0004458 -.0011484 .0002569 .0031357
    1982 -.000402 -.0011498 .0003458 .0031357

    I have estimated yearly data on b, and lower and upper are the lower and upper values of 95 % confidence intervals of b. Variable mavy is the maximum value of upper in whole dataset, i.e. egen maxy=max(upper).

    I would like to draw a figure, where b is plotted with its confidence intervals, and addition to that, I would like to add in the same graph bars denoting those years, when the coefficient b is statistically significant at 5% significance level. This is my code so far:

    . twoway (bar maxy year if lower>=0) (line b year) (rline lower upper year) , title(65f)

    And this is the graph that I get:


    Click image for larger version

Name:	Graph.png
Views:	1
Size:	28.5 KB
ID:	1379949


    I wonder why do the bars "start" at 0? First I thought that it is because I have in the code "if lower>=0", but I also tried following: I generated a new variable signif, which gets value 1 if lower>=0 and 0 otherwise, i.e.

    . gen signif=.
    . replace signif=1 if lower>=0


    and tried following code:

    . twoway (bar upper year if signif==1) (line b year) (rline lower upper year) , title(65f)

    But the graph was the same.

    Could someone, please, help me on this one? How could I get the bars "starting" from the x-axis?

    With best regards,
    Sanna Huikari

  • #2
    The bars start at 0 because that is the default. (What other default would you expect for bars?). See documentation of the base() option in

    Code:
    help twoway bar
    The x axis is, naturally, the line y = 0. That is the standard mathematical definition, and your bars already start there.

    If you want your bars to start somewhere else, specify the base() option.

    Also check out

    Code:
    help twoway rbar

    Comment


    • #3
      Thank you very much for your helpful answer, Mr. Cox. And apologies for my not so fluent English and for somewhat misleading question. Instead of x-axis I should have used the vertical line in the bottom of the figure.

      base()option works beautifully. I have quite a lot of similar figures to be drawn and the base value varies in different age-cohorts and between genders (for example, the graph that I posted was for over 65 years old female). I am wondering if there is the way to define the base value so that it would every time be the value that would result the bars starting from the bottom of the figure, or do I need to define the base value for every figure separately.

      With best regards,
      Sanna Huikari

      Comment


      • #4
        Couldn't you put axes on both sides of the graph? That would allow for the line graph to be negative and also let the bars start at zero at the bottom.

        http://www.stata.com/manuals13/g-3ax...ce_options.pdf

        Comment


        • #5
          I am a bit puzzled why it makes sense to stop the bars at the empirical maximum but extend them all the way to the bottom of the graph.

          Otherwise this is morphing towards (or I would like to think it is morphing towards) a problem discussed at some length in http://www.stata-journal.com/article...article=gr0067

          That's a good paper but at this writing requires a subscription to the Stata Journal to access. If you don't have access you can purchase a copy; no money will come back to me.

          Short of that, let's show you a recipe and you may be able to modify it. Suppose you want bars underneath some data and the recipe is going to be that it extends from a nice number <= the response minimum to another nice number >= the response maximum. In essence you look at the data to see what will work and then also control axis labels.

          You can copy and paste this code into a do-file editor window and then run it.

          Code:
          ysuse uslifeexp, clear 
          
          gen min = 82
          gen max = 28 
          
          foreach v of var le* { 
                twoway rarea min max year if inrange(year, 1939, 1945), color(red*0.2) || ///
                line `v' year, name(`v', replace) legend(off) yla(30(10)80) ///
                ytitle("`: var label `v''") 
          }
          I chose an example where the same max and min make sense. If your different graphs need different ranges, you are going to need more complicated code.

          See also

          Code:
          . search nicenum, historical
          
          
          STB-25  dm28  . . .  Calculate nice numbers for labeling or drawing grid lines
                  (help nicenum if installed) . . . . . . . . . . . . . . . J. W. Hardin
                  5/95    pp.2--3; STB Reprints Vol 5, pp.19--20

          Comment


          • #6
            Thank you again very much for you answer, and using your time for helping me.

            The bars should not necessarily stop to the empirical maximum, that was just the way I managed to get the each bar to reach at the same level. Actually, I would like the bars to reach to the top of the figure as well, but I do not know how to do that otherwise than defining the value, e.g. 0.004 in above picture.

            I think that the article that you referred will help me on this matter for a great deal, because I want to highlight those years for which the coefficient is statistically significant at 5 % significance level. I have an acces on Stata Journal from my office computer, so I will read your article on Monday, when I am back at office.

            With best regards,
            Sanna Huikari

            Comment


            • #7
              OK. Note that the first line of code in #5 should be

              Code:
               
               sysuse uslifeexp, clear

              Comment


              • #8
                Dear Mr. Cox. With the help of your article, I managed to create exactly the kinds of graphs that were my objectives. Thank you once again for your help!

                With best regards,
                Sanna Huikari

                Comment


                • #9
                  The form "Nick" is fine by me.

                  Comment

                  Working...
                  X