Announcement

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

  • Formatting of barchart produced with grc1leg

    I have combined five horizontal barcharts into one figure with a common legend using grc1leg. My code is inserted below and I have attached the resulting graph.

    Overall it works fine and produces the desired graph - however, I am struggling with the following issues related to formatting:
    1. I would like to add some horizontal spacing so that the graph appears less condensed/squeezed (hope this sentence makes sense - English is not my first language)
    2. I would like the five graphs to appear similar in the combined graph (right now the Fifth graph appears smaller)
    3. I would like to make the legend wider
    4. I would like to add percentages to the bars
    5. I would like to move the five questions to the left
    6. I would like to change the font to times new roman
    7. I would like to remove the numbers 1-5 from the output
    Any suggestions about how these issues can be fixed?



    input quest str25 q a1 a2 a3 a4
    1 "Model A" 7 9 53 211
    1 "Model B" 4 8 58 210
    1 "Model C" 0 13 65 202
    2 "Model A" 49 25 110 96
    2 "Model B" 24 28 150 78
    2 "Model C" 2 17 138 123
    3 "Model A" 34 19 110 117
    3 "Model B" 16 34 143 87
    3 "Model C" 5 27 142 106
    4 "Model A" 62 29 108 81
    4 "Model B" 42 45 119 74
    4 "Model C" 18 43 131 88
    5 "Model A" 176 20 32 52
    5 "Model B" 194 25 32 29
    5 "Model C" 223 29 16 12
    end

    graph hbar a1-a4 if quest==1, percent over(quest, gap(1)) over(q, gap(10)) ///
    stack legend(off) yscale(off) yline(20 40 60 80 ,lwidth(0.25) lcolor(black)) ///
    subtitle("Do you agree with the first question?") ///
    saving(a1, replace) yscale(off) plotregion(margin( b+3 t+3)) outergap(30)

    graph hbar a1-a4 if quest==2, percent over(quest, gap(1)) over(q, gap(10)) ///
    stack legend(off) yscale(off) yline(20 40 60 80 ,lwidth(0.25) lcolor(black)) ///
    subtitle("Do you agree with the second question?") ///
    saving(a1a, replace) yscale(off) plotregion(margin( b+3 t+3)) outergap(30)

    graph hbar a1-a4 if quest==3, percent over(quest, gap(1)) over(q, gap(10)) ///
    stack legend(off) yscale(off) yline(20 40 60 80 ,lwidth(0.25) lcolor(black)) ///
    subtitle("Do you agree with the third question?") ///
    saving(a1b, replace) yscale(off) plotregion(margin( b+3 t+3)) outergap(30)

    graph hbar a1-a4 if quest==4, percent over(quest, gap(1)) over(q, gap(10)) ///
    stack legend(off) yscale(off) yline(20 40 60 80 ,lwidth(0.25) lcolor(black)) ///
    subtitle("Do you agree with the fourth question?") ///
    saving(a1c, replace) yscale(off) plotregion(margin( b+3 t+3)) outergap(30)

    graph hbar a1-a4 if quest==5, percent over(quest, gap(1)) over(q, gap(10)) ///
    subtitle("Do you agree with the fifth question?") ///
    stack legend(span rows(1) label(1 "No") label(2 "To a small degree") ///
    label(3 "To some degree") label(4 "To a high degree ") ///
    size(small)) ///
    yline(20 40 60 80 ,lwidth(0.25) lcolor(black)) saving(a2, replace)

    grc1leg a1.gph a1a.gph a1b.gph a1c.gph a2.gph, cols(1) ///
    imargin(0 0 0 0 0) ycommon xcommon legendfrom(a2.gph)

    graph export "$rpath\utilitygraph_example.png", replace

    exit

  • #2
    Thanks for the self-contained example and challenging question. Please post graphs as .png attachments, not photos (FAQ Advice #12).

    But as you are well aware, your code creates several problems, most of which can be avoided. I don't think it is necessary to touch grc1leg at all.

    ​Insisting on small text indicates a major problem. Creating separate graphs to later combine them creates several problems, as so much space goes into separating and surrounding them. Repeated identical text is not needed. Even with your squeezes, the bars, axis labels and legend text are all uncomfortably close.

    With your chosen design of stacked bar charts I think you can do better with graph hbar defaults with the one simple trick of two over() options.

    I think stacked bars are overrated so I also tried my hand with tabplot (SSC; various Stata Journal mentions). This design allows the percents to be shown legibly: I don't think you have any scope to do that with the stacked design. (They could be made larger.)

    Here is the code and then two candidate graphs.


    Code:
    clear
    
    set scheme s1color
    
    input quest str25 q a1 a2 a3 a4
    1 "Model A" 7 9 53 211
    1 "Model B" 4 8 58 210
    1 "Model C" 0 13 65 202
    2 "Model A" 49 25 110 96
    2 "Model B" 24 28 150 78
    2 "Model C" 2 17 138 123
    3 "Model A" 34 19 110 117
    3 "Model B" 16 34 143 87
    3 "Model C" 5 27 142 106
    4 "Model A" 62 29 108 81
    4 "Model B" 42 45 119 74
    4 "Model C" 18 43 131 88
    5 "Model A" 176 20 32 52
    5 "Model B" 194 25 32 29
    5 "Model C" 223 29 16 12
    end
    
    replace q = subinstr(q, "Model ", "", .)
    graph hbar a1-a4, percent over(q) over(quest) ///
    stack yline(20 40 60 80,lwidth(0.25) lcolor(black)) ///
    legend(rows(2) order(1 "no" 2 "small degree" 3 "some degree" 4 "high degree"))  
    
    gen id = _n
    reshape long a, i(id) j(answer)
    rename q Model
    bysort quest Model : gen total = sum(a)
    by quest Model: replace total = total[_N]
    capture ssc inst tabplot
    local ylabels 1 "no" 2 `" "small" "degree" "' 3 `" "some" "degree" "' 4 `" "high" "degree" "'
    tabplot answer Model [fw=a], yasis by(quest, row(1) note("percent") compact) ///
    ytitle("") percent(quest Model) showval(mlabcolor(black)) yla(`ylabels', ang(h) tlcolor(none))
    Click image for larger version

Name:	astrid1.png
Views:	1
Size:	10.6 KB
ID:	1331968



    Click image for larger version

Name:	astrid2.png
Views:	1
Size:	24.0 KB
ID:	1331969

    Last edited by Nick Cox; 22 Mar 2016, 07:58.

    Comment


    • #3
      Thank you for the hints and the useful code examples. They enable me to make the graph I would like.

      Comment

      Working...
      X