Announcement

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

  • How can I increase the number of characters on axis labels on a graph?

    I have slightly long value labels for a variable I have created a horizontal bar chart for, and they have been truncated. I would like to increase the length of the axis labels, if possible, as they are more important information in this case than the bars (i.e., I would like the bars to take up less space in the graph).
    Code:
    graph hbar (asis) prop in 1/20, over(occup2merge, sort(prop) descending label(labsize(small))) title("Modal two-digit occupations in PALMS", span) note("Total proportion of categories represented here: `represent'")
    Click image for larger version

Name:	modaltwodigitoccs.png
Views:	1
Size:	44.8 KB
ID:	1558063


    Dataex (I collapsed the dataset with a sampling weight, and generated the percentages. I have just looked up -contract- and I see that I should have used contract.):
    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input float occup2merge double count float prop
    91 159901414.10819277     .16708773
    51  94526189.24348901      .0987744
    41  65583660.85793375     .06853113
    93  59233561.85169494     .06189565
    71  58507001.71862424     .06113643
    92  58345606.10650118     .06096778
    83 51823326.948460504     .05415238
    12  47010386.76683183     .04912313
    52  40218623.46498255     .04202614
    72 39016734.760210834     .04077023
    42   33545790.1701924     .03505341
    34  31543574.66525055    .032961212
    33 28412273.521815125     .02968918
    13 26106903.896305665    .027280204
    82 22430265.660911735     .02343833
    24 21995550.273464024     .02298408
    31 19696664.311145265    .020581875
    32  17166525.78292244    .017938027
    74  16765460.62872613    .017518936
    23 12755740.775627147     .01332901
    21  9954382.979502445    .010401755
    81  8386879.132879108    .008763803
    61  8167152.061751989    .008534201
    62  8015457.204215655    .008375689
    22  7372757.707365178    .007704105
    73  5115432.935445052    .005345332
    11 3441729.1718450896    .003596408
     8 1426840.1302517399   .0014909654
    53  282556.7655838961   .0002952555
    35  161876.3596279645  .00016915143
    15  27176.76826377258  .00002839815
    98   25693.6422316163  .00002684837
    99 13908.765224794282 .000014533855
    57  4631.417922723242  4.839564e-06
    77 1393.7234084775369 1.4563603e-06
    84 1157.9445286330504  1.209985e-06
    40 1059.1637348730678 1.1067648e-06
    44  651.9258829926297  6.812248e-07
    56  643.7014519852817  6.726308e-07
    14  629.4893419596493    6.5778e-07
    55  628.4369225556962  6.566802e-07
    47  623.5352522169171  6.515583e-07
    68    502.07629356924  5.246407e-07
    43 474.04928602656105  4.953541e-07
    67  403.4298282043139  4.215608e-07
    30 354.26303438678246  3.701844e-07
    29    272.17477357745  2.844069e-07
    58 249.17024150356258  2.603685e-07
    end
    label values occup2merge occup2merge
    label def occup2merge 12 "12 Corporate managers", modify
    label def occup2merge 13 "13 General managers", modify
    label def occup2merge 23 "23 Teaching professionals", modify
    label def occup2merge 24 "24 Other professionals", modify
    label def occup2merge 31 "31 Natural science and engineering associate professionals", modify
    label def occup2merge 32 "32 Life science and health associate professionals", modify
    label def occup2merge 33 "33 Teaching associate professionals", modify
    label def occup2merge 34 "34 Other associate professionals", modify
    label def occup2merge 41 "41 Office clerks", modify
    label def occup2merge 42 "42 Customer service clerks", modify
    label def occup2merge 51 "51 Personal and protective services", modify
    label def occup2merge 52 "52 Models, salespersons and demonstrators", modify
    label def occup2merge 71 "71 Extraction and building trades workers", modify
    label def occup2merge 72 "72 Metal and machinery workers", modify
    label def occup2merge 74 "74 Other craft workers", modify
    label def occup2merge 82 "82 Machine operators and assemblers", modify
    label def occup2merge 83 "83 Drivers and mobile-plant operators", modify
    label def occup2merge 91 "91 Elementary sales and services", modify
    label def occup2merge 92 "92 Agricultural and fishery labourers", modify
    label def occup2merge 93 "93 Labourers in mining, construction, manufacturing and transport", modify

  • #2
    If you are willing to sacrifice the first three characters, then you can make progress like this:

    Code:
    decode occup2merge, gen(toshow)
    replace toshow = substr(toshow, 4, .)
    
    graph hbar (asis) prop in 1/20, over(toshow, sort(prop) descending label(labsize(small))) title("Modal two-digit occupations in PALMS", span) note("Total proportion of categories represented here: `represent'") yla(0(0.05)0.15) xsize(7)

    Comment


    • #3
      On a slightly different issue, I find ordering by value here makes it hard to interpret the multiple 'other' categories here, such as 'Other associate professionals'. It is straightforward when you see the 30-s group:

      31 Natural science and engineering associate professionals
      32 Life science and health associate professionals
      33 Teaching associate professionals
      34 Other associate professionals


      But on the chart it is all chaotically interleaved with other groups, so interpretation of 'other' is confusing. Perhaps, present it as separate charts by first digit code?? Maintaining the last position for other, regardless of its value.

      Best, Sergiy Radyakin

      PS: in other words, interpretation of "Other X" is possible only in the context of already presented X1, X2,... and (imho) is not possible independently.

      Comment


      • #4
        I didn't really want to sacrifice the first three characters, as they are useful information for the reader to check the occupations codebook. The following example shows that there is enough room in the graph for more characters. Is it impossible in Stata to force more characters in? Where is the character restriction documented?
        I am using Stata version 15.1.

        Code:
        graph hbar (asis) prop in 1/20, over(occup2merge, sort(prop) descending label(labsize(small))) title("Modal two-digit occupations in PALMS", span) note("Total proportion of categories represented here: `represent'") yla(0(0.05)0.15) aspectratio(1.5) xsize(7) ysize(4.5)
        Click image for larger version

Name:	Graph.png
Views:	1
Size:	39.4 KB
ID:	1558207

        Last edited by Aidan Horn; 11 Jun 2020, 06:09.

        Comment


        • #5
          Caveat emptor.

          Somewhere along the way -graph- calls _ggroup.ado which truncates the label lengths to 32 character. Lines 78-80:
          Code:
                  if ("`truncate'" == "") {
                      local truncate 32
                  }
          If the change this to 65, -discard-, and then re-run your example you get:

          Click image for larger version

Name:	Graph.png
Views:	1
Size:	99.4 KB
ID:	1558243

          Comment


          • #6
            That is a good detail from Scott Merryman. The best fix would be if graph bar (etc.) allowed a truncate() option to be passed along. Otherwise hacking at Stata's own code is at your peril.

            Comment


            • #7
              Thank you Scott. I perhaps have a different version to you, because I had `c(namelenchar)' as the value on line 41 of "C:\Stata15\ado\base\_\_ggroup.ado":
              Code:
              local truncate 65 // `c(namelenchar)'
              Nick, I understand that changing this is risky (I'd also caution other readers of this post to be careful when changing the Stata base code, at least without remembering it). I'll keep this change I made in mind as I continue using Stata in future. I've encountered this problem of having too-short axis labels before, so I like it how I'm able to change this.
              Click image for larger version

Name:	modaltwodigitoccs.png
Views:	1
Size:	47.5 KB
ID:	1558264

              Comment


              • #8
                Yes, I am using version 16.1:

                Code:
                . which _ggroup
                C:\Program Files\Stata16\ado\base\_\_ggroup.ado
                *! version 1.0.0  14apr2020

                Comment


                • #9
                  Something to note is that the SSC package splitvallabels can transform the value labels onto multiple lines.
                  https://www.ssc.wisc.edu/sscc/pubs/stata_bar_graphs.htm

                  Comment


                  • #10
                    Hacking Stata code seems overly excessive, unless you often produce graphs with very long labels. You can simply specify labels greater than 32 characters using the -relabel- option. See #11 of the following link.

                    https://www.statalist.org/forums/for...rid-in-a-graph

                    Comment


                    • #11
                      Originally posted by Andrew Musau View Post
                      Hacking Stata code seems overly excessive, unless you often produce graphs with very long labels. You can simply specify labels greater than 32 characters using the -relabel- option. See #11 of the following link.

                      https://www.statalist.org/forums/for...rid-in-a-graph
                      Andrew, could you please show that -relabel- can be applied to my example?
                      Thanks
                      Last edited by Aidan Horn; 15 Jun 2020, 01:20.

                      Comment


                      • #12
                        Andrew, this question is about how to increase the length of the axis labels, and I believe that -relabel- does not achieve that.
                        Erm, actually it does as illustrated in the linked example.

                        Code:
                        * Example generated by -dataex-. To install: ssc install dataex
                        clear
                        input float occup2merge double count float prop
                        91 159901414.10819277     .16708773
                        51  94526189.24348901      .0987744
                        41  65583660.85793375     .06853113
                        93  59233561.85169494     .06189565
                        71  58507001.71862424     .06113643
                        92  58345606.10650118     .06096778
                        83 51823326.948460504     .05415238
                        12  47010386.76683183     .04912313
                        52  40218623.46498255     .04202614
                        72 39016734.760210834     .04077023
                        42   33545790.1701924     .03505341
                        34  31543574.66525055    .032961212
                        33 28412273.521815125     .02968918
                        13 26106903.896305665    .027280204
                        82 22430265.660911735     .02343833
                        24 21995550.273464024     .02298408
                        31 19696664.311145265    .020581875
                        32  17166525.78292244    .017938027
                        74  16765460.62872613    .017518936
                        23 12755740.775627147     .01332901
                        21  9954382.979502445    .010401755
                        81  8386879.132879108    .008763803
                        61  8167152.061751989    .008534201
                        62  8015457.204215655    .008375689
                        22  7372757.707365178    .007704105
                        73  5115432.935445052    .005345332
                        11 3441729.1718450896    .003596408
                         8 1426840.1302517399   .0014909654
                        53  282556.7655838961   .0002952555
                        35  161876.3596279645  .00016915143
                        15  27176.76826377258  .00002839815
                        98   25693.6422316163  .00002684837
                        99 13908.765224794282 .000014533855
                        57  4631.417922723242  4.839564e-06
                        77 1393.7234084775369 1.4563603e-06
                        84 1157.9445286330504  1.209985e-06
                        40 1059.1637348730678 1.1067648e-06
                        44  651.9258829926297  6.812248e-07
                        56  643.7014519852817  6.726308e-07
                        14  629.4893419596493    6.5778e-07
                        55  628.4369225556962  6.566802e-07
                        47  623.5352522169171  6.515583e-07
                        68    502.07629356924  5.246407e-07
                        43 474.04928602656105  4.953541e-07
                        67  403.4298282043139  4.215608e-07
                        30 354.26303438678246  3.701844e-07
                        29    272.17477357745  2.844069e-07
                        58 249.17024150356258  2.603685e-07
                        end
                        label values occup2merge occup2merge
                        label def occup2merge 12 "12 Corporate managers", modify
                        label def occup2merge 13 "13 General managers", modify
                        label def occup2merge 23 "23 Teaching professionals", modify
                        label def occup2merge 24 "24 Other professionals", modify
                        label def occup2merge 31 "31 Natural science and engineering associate professionals", modify
                        
                        graph hbar (asis) prop in 1/20, over(occup2merge, descending label(labsize(small)))
                        gr save gr1, replace
                        graph hbar (asis) prop in 1/20, over(occup2merge, sort(prop) descending ///
                        relabel(5 "31. Natural science and engineering associate professionals" )label(labsize(small)))
                        gr save gr2, replace
                        gr combine gr1.gph gr2.gph
                        Res.:


                        Click image for larger version

Name:	Graph.png
Views:	1
Size:	35.4 KB
ID:	1558868



                        This thread can be closed from here.
                        Good that you solved your problem, but bear in mind that if you post on a public forum like Statalist, the thread is not only for your own benefit but for others who will face the same problem in the future. Therefore, others can continue this thread if they have a similar issue and there is no ownership of threads as such.
                        Last edited by Andrew Musau; 15 Jun 2020, 01:17.

                        Comment

                        Working...
                        X