Announcement

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

  • Shifting labels vertically when using hbar

    When creating a horizontal bar graph, the default behavior seems to be to align the labels with the middle of the bars. For example:

    Code:
    sysuse nlsw88.dta, clear
    
    graph hbar wage, over(married)
    Results in:



    You can see that the labels "single" and "married" are approximately aligned to be in the center of the bars.

    However, when you have enough variables with a large enough label size, the labels are no longer aligned with the middle, and seem to be shifted down. For example:

    Code:
    sysuse nlsw88.dta, clear
    
    gen arbitrary_cat = ""
    forvalues i = 1/31 {
        replace arbitrary_cat = "Arbitrary cat " + "`i'" if mod(_n, 31) == `i'
    }
    graph hbar wage, over(arbitrary_cat, sort(wage) label(labsize(0.2cm) labgap(0.05cm)))
    Results in:



    Here you can see the labels dip below the bars and are no longer approximately in the middle. Perhaps easier to look at:

    Code:
    graph hbar wage, over(arbitrary_cat, sort(wage) label(labsize(0.2cm) labgap(0.05cm))) legend(off) asyvars showyvars


    The extra space afforded by eliminating the gaps between the bars seems to raise the labels a bit, but they are still seemingly aligned with the bottom of the bars rather than the middle.

    How can I shift the labels vertically so that they align to the middle?

    (Note: I'm using Stata 16, if that makes any difference)
    Last edited by Tyson Banks; 29 Jun 2024, 19:31.

  • #2
    It could be your font. Even in V16, I do not get the misalignment.

    Click image for larger version

Name:	Graph.png
Views:	1
Size:	33.2 KB
ID:	1757455


    You can check and adjust your defaults using

    Code:
    graph set
    where I have:

    Code:
    . graph set
    -> graph set window
    
       window           current
       setting          default          choices
       ------------------------------------------------------------------------
       fontface         Arial             font name
       fontfacesans     Arial             font name
       fontfaceserif    Times New Roman   font name
       fontfacemono     Courier New       font name
       fontfacesymbol   Symbol            font name
       ------------------------------------------------------------------------
       To change setting, type "graph set window setting choice"

    Comment


    • #3
      After setting my fonts identically to yours, I am still getting the same misalignment I got before. So now there is an element of mystery to this.

      I'm using Stata for Mac, which I don't think should make a difference, but I feel like it could.

      Comment


      • #4
        You can experiment with the fonts in #13 of https://www.statalist.org/forums/for...hs-in-stata-17. I'd first start by updating Stata.

        Code:
        update all
        If that does not solve it, maybe a Mac user can test the code in #1 and tell you whether they can replicate your issue. If that's not the case, you may need to reinstall Stata.

        Comment


        • #5
          To be clear, is it just not possible to manually shift the labels upward? It is possible to shift blabels upwards or downwards, so I would think there's also a way of doing it with the categorical labels.

          Comment


          • #6
            I do not think so, the -labgap()- option will just increase or decrease the gap between the label and the ticks (horizontal distance). You can browse the axis label options using

            Code:
            help axis_label_options
            This does not seem to be a general problem (I suspect even for Mac users, otherwise, it would have come up more frequently). Therefore, it would be better to determine why it happens to you and not to others, and then fix it. If you are unable to make progress, contact Tech Support at https://www.stata.com/support/tech-support/contact/. They will need your license information before they can process your query.
            Last edited by Andrew Musau; 30 Jun 2024, 09:14.

            Comment


            • #7
              For anyone who might have this same problem and stumbles upon this thread in the future: I have technically found a solution, but it just might be the worst solution imaginable:

              Code:
              sysuse nlsw88.dta, clear
              
              local starting_position_x = 0.018
              local starting_position_y = 99.13
              local offset_value = 3.351
              local gap_size = 1.421
              gen arbitrary_cat = ""
              forvalues i = 1/31 {
                  replace arbitrary_cat = "Arbitrary cat " + "`i'" if mod(_n, 31) == `i'
              }
              egen cat_average = mean(wage), by(arbitrary_cat)
              gsort- cat_average
              gen name_value = ""
              gen unique_counter = 1 if cat_average != cat_average[_n - 1] & arbitrary_cat != ""
              qui count if unique_counter == 1
              local labels_number = r(N)
              local counter_for_cat = r(N) + 1
              local max = _N
              forvalues i = 1/`max' {
                  if unique_counter[`i'] == 1 {
                      local counter_for_cat = `counter_for_cat' - 1
                      replace name_value = arbitrary_cat[`i'] if _n == `counter_for_cat'
                  }
              }
              gen strL label_settings = ""
              forvalues i = 1/`labels_number' {
                  replace label_settings = label_settings + "text(`starting_position_x' `starting_position_y' " + `"""' + name_value[`i'] + `"""' + ", size(0.2cm) color(black) placement(west)) "
                  local starting_position_y = `starting_position_y' - `offset_value'
              }
              local all_label_text = label_settings
              graph hbar wage, over(arbitrary_cat, sort(wage) label(labsize(0cm) labgap(`gap_size'cm) labcolor(ltbluishgray))) `all_label_text'
              drop cat_average name_value unique_counter label_settings
              This produces:




              Exactly the desired result. Except that the first four numbers—starting_position_x, starting_position_y, offset_value, and gap_size—need to be manually adjusted each time you make a graph. I don't suggest using this unless like me you too are insane.


              Comment

              Working...
              X