Announcement

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

  • Problem labeling xaxis with graph bar

    I am pretty sure this is an easy question but somehow I have not managed to find the answer on this forum. I am trying to create a chart with the following command:

    graph bar (count) xvar, over(date, label(labsize(small))), xvar>0, ytitle(XVAR) xtitle(Date)

    The "Date" variable is string (which is fine). For some reason, stata tells me:

    xtitle(Date) not allowed, xaxis1 does not exist

    Any help would be much appreciated, thanks!

  • #2
    This is explained in the Manual entry. graph dot and graph bar and graph hbar are written in terms of a y axis, which shows the outcome, and may be horizontal, and a categorical axis, which is the other axis.

    The code

    Code:
    , xvar > 0 ,
    is not going to make sense to Stata and will bite you, but your immediate question can be answered by example

    Code:
    . sysuse auto, clear
    (1978 automobile data)
    
    . graph bar (count) rep78, over(foreign)
    
    . graph bar (count) rep78, over(foreign) b1title(Foreign)
    
    .
    To put a title at the bottom of your graph you can use b1title() orb2title()

    It's much, much better to give a data example we can read in OR to pose a question in terms of a dataset accessible to all.

    Most users of Stata will have a hard time looking at

    Code:
    graph bar (count) xvar, over(date, label(labsize(small))), xvar>0, ytitle(XVAR) xtitle(Date)
    and seeing immediately what it is intended to do (what it does is fail as illegal syntax even with variables date and xvar).
    Last edited by Nick Cox; 05 Oct 2021, 11:57.

    Comment


    • #3
      This works perfectly, thank you very much!

      Comment


      • #4
        Originally posted by Nick Cox View Post
        This is explained in the Manual entry. graph dot and graph bar and graph hbar are written in terms of a y axis, which shows the outcome, and may be horizontal, and a categorical axis, which is the other axis.

        The code

        Code:
        , xvar > 0 ,
        is not going to make sense to Stata and will bite you, but your immediate question can be answered by example

        Code:
        . sysuse auto, clear
        (1978 automobile data)
        
        . graph bar (count) rep78, over(foreign)
        
        . graph bar (count) rep78, over(foreign) b1title(Foreign)
        
        .
        To put a title at the bottom of your graph you can use b1title() orb2title()

        It's much, much better to give a data example we can read in OR to pose a question in terms of a dataset accessible to all.

        Most users of Stata will have a hard time looking at

        Code:
        graph bar (count) xvar, over(date, label(labsize(small))), xvar>0, ytitle(XVAR) xtitle(Date)
        and seeing immediately what it is intended to do (what it does is fail as illegal syntax even with variables date and xvar).
        Dear Nick Cox Nick Cox ,

        Thanks for your answer! Now I have a very similar problem with X-axis when I am using "graph bar" command.
        Here is my dataset:
        year num_j
        2001 0
        2002 0
        2003 0
        2004 0
        2005 0
        2006 0
        2007 0
        2008 0
        2009 0
        2010 0
        2011 0
        2012 0
        2013 0
        2014 5
        2015 24
        2016 32
        2017 57
        2018 121
        2019 136

        code:
        graph bar n , over(year) ///
        ylabel(0(20)80, angle(h) glc(gs12) glw(thin) nogextend gmin gmax labsize(medium)) ///
        ytitle("Number of countries") ///
        title("New BRI countries in each year") ///
        blabel(bar, pos(outside) angle(180) size(large))

        I would like to create a bar chart. The problem now is that X-axis is too crowded. Then I tried to use "xlabel(2001(3)2019)" option. But stata tells "xaxis1 does not exist".

        After reading your answer here, I think it could also be explained "graph bar and graph hbar are written in terms of a y axis". So maybe you have some solutions for me to adjust X-axis in my case.

        Thanks very much in advance!

        Comment


        • #5
          See for discussion

          SJ-21-1 gr0086 Stata tip 140: Shorter or fewer category labels with graph bar
          . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . N. J. Cox
          Q1/21 SJ 21(1):263--271 (no commands)
          tip on fixing categorical axis labels

          https://journals.sagepub.com/doi/epu...6867X211000032

          In your case I would just switch to twoway bar. Here is some technique

          Code:
          clear 
          input year num_j
          2001 0
          2002 0
          2003 0
          2004 0
          2005 0
          2006 0
          2007 0
          2008 0
          2009 0
          2010 0
          2011 0
          2012 0
          2013 0
          2014 5
          2015 24
          2016 32
          2017 57
          2018 121
          2019 136
          end 
          
          twoway bar n year, ylabel(0(25)125, angle(h) glc(gs12) glw(thin) nogextend gmin gmax labsize(medium)) ///
          ytitle("Number of countries") ///
          title("New BRI countries in each year") ///
          || scatter n year, ms(none) mla(n) mlabpos(12) legend(off) xla(2001(3)2019)
          Code:
          
          


          Click image for larger version

Name:	bri_bar.png
Views:	1
Size:	33.8 KB
ID:	1726062
          or even

          Code:
          twoway bar n year, ysc(off) ///
          ytitle("Number of countries") ///
          title("New BRI countries in each year") ///
          || scatter n year, ms(none) mla(n) mlabpos(12) legend(off) xla(2001(3)2019) xtitle("")
          Click image for larger version

Name:	bri_bar2.png
Views:	1
Size:	27.7 KB
ID:	1726063
          Sometimes less (clutter) is more (effective).

          Comment


          • #6
            Thanks professor for your comments and suggestions!It is very helpful!

            I also find another solution based on "graph bar". I specify the label size by ", label(labelsize(*0.8)" under "over" option. The final codes as follows:

            graph bar n , over(year, ,label(labsize(*0.8) ) ///
            ylabel(0(20)80, angle(h) glc(gs12) glw(thin) nogextend gmin gmax labsize(medium)) ///
            ytitle("Number of countries") ///
            title("New BRI countries in each year") ///
            blabel(bar, pos(outside) angle(180) size(large))

            Maybe these codes could also help others facing similar problems.

            Comment


            • #7
              Why do your axis labels go 0(20)80 when the data go to 136? Just curious.

              Comment

              Working...
              X