Announcement

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

  • Help with modifying vertical bar graph issues

    I have the following which seems to be working just fine but requires modifying
    Code:
    graph bar percent, ///
        over(yesnodontknow_response, label(labsize(small)) gap(0)) /// Grouped bars
        over(disease_transmission,label(angle(0))) /// Diseases on y-axis
        bargap(0.5) /// Space between groups
        bar(1, color(blue)) bar(2, color(red)) bar(3, color(green)) ///
        title("Farmers Responses on Zoonotic Disease Transmission") ///
        ytitle("Percentage of Farmers (%)") ///
        blabel(bar, format(%3.1f) size(small)) /// Percentages inside bars
        legend(order(1 "Yes" 2 "No" 3 "Don't Know") size(small))
    I want to get rid of the yes no dont know appearing on the x axis, and also have different colors for the bars so that they appear as a legend. Is anyone able to help. Not sure what I am doing wrong. Thanks in advance Paul.

    Click image for larger version

Name:	Screenshot 2024-12-07 174837.png
Views:	1
Size:	199.0 KB
ID:	1768995

  • #2
    It's not a question of what you're doing wrong. There are rather three issues at least. Opinions follow as well as facts.

    1. You won't get different colours unless you ask for them.

    2. You don't have nearly enough space for your text labels to be anywhere near legible with that design.

    3. Wanting to add a legend and depart from direct labelling is a step in the wrong direction. A legend creates the need for mental back and forth, which direct labelling avoids.

    The only designs that have half a chance of working well have your 12 categories on the other axis.

    I mocked up a copy of your data, except that I blanked on trying to work out what the 12 categories are. That's much of the point.

    On a different level, note correct punctuation: Farmers' not Farmers; Don't know not Dont know.

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input float(percent which) byte response
     4  1 1
    12  2 1
     3  3 1
    17  4 1
     6  5 1
     2  6 1
    18  7 1
    25  8 1
    22  9 1
    33 10 1
    17 11 1
    24 12 1
    96  1 2
    83  2 2
    96  3 2
    74  4 2
    93  5 2
    93  6 2
    75  7 2
    50  8 2
    54  9 2
    61 10 2
    69 11 2
    57 12 2
     0  1 3
     5  2 3
     1  3 3
     9  4 3
     1  5 3
     5  6 3
     7  7 3
    25  8 3
    24  9 3
     6 10 3
    14 11 3
    19 12 3
    end
    label values which which
    label def which 1 "category 1", modify
    label def which 2 "category 2", modify
    label def which 3 "category 3", modify
    label def which 4 "category 4", modify
    label def which 5 "category 5", modify
    label def which 6 "category 6", modify
    label def which 7 "category 7", modify
    label def which 8 "category 8", modify
    label def which 9 "category 9", modify
    label def which 10 "category 10", modify
    label def which 11 "category 11", modify
    label def which 12 "category 12", modify
    label values response response
    label def response 1 "Yes", modify
    label def response 2 "No", modify
    label def response 3 "Don't know", modify
    
    
    separate percent, by(response) veryshortlabel
    
    graph hbar (asis) percent?, over(which) by(response, legend(off) note("") row(1)) nofill blabel(bar)
    Notes:

    0. The up and down nature of your bar layout doesn't make comparisons easy. The layout here is based on a guess on what comparisons are of most interest.

    1. The graph from the code above repeats the text category 1 etc twice more. I used the Graph Editor to remove that.

    2. You should consider whether the ordering of your categories is really optimal. Some related ideas are discussed at

    J-21-3 st0654 . . Speaking Stata: Ordering or ranking groups of observations
    (help myaxis if installed) . . . . . . . . . . . . . . . . N. J. Cox
    Q3/21 SJ 21(3):818--837
    discusses procedures for datasets based on aggregate
    frequencies and for datasets based on individuals and
    introduce a new convenience command, myaxis, that handles
    many cases directly


    3. You may want different colours.

    4. You will want additional titles.

    5. I rounded all percents to integers. Whether your extra fractions are needed or helpful for interpretation is your call.

    6. You perhaps don't need the magnitude axis if you show numeric values, which I do agree is helpful.

    Click image for larger version

Name:	farmers1.png
Views:	1
Size:	58.6 KB
ID:	1769000

    Comment


    • #3
      Hello Nick,

      Your proposed solution works perfectly fine. Thank you!

      Best regards,
      Paul

      Comment


      • #4
        Pushed a bit further with the final steps hiding various elements using the Graph Editor.

        Code:
        separate percent, by(response) veryshortlabel
        
        myaxis which2 = which, sort(mean percent1) descending 
        
        graph hbar (asis) percent?,  over(which2) ///
        by(response, title("Farmers' Responses on Zoonotic Disease Transmission") ///
        legend(off) note("Percent") row(1)) nofill blabel(bar)
        Click image for larger version

Name:	farmer2.png
Views:	1
Size:	60.2 KB
ID:	1769037

        Comment

        Working...
        X