Announcement

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

  • Avoiding truncation of labels in graph

    Hi all,

    I am trying to avoid truncation of labels on the y-axis here in this graph below.

    I have tried to change margins using
    Code:
    graph hbar (count), ///
    over(spec, gap(*1) label(labsize(tiny))) ///
    blabel(bar, size(tiny)) yla(0(50)400, labsize(vsmall)) ///
    ysize(15) xsize(15)  ///
    ytitle("Number of repsodents", size(small)) ///
    title("Specialty you are COMPLETING in 2021 (mark only one)", size(small) span) ///
    graphregion(margin(zero))
    but they are still getting truncated. See the pic attached.
    Click image for larger version

Name:	Graph.jpg
Views:	1
Size:	126.9 KB
ID:	1672422



    I tried using splitvallables to reduce the space consumed on the left side to accommodate the full labels using
    Code:
    splitvallabels spec, nob
    graph hbar (count), ///
    over(spec, gap(*1) label(labsize(vsmall)) relabel(`r(relabel)')) ///
    blabel(bar, size(tiny)) yla(0(50)400, labsize(vsmall)) ///
    ysize(25) xsize(15)  ///
    ytitle("Number of repsodents", size(small)) ///
    title("Specialty you are COMPLETING in 2021 (mark only one)", size(small) span)
    .

    But then the labels are getting mixed up. See
    Click image for larger version

Name:	Graph2.jpg
Views:	1
Size:	102.7 KB
ID:	1672423


    Any help or feedback is much appreciated. Thank you.

  • #2
    See #11 of https://www.statalist.org/forums/for...rid-in-a-graph on using the -relabel()- option, but I would recommend installing splitvallabels from SSC and reading the documentation.

    Code:
    help splitvallabels

    In any case, your graph has too many items and is not readable as is, so fixing the truncation will help some but not much.
    Last edited by Andrew Musau; 06 Jul 2022, 13:03.

    Comment


    • #3
      Andrew Musau Thank you, I will try to work with splitvallabels. However, is there any way to push the labels to the left and utilize that extra space to avoid truncation in Graph 1, instead of using splitvallabels which takes up more vertical space (as you can see from Graph 2)?

      In any case, your graph has too many items and is not readable as is, so fixing the truncation will help some but not much.
      Is the first graph not readable too? Any suggestion on how it could be made more readable? Thanks much.
      Last edited by Lars Pete; 06 Jul 2022, 13:28.

      Comment


      • #4
        If you were not able to follow the linked example, provide a sample dataset using dataex. The same is required for alternative suggestions.

        Comment


        • #5
          Here:

          Code:
          * Example generated by -dataex-. For more info, type help dataex
          clear
          input str60 acgme byte(spec gender age)
          "400-35-21-270" 31 .  .
          "080-35-21-065"  5 1 34
          "430-35-21-070" 35 2 32
          "4013521004."   33 2 38
          "0413512029"     4 2 32
          "1403521287"     8 2 29
          "0403521104"     2 1  .
          "0803500127"     5 2 31
          ""              26 4  .
          "320-35-11-161" 27 2 30
          "4403521227"    36 1 30
          "140-35-21-301"  2 2  .
          ""               9 1  .
          "400-35-21-142" 31 2 31
          ""               5 4 31
          "2203521200"    23 2 32
          "2673521011"    40 1 33
          "4203511123"    34 1 33
          "3603521058"    42 1 38
          "140-35-21-287"  8 1  .
          "409-35-31-020" 31 2  .
          "0803500127"     5 1  .
          "140-35-21-510"  8 4  .
          "140-35-31-288"  8 1  .
          ""              25 2  .
          "409 352 1001"  31 1  .
          "400-35-11-242" 31 1 31
          "4003521270"    31 1 32
          "140-35-21-510"  5 2 27
          "1403531517"     8 2 33
          "140-35-00-921"  8 1 34
          " 3103521063"   26 2 36
          "260-35-00-225" 44 1 30
          "155-35-31-101" 14 1 38
          "477-318-5601"  27 1 39
          "302-35-30-002" 26 1 30
          "1403521486"     8 2  .
          "1403511303"     8 2 32
          ""               8 1 32
          "320-35-11-143" 27 1  .
          "3293521074"    28 2 31
          "0403521106"     2 1 30
          "1503521083"    18 1 53
          ""               8 1  .
          "123466"        23 2 40
          ""              12 1 33
          "320-352-1141"  27 2 34
          "1403521287"     8 4 28
          "1203521204"     7 2 38
          "141-35-14-283"  9 1 33
          "1403531288"    17 2 29
          ""              22 1 40
          "3203511161"    27 1 28
          ""              31 1 27
          "405-35-40-198" 32 2 42
          "4403521383"    36 2 29
          "362-35-00-154" 42 2 36
          "4463544006"    44 2 35
          "3003521398"    45 4  .
          "140-35-11-272"  8 4  .
          ""              36 1 26
          ""              43 2  .
          "4003511136"    31 1 35
          "400-35-11-136" 31 1 30
          ""              45 2 30
          ""               7 1 28
          ""               8 1 30
          "5203514058"    21 1 34
          "3293521075"    28 2 31
          ""               8 2 27
          "140-35-11-261"  8 2 27
          "340-35-21-039" 29 1 38
          "4003511134"    31 2 30
          ""              39 1 29
          "4403521394"    44 1 31
          "146-35-11-013" 15 1 39
          "1563513054"    17 2 41
          "2157320P0"     27 2 29
          "3293521077"    28 2 40
          "146-35-21-012" 15 2 45
          ""              26 1 32
          "380-35-80-112" 30 2 55
          "4403500452"    36 1  .
          "1003531074"     5 2 33
          ""              36 2 31
          "1553531144"    14 2 33
          "1403531314"     8 1  .
          " 3103521063"   26 1 33
          ""              19 2  .
          "4403500451"    36 1 26
          "4403500451"    36 1 38
          "unknown"        9 2 35
          ""              13 1 33
          "155-35-31-152" 14 1 40
          "2978146F0"     15 1 33
          ""              33 2 36
          "3633521010"    42 2  .
          ""              45 1 39
          "4003511128"    31 . 37
          "042-35-04-067"  4 1 31
          end
          label values spec labels18
          label def labels18 2 "Anesthesiology (General)", modify
          label def labels18 4 "Other Anesthesiology Subspecialty--specify:", modify
          label def labels18 5 "Dermatology", modify
          label def labels18 7 "Family Medicine", modify
          label def labels18 8 "Internal Medicine (General)", modify
          label def labels18 9 "Cardiology", modify
          label def labels18 12 "Gastroenterology", modify
          label def labels18 13 "Geriatrics", modify
          label def labels18 14 "Hematology/Oncology", modify
          label def labels18 15 "Infectious Disease", modify
          label def labels18 17 "Pulmonary Disease/CCM", modify
          label def labels18 18 "Rheumatology", modify
          label def labels18 19 "Other Internal Medicine Subspecialty-specify:", modify
          label def labels18 21 "Neurology", modify
          label def labels18 22 "Nuclear Medicine", modify
          label def labels18 23 "Obstetrics and Gynecology (General)", modify
          label def labels18 25 "Pathology (General)", modify
          label def labels18 26 "Pathology (Subspecialty)-specify:", modify
          label def labels18 27 "Pediatrics (General)", modify
          label def labels18 28 "Pediatrics (Subspecialty)-specify:", modify
          label def labels18 29 "Physical Medicine and Rehabilitation", modify
          label def labels18 30 "Preventive Medicine/Public Health/Occupational Medicine", modify
          label def labels18 31 "Psychiatry", modify
          label def labels18 32 "Child and Adolescent Psychiatry", modify
          label def labels18 33 "Other Psychiatry Subspecialty-specify", modify
          label def labels18 34 "Radiology (Diagnostic)", modify
          label def labels18 35 "Radiology (Therapeutic)", modify
          label def labels18 36 "Surgery (General)", modify
          label def labels18 39 "Ophthalmology", modify
          label def labels18 40 "Orthopedic Surgery", modify
          label def labels18 42 "Plastic Surgery", modify
          label def labels18 43 "Urology", modify
          label def labels18 44 "Other Surgical Subspecialty-specify:", modify
          label def labels18 45 "Other-specify:", modify
          label values gender labels1
          label def labels1 1 "Male", modify
          label def labels1 2 "Female", modify
          label def labels1 4 "Prefer not to disclose", modify

          Comment


          • #6
            It appears to me that your main issue is with cleaning your labels. If I decode the variable and directly use the strings to plot, I get no length restrictions. You can optimize on the space by dropping the axis labels as the information is provided by the bar labels and getting rid of the plot region margins.

            Code:
            * Example generated by -dataex-. For more info, type help dataex
            clear
            input str60 acgme byte(spec gender age)
            "400-35-21-270" 31 .  .
            "080-35-21-065"  5 1 34
            "430-35-21-070" 35 2 32
            "4013521004."   33 2 38
            "0413512029"     4 2 32
            "1403521287"     8 2 29
            "0403521104"     2 1  .
            "0803500127"     5 2 31
            ""              26 4  .
            "320-35-11-161" 27 2 30
            "4403521227"    36 1 30
            "140-35-21-301"  2 2  .
            ""               9 1  .
            "400-35-21-142" 31 2 31
            ""               5 4 31
            "2203521200"    23 2 32
            "2673521011"    40 1 33
            "4203511123"    34 1 33
            "3603521058"    42 1 38
            "140-35-21-287"  8 1  .
            "409-35-31-020" 31 2  .
            "0803500127"     5 1  .
            "140-35-21-510"  8 4  .
            "140-35-31-288"  8 1  .
            ""              25 2  .
            "409 352 1001"  31 1  .
            "400-35-11-242" 31 1 31
            "4003521270"    31 1 32
            "140-35-21-510"  5 2 27
            "1403531517"     8 2 33
            "140-35-00-921"  8 1 34
            " 3103521063"   26 2 36
            "260-35-00-225" 44 1 30
            "155-35-31-101" 14 1 38
            "477-318-5601"  27 1 39
            "302-35-30-002" 26 1 30
            "1403521486"     8 2  .
            "1403511303"     8 2 32
            ""               8 1 32
            "320-35-11-143" 27 1  .
            "3293521074"    28 2 31
            "0403521106"     2 1 30
            "1503521083"    18 1 53
            ""               8 1  .
            "123466"        23 2 40
            ""              12 1 33
            "320-352-1141"  27 2 34
            "1403521287"     8 4 28
            "1203521204"     7 2 38
            "141-35-14-283"  9 1 33
            "1403531288"    17 2 29
            ""              22 1 40
            "3203511161"    27 1 28
            ""              31 1 27
            "405-35-40-198" 32 2 42
            "4403521383"    36 2 29
            "362-35-00-154" 42 2 36
            "4463544006"    44 2 35
            "3003521398"    45 4  .
            "140-35-11-272"  8 4  .
            ""              36 1 26
            ""              43 2  .
            "4003511136"    31 1 35
            "400-35-11-136" 31 1 30
            ""              45 2 30
            ""               7 1 28
            ""               8 1 30
            "5203514058"    21 1 34
            "3293521075"    28 2 31
            ""               8 2 27
            "140-35-11-261"  8 2 27
            "340-35-21-039" 29 1 38
            "4003511134"    31 2 30
            ""              39 1 29
            "4403521394"    44 1 31
            "146-35-11-013" 15 1 39
            "1563513054"    17 2 41
            "2157320P0"     27 2 29
            "3293521077"    28 2 40
            "146-35-21-012" 15 2 45
            ""              26 1 32
            "380-35-80-112" 30 2 55
            "4403500452"    36 1  .
            "1003531074"     5 2 33
            ""              36 2 31
            "1553531144"    14 2 33
            "1403531314"     8 1  .
            " 3103521063"   26 1 33
            ""              19 2  .
            "4403500451"    36 1 26
            "4403500451"    36 1 38
            "unknown"        9 2 35
            ""              13 1 33
            "155-35-31-152" 14 1 40
            "2978146F0"     15 1 33
            ""              33 2 36
            "3633521010"    42 2  .
            ""              45 1 39
            "4003511128"    31 . 37
            "042-35-04-067"  4 1 31
            end
            label values spec labels18
            label def labels18 2 "Anesthesiology (General)", modify
            label def labels18 4 "Other Anesthesiology Subspecialty--specify:", modify
            label def labels18 5 "Dermatology", modify
            label def labels18 7 "Family Medicine", modify
            label def labels18 8 "Internal Medicine (General)", modify
            label def labels18 9 "Cardiology", modify
            label def labels18 12 "Gastroenterology", modify
            label def labels18 13 "Geriatrics", modify
            label def labels18 14 "Hematology/Oncology", modify
            label def labels18 15 "Infectious Disease", modify
            label def labels18 17 "Pulmonary Disease/CCM", modify
            label def labels18 18 "Rheumatology", modify
            label def labels18 19 "Other Internal Medicine Subspecialty-specify:", modify
            label def labels18 21 "Neurology", modify
            label def labels18 22 "Nuclear Medicine", modify
            label def labels18 23 "Obstetrics and Gynecology (General)", modify
            label def labels18 25 "Pathology (General)", modify
            label def labels18 26 "Pathology (Subspecialty)-specify:", modify
            label def labels18 27 "Pediatrics (General)", modify
            label def labels18 28 "Pediatrics (Subspecialty)-specify:", modify
            label def labels18 29 "Physical Medicine and Rehabilitation", modify
            label def labels18 30 "Preventive Medicine/Public Health/Occupational Medicine", modify
            label def labels18 31 "Psychiatry", modify
            label def labels18 32 "Child and Adolescent Psychiatry", modify
            label def labels18 33 "Other Psychiatry Subspecialty-specify", modify
            label def labels18 34 "Radiology (Diagnostic)", modify
            label def labels18 35 "Radiology (Therapeutic)", modify
            label def labels18 36 "Surgery (General)", modify
            label def labels18 39 "Ophthalmology", modify
            label def labels18 40 "Orthopedic Surgery", modify
            label def labels18 42 "Plastic Surgery", modify
            label def labels18 43 "Urology", modify
            label def labels18 44 "Other Surgical Subspecialty-specify:", modify
            label def labels18 45 "Other-specify:", modify
            label values gender labels1
            label def labels1 1 "Male", modify
            label def labels1 2 "Female", modify
            label def labels1 4 "Prefer not to disclose", modify
            
            decode spec, g(label)
            replace label = ustrregexra(ustrregexra(label, "(\w+|\w+\))(-specify|--specify[:]$)", "$1"), ":", "")
            contract label
            set scheme s1mono
            gr hbar _freq, over(label, lab(labsize(vsmall)) gap(25) sort(1)) ytitle(Frequency) ///
            blab(total, size(vsmall)) ylab("") ysc(r(. 17)) plotregion(margin(zero))
            Click image for larger version

Name:	Graph.png
Views:	1
Size:	106.9 KB
ID:	1672651

            Last edited by Andrew Musau; 07 Jul 2022, 13:42.

            Comment


            • #7
              Andrew Musau Thank you. I could only figure out vaguely what you did in the second line of the code but it worked perfectly. Best wishes.

              Comment

              Working...
              X