Announcement

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

  • Question About X-Axis Title Using Graph Bar

    I have data similar to the sp500 dataset that comes with Stata. Using the sp500 dataset as an example, I'd like to make a graph of the opening price over time, and label the x-axis "Date." I can't figure out how to do this! See code below.

    sysuse sp500, clear
    keep if date < date("1/15/2001", "MDY")
    graph bar open, over(date) ytitle("Opening Price")

    Any advice you can provide would be so helpful.

    Best,
    Erika

  • #2
    For some reason, xtitle() is not allowed in graph bar and some other non-twoway graph types. Use b1title() instead:
    Code:
    graph bar open, over(date) ytitle("Opening Price") b1title("Date")

    Comment


    • #3
      graph bar and its siblings don't use the terminology of an a x axis. From the help for graph bar you can click your way to finding that something like

      Code:
       
      b1title(Date)
      will add a title at the bottom.

      But if your dates are clearly shown then I can't see that a title is needed for the time axis. I have my work cut out with students who think that a time series with times like 1950(10)2010 needs to be explained with "year" or "date", perhaps because some school teacher told them that they should always label their axes. Good advice, but not advice that should overrule good sense.

      More crucially, unless you want to show only about 10, 20, 30 prices graph bar is a poor choice here, as it is predisposed to think that each value of your date variable is a category and to supply a text label. That is a recipe for a mess unless the dataset is trivially small. I'd use twoway bar or (more likely) line.

      Comment


      • #4
        Nick Cox Svend Juul I have the same problem (hbar with over option and I want to change the x-axis title). When I use the bititle option, the title appear in the chart but the original x-axis title still there as well. In other words, there are two x-axis title in my chart.

        Code:
        graph hbar (mean) elapse if index <11, over(index) ///
        title("Average days elapse between incidents") b1title("Days")
        This code produces a chart with two titles "mean of elapse" and below "Days". I just need "Days".

        Any ideas? Thank you in advance

        Comment


        • #5
          No data example that we can use, but consider this reproducible example:

          Code:
          . sysuse auto, clear
          (1978 Automobile Data)
          
          . graph hbar (mean) mpg , over(foreign) b1title(stuff) ytitle("")
          Otherwise put, it is the y axis title you want to suppress, not the x axis title. Stata's usage here is not standard, but has a rationale. See the help for graph bar, very early on:

          graph hbar draws horizontal bar charts. In a horizontal bar chart, the numerical axis
          is still called the y axis, and the categorical axis is still called the x axis, but y
          is presented horizontally, and x vertically.



          Comment


          • #6
            Thank you so much for the quick response Nick Cox ! I just did:

            Code:
             graph hbar (mean) elapse if index <11, over(index)
            /// title("Average days elapse between incidents") ytitle("Days")

            Comment


            • #7
              Thank you, Nick Cox!

              Comment

              Working...
              X