Announcement

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

  • Bar graph - Can x-labels be angled and include line breaks?

    Hello, I would like to create a bar graph with x-labels that are angled at 45 degrees, where one of the labels is split into two lines of text. Is this possible? When I run the following code, the resulting graph shows labels 1, 3, 4, and 5 at the desired 45 degree angle. Label #2, the label I would like to split into 2 lines of text, is displayed at the default 0 degrees, ruining the graph.

    Code:
    graph bar coreP1, ///
        over(num5, axis(lwidth(medthin) lcolor("217 217 217") lpattern(solid)) ///
            gap(60) label(labgap(0.05in) angle(45)) ///
            relabel( ///
                1 "Administrative" ///
                2 `" "Community" "Engagement" "' ///
                3 "Research" ///
                4 "Data Science" ///
                5 "Pilot Projects")) ///
        bar(1, fcolor("160 205 209") lcolor("030 137 129%100") lwidth(medium)) ///
        ylabel(0 "0%" 25 "25%" 50 "50%" 75 "75%" 100 "100%", ///
            angle(0) labsize(10pt) labgap(0.06in) notick ///
            gmax glwidth(medthin) glcolor("217 217 217") glpattern(solid)) ///
        ytitle("") ///
        yscale(lstyle(none) range(0 105)) ///
        blabel(bar, format(%4.1f) size(10.5pt) box bcolor(white) ///
            gap(1.4) margin(0 0 0 0)) ///
        graphregion(margin(l-4 b-0)) ///
        outergap(5) ///
        legend(off)
    core25.gph

    In case it is relevant, I am using Stata 18 and my operating system is Mac (macOS Tahoe 26.5.1). Thank you!

  • #2
    I don't use the Mac version of Stata. You may have unearthed a small bug.

    Posting a .gph file is deprecated in the FAQ as the forum software can't read it and to do that people need to download it and then read it into Stata.

    I don't see how your code leads to trailing % signs.

    The bigger question to me is that angled text is never ideal, and there is a simpler solution. Here it is: a horizontal bar chart.

    Code:
    clear 
    set obs 5 
    gen num5 = _n 
    input coreP1
    20
    53.3
    26.7
    13.3
    10
    
    graph hbar coreP1, ///
        over(num5, axis(lwidth(medthin) lcolor("217 217 217") lpattern(solid)) ///
            gap(60) label(labgap(0.05in)) ///
            relabel( ///
                1 "Administrative" ///
                2 `" "Community" "Engagement" "' ///
                3 "Research" ///
                4 "Data Science" ///
                5 "Pilot Projects")) ///
        bar(1, fcolor("160 205 209") lcolor("030 137 129%100") lwidth(medium)) ///
        ylabel(0 "0%" 25 "25%" 50 "50%" 75 "75%" 100 "100%", ///
            angle(0) labsize(10pt) labgap(0.06in) notick ///
            gmax glwidth(medthin) glcolor("217 217 217") glpattern(solid)) ///
        ytitle("") ///
        yscale(lstyle(none) range(0 105)) ///
        blabel(bar, format(%4.1f) size(10.5pt) box bcolor(white) ///
            gap(1.4) margin(0 0 0 0)) ///
        graphregion(margin(l-4 b-0)) ///
        outergap(5) ///
        legend(off)
    Click image for larger version

Name:	duthie.png
Views:	1
Size:	36.5 KB
ID:	1786340

    Comment

    Working...
    X