Announcement

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

  • Why graph bar with by option leave blank space in left and right panel?

    Dear Stata users,

    I run a simple graph bar command with by() option. The result I get is some of weired, because in the left and right individual graphs, I find Stata leaves blank space between the bar and the margin, and thus bars width become thinner than normal. I want to know what's going wrong and find a solution. Thank you very much.

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input str18 group str40 item float percent
    "United of American" "This is a Label"             45
    "United of American" "This is not a Label"       12.8
    "United of Kingdom"  "This Label is real"        79.6
    "United of Kingdom"  "This Label is not real"     6.9
    "United of Nations"  "This is a long Label"      53.5
    "United of Nations"  "This is a real long Label" 72.9
    end
    
    graph bar percent, over(item) by(group, row(1) compact) nofill ytitle("") name(g1)
    graph bar percent, over(item, relabel(1 "This" 2 "is" 3 "not" 4 "a" 5 "long" 6 "label")) by(group, row(1) compact) nofill ytitle("") name(g2)
    Click image for larger version

Name:	g1.png
Views:	1
Size:	85.9 KB
ID:	1770738


    Click image for larger version

Name:	g2.png
Views:	1
Size:	71.9 KB
ID:	1770739

  • #2
    Should be caused by using distinct categories across groups. The -nofill- option here does not work as you think. Create a variable that orders the bars within groups and use this instead.

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input str18 group str40 item float percent
    "United States" "This is a Label"             45
    "United States" "This is not a Label"       12.8
    "United Kingdom"  "This Label is real"        79.6
    "United Kingdom"  "This Label is not real"     6.9
    "United Nations"  "This is a long Label"      53.5
    "United Nations"  "This is a real long Label" 72.9
    end
    
    gen order= cond(ustrregexm(item,"\b(is a Label|not real|a long Label)\b"), 1, 2)
    graph bar percent, over(order, relabel(1 "This" 2 "is" 3 "not" 4 "a" 5 "long" 6 "label")) by(group, row(1) compact note("")) ytitle("")
    Click image for larger version

Name:	Graph.png
Views:	1
Size:	33.6 KB
ID:	1770754
    Last edited by Andrew Musau; 14 Jan 2025, 07:03.

    Comment


    • #3
      I spent some time on this without making any progress. Andrew Musau's nice code produces bars and spaces of equal size, but legend labels 3 4 5 6 need to be fixed otherwise.

      Comment


      • #4
        I didn't pay attention to the labels. Since using a common grouping variable precisely identifies the number of bars in each group, using -relabel()- to fix the labels won't work. Instead, one can use the graph editor for this.

        Code:
        * Example generated by -dataex-. To install: ssc install dataex
        clear
        input str18 group str40 item float percent
        "United States" "This is a Label"             45
        "United States" "This is not a Label"       12.8
        "United Kingdom"  "This Label is real"        79.6
        "United Kingdom"  "This Label is not real"     6.9
        "United Nations"  "This is a long Label"      53.5
        "United Nations"  "This is a real long Label" 72.9
        end
        
        gen order= cond(ustrregexm(item,"\b(is a Label|not real|a long Label)\b"), 1, 2)
        graph bar percent, over(order) by(group, row(1) compact note("")) nofill ytitle("") 
        
        *THIS WORKS FOR 2 BARS. COORDINATES NEED TO BE MODIFIED FOR ADDITION OF BARS
        *MAY BE EASIER TO DO DIRECTLY IN THE GRAPH EDITOR
        local labels This is a long not label
        local bar 0
        forval gr=1/3{
            local++bar
            gr_edit .plotregion1.grpaxis[`gr'].edit_tick 1 22.8013 `=word("`labels'", `bar')', tickset(major)
            local++bar
            gr_edit .plotregion1.grpaxis[`gr'].edit_tick 2 77.1987 `=word("`labels'", `bar')', tickset(major)
        }

        Click image for larger version

Name:	Graph.png
Views:	1
Size:	33.8 KB
ID:	1770763

        Comment


        • #5
          Dear Andrew Musau Nick Cox thank you very much! It seems that using distinct categories across groups is not a regular, not to say legal, use of graph bar, by(). Fortunately, Andrew gives a nice solution, and Nick makes it complete.

          Comment

          Working...
          X