Announcement

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

  • how to trim the y-axis on a bar graph in Stata17

    Dear Statalist,

    I'm hoping to trim off part of the y-axis on a bar graph (circled in red in image below) to make it consistent with a graph combine (am combining with a range area plot)

    Click image for larger version

Name:	image_statalist.png
Views:	1
Size:	17.7 KB
ID:	1697306


    I've tried to use the yscale(range(numlist)) option without success, as you can see from the following code:

    webuse auto
    graph bar price if make=="AMC Concord" | make == "Volvo 260", over(foreign) yscale(range(0 15000))


    If anyone can advise how to trim the y-axis line a bit (while keeping the labels the same) that would be most appreciated

    Kind regards,
    Ben







  • #2
    If the problem is the little extra bit of axis beyond 15000, that's Stata's way of not truncating the axis label, as the axis label needs its own space too. You could try insisting that the axis goes to 15000 but remove the label at that point.

    Code:
    graph bar price if make=="AMC Concord" | make == "Volvo 260", over(foreign) yla(0(4000)12000)
    exemplifies how spelling out different y axis labels reduces the space used. Otherwise put, what is biting here is graph's default of trying to show "nice" numbers on the axis, where multiplies of 5 score over multiples of 4.

    The real problem here with your real data and other graphs we can't see is possibly that graph combine is being expected to clean up a bit of a mess.
    Last edited by Nick Cox; 15 Jan 2023, 03:39.

    Comment


    • #3
      I doubt that you will get perfection with graph combine if you don't with the -ycommon- option assuming that the legends are not causing the misalignment. See https://journals.sagepub.com/doi/pdf...36867X20976341 for some discussion on this.

      I'm hoping to trim off part of the y-axis on a bar graph
      If you mean that the line extends somewhat beyond some upper limit, then there is a -noextend- option within -yscale()- that will trim the axis. But there will still be a space left above the axis.

      Code:
      sysuse auto, clear
      graph bar price if make=="AMC Concord" | make == "Volvo 260", ///
      over(foreign) yla(0(4000)12000) ysc(range(0 12000) noextend)  ///
      ylab(, angle(hor)) ytitle("")
      Click image for larger version

Name:	Graph.png
Views:	1
Size:	15.1 KB
ID:	1697317





      To get rid of the spaces at the margins:

      Code:
      sysuse auto, clear
      graph bar price if make=="AMC Concord" | make == "Volvo 260", ///
      over(foreign) yla(0(4000)12000) plotregion(margin(zero)) ///
      ylab(, angle(hor)) ytitle("")
      Click image for larger version

Name:	Graph2.png
Views:	1
Size:	15.1 KB
ID:	1697318

      Comment


      • #4
        Thanks Nick,

        I found a solution to my question, which is to specify the option: plotregion(margin(zero))

        I'm still having difficulty getting the y axes to line up exactly using graph combine and can't think of a way to reformat data to use the by option because one graph has a categorical x-variable and the other has a continuous one.

        For example, the following code:

        webuse census.dta, clear

        gen poprural = pop - popurban
        gen proportion_metro = popurban/pop
        gen proportion_rural = poprural/pop
        graph bar proportion_metro proportion_rural, over(region) stack plotregion(margin(zero)) legend(off) name(graph1, replace)

        gen idvar = _n
        gen othervar = idvar*(1/50)
        scatter othervar idvar, plotregion(margin(zero)) xti("") xla(,notick) name(graph2, replace)

        graph combine graph1 graph2


        Generates the following graph,

        Click image for larger version

Name:	image_statalist_3.png
Views:	1
Size:	22.9 KB
ID:	1697324



        in which the zero on the y-axes are almost, but not exactly aligned. I had to suppress the x-axis ticks on the scatterplot (which is not ideal) to get this as close as possible and in my actual bar graph have some categorical axis labels that needs to span across 2 lines.

        Do you have any suggestions on how to get the y-axes to align exactly?

        Kind regards,
        Ben

        Comment


        • #5
          My only suggestion is to follow the advice of Andrew Musau in #3 and specify ycommon in graph combine.

          Comment


          • #6
            Andrew Musau Apologies only saw your response after posting the reply to Nick's post
            Thanks both. It still doesn't align perfectly with ycommon but it's close enough!

            Comment


            • #7
              You can fiddle around with -outergap()- within -xscale()- to try and achieve alignment.

              Code:
              webuse census.dta, clear
              
              gen poprural = pop - popurban
              gen proportion_metro = popurban/pop
              gen proportion_rural = poprural/pop
              graph bar proportion_metro proportion_rural, over(region) stack plotregion(margin(zero)) legend(off) name(graph1, replace)
              
              gen idvar = _n
              gen othervar = idvar*(1/50)
              scatter othervar idvar, plotregion(margin(zero)) xti("") xla(0(10)50, notick) xsc(outergap(*1.345)) name(graph2, replace)
              
              graph combine graph1 graph2, ycommon
              Last edited by Andrew Musau; 16 Jan 2023, 01:03.

              Comment

              Working...
              X