Announcement

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

  • horizontal bar graph x-axis and xlabel, and the code in the command window when editing in the graph editor

    Dear Stata users,

    The bar graph attached shows the proportion of variables var1 to var5 taking the value equal 1.
    I would like to change the labels to x-axis in percent with the command "xlabel" instead of changing it from the graph editor.
    The labels to x-axis should be like "0% 10% 20% .... 50%" instead of the currently displayed labels "0 .1 .2 .... .5"

    When I run the following code:

    graph hbar var1 var2 var3 var4 var5 if `tagb', ///
    title("Requests in %", size(medium) span) ///
    xlabel(0(20)60) scheme(s2color)


    I get this error message:

    xlabels(0(20)60) not allowed, xaxis1 does not exist


    Can you help me please?

    Is there any way to see in the command window changes I make to the graph from the graph editor?
    This was possible for Stata v16 but I don't know how to do it in Stata v18.
    Thank you for your help.

    Chwen Chwen

    Click image for larger version

Name:	hbar_req_all-1.png
Views:	1
Size:	21.9 KB
ID:	1745125

  • #2
    The key point here is that with graph hbar the horizontal axis is considered to be the y axis. That flouts centuries of mathematical tradition but is a Stata convention intended to make it easy to flip between graph hbar and graph bar.

    See the help for graph hbar:

    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.
    This code fragment shows some technique.


    Code:
    clear 
    set obs 10 
    
    forval j = 1/5 {
        gen var`j' = _n > 2 * `j' - 1 
    }
    
    graph hbar var?, yla(0 0.2 "20" 0.4 "40" 0.6 "60" 0.8 "80" 1 "100") ytitle(percent)
    You may repeat % if you wish, but why do that?

    Giving us a data example, even a fake one as just given, might have elicited a speedier reply.

    Comment


    • #3
      Thank you, Nick, for your insightful explanation.
      I agree with you that a percent label is more straightforward and simple than % repeated many times.
      I just did not consider it while I was trying in the graph editor to get the graph right,
      thank you so much to make me be aware of it.

      Best wishes,
      Chwen Chwen

      Comment


      • #4
        Dear Stata users,

        I would like to ask your help again: how can I add in the code, say, N=24 below "Domestic" and N=50 below "Foreign" in the graph attached?

        I tried with the following code:

        graph hbar rep78, by(foreign) subtitle("N=48" "N=21")


        but I don't get the result I wish.

        Thank you.

        Chwen Chwen

        Click image for larger version

Name:	Graph-bar-subtitle.png
Views:	1
Size:	48.5 KB
ID:	1745229


        Comment


        • #5
          Here's one approach:

          Code:
          sysuse auto, clear 
          
          clonevar FOREIGN = foreign 
          
          forval x = 0/1 {
              local lbl : label (FOREIGN) `x'
              count if FOREIGN == `x'
              local lbl "`lbl' ({it:n} = `r(N)')"
              label def foreign `x' "`lbl'", add 
          }
          
          label val FOREIGN foreign 
          
          graph hbar mpg, over(FOREIGN)

          Comment


          • #6
            Thanks a lot, Nick!

            Comment

            Working...
            X