Announcement

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

  • hbar graph help!!!!!!

    Hi! I wrote this combined graph with the grc1leg package, but I cannot seem to fix the titles to the left.

    Code:
     
    
    ssc install catplot
    net from http://www.stata.com
    net cd users
    net cd vwiggins
    net install grc1leg
    
    graph hbar, over(mt_indicators_1) asyvar percent stack legend(off) yscale(off) yline(20 40 60 80,lwidth(0.25) lcolor(gs11)) saving(t1, replace)yscale(off) plotregion(margin( b+3 t+3))  outergap(30) intensity(30) blabel(bar, bfcolor(black*0.2) position(center) format(%3.1f)) title("Useful", position(9))
    graph hbar, over(mt_indicators_2) asyvar percent stack legend(off) yscale(off) yline(20 40 60 80,lwidth(0.25) lcolor(gs11)) saving(t2, replace)yscale(off) plotregion(margin( b+3 t+3))  outergap(30) intensity(30) blabel(bar, bfcolor(black*0.2) position(center) format(%3.1f))  title("Affordable", position(9))
    graph hbar, over(mt_indicators_3) asyvar percent stack legend(off) yscale(off) yline(20 40 60 80,lwidth(0.25) lcolor(gs11)) saving(t3, replace)yscale(off) plotregion(margin( b+3 t+3))  outergap(30) intensity(30) blabel(bar, bfcolor(black*0.2) position(center) format(%3.1f)) title(Reliable,position(9))
    graph hbar, over(mt_indicators_4) asyvar percent stack legend(off) yscale(off) yline(20 40 60 80,lwidth(0.25) lcolor(gs11)) saving(t4, replace)yscale(off) plotregion(margin( b+3 t+3))  outergap(30) intensity(30) blabel(bar, bfcolor(black*0.2) position(center) format(%3.1f)) title(Can share,position(9))
    graph hbar, over(mt_indicators_5) asyvar percent stack legend(off) yscale(off) yline(20 40 60 80,lwidth(0.25) lcolor(gs11)) saving(t5, replace)yscale(off) plotregion(margin( b+3 t+3))  outergap(30) intensity(30) blabel(bar, bfcolor(black*0.2) position(center) format(%3.1f)) title(Safe,position(9))
    graph hbar, over(mt_indicators_3) asyvar percent stack legend(span rows(1) label(1 "Strongly disagree") label(2 "Somewhat disagree") label(3 "Neither agree nor disagree") label(4 "Somewhat agree") label(5 "Strongly agree") label(6 "Always") size(vsmall)) yline(20 40 60 80,lwidth(0.25) lcolor(gs11)) saving(t6, replace) plotregion(margin( b+3 t+3))  outergap(30) intensity(30) blabel(bar, bfcolor(black*0.2) position(center) format(%3.1f)) legend(row(3)) ytitle("Percentage of respondents") title(Reduce carbon footprint,position(9))
    
    grc1leg t1.gph t2.gph t3.gph t4.gph t5.gph t6.gph, cols(1) imargin(0 0 0 0) ycommon xcommon legendfrom(t6.gph)
    Click image for larger version

Name:	Screenshot 2023-01-28 at 10.00.28.png
Views:	1
Size:	87.0 KB
ID:	1699242

  • #2
    This is a case where fixing the legend is not the main problem at all, which is to make compatible graphs that were drawn in ignorance of each other. There is one good solution, to back up and redraw the graph from a different data layout. All other solutions are bad.

    I wouldn't use catplot (SSC) here and I don't have any bias against its author. Better choices are tabplot (Stata Journal) or floatplot. (SSC).

    Here is one possibility and there are many others. There is scope for changing the order of the columns and colours are at choice too.

    Also try xsc(alt) as an extra option.

    I typed in the numbers, so they need checking.

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input double question byte answer double percent
    1 1  5.2
    1 2  8.1
    1 3 14.4
    1 4 35.8
    1 5 36.5
    2 1    3
    2 2  4.8
    2 3 17.4
    2 4 36.5
    2 5 38.3
    3 1  2.8
    3 2  3.5
    3 3 10.5
    3 4 37.5
    3 5 45.8
    4 1  8.8
    4 2 11.1
    4 3 12.8
    4 4 33.7
    4 5 33.6
    5 1  5.6
    5 2   10
    5 3 18.1
    5 4 33.6
    5 5 32.8
    6 1  2.8
    6 2  3.5
    6 3 10.5
    6 4 37.5
    6 5 45.8
    end
    label values question question
    label def question 1 "Useful", modify
    label def question 2 "Affordable", modify
    label def question 3 "Reliable", modify
    label def question 4 "Can share", modify
    label def question 5 "Safe", modify
    label def question 6 `" "Reduce" "carbon" "footprint" "', modify
    label values answer answer
    label def answer 1 "Strongly disagree", modify
    label def answer 2 "Somewhat disagree", modify
    label def answer 3 "Neither", modify
    label def answer 4 "Somewhat agree", modify
    label def answer 5 "Strongly agree", modify
    
    floatplot answer [aw=percent], over(question) vertical centre(3) fcolors(red*0.7 red*0.3 white blue*0.3 blue*0.7) xtitle("") xla(, labsize(small)) lcolors(red red gs8 blue blue) textoffset(0.4) legend(symxsize(small))
    Click image for larger version

Name:	carbonfootprint.png
Views:	1
Size:	25.1 KB
ID:	1699252

    Last edited by Nick Cox; 28 Jan 2023, 10:24.

    Comment

    Working...
    X