Announcement

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

  • Challenged by a bar graph

    Hi Statalisters,

    I find myself frequently a bit stuck with Stata's deep graphing capacity. I am attempting to graph the prevalence of a condition (positive result for a Lassa fever ELISA test) by study village. I have data from around 8100 residents of rural Sierra Leone, and would like to show a bar graph of these values by village (n=26), but I'd also like to sort the villages into administrative districts (n=3). Each village should appear once and only once, within its respective district (which is not happening, see below). Within each administrative district, I may wish to sort villages - perhaps by prevalence (e.g., ascending), but I am not yet sure I want to do this, and I can't seem to get Stata to do much of these tasks for me. I'm attaching my data, and a few examples of my resultant graphs. Could someone help me:

    1. Sort the villages by administrative district. Each village should appear on the graph only once.
    2. Within each administrative district, sort the villages by prevalence
    3. Bonus question: how would you get Stata to show the prevalence proportion as a percentage? (i.e., 0.6 should be 60%)


    Code:
    graph hbar bl_serostatus_astmh, over(village_astmh) over(districtp) ytitle("LASV prevalence (probability)") saving(example_graph)
    graph export example_graph.png, replace
    Click image for larger version

Name:	example_graph.png
Views:	1
Size:	110.0 KB
ID:	1766426
    Attached Files

  • #2
    Aha! I had a google breakthrough, and it led me back to Statalist. I've figured out how to sort by prevalence: https://www.statalist.org/forums/for...while-using-by

    Belated thanks to Andrew. Still stuck on #1 and #3

    Comment


    • #3
      Here is some technique that may help.

      1. You're asked not to post .dta attachments (FAQ Advice #12). In any case contract yields a data example that can be posted easily.

      2. nofill is an incantation you're missing.

      3. I have used mylabels from the Stata Journal because I have a copy to hand, but you could just convert on the fly

      Code:
      yla(0 0.1 "10" 0.2 "20")
      and so on.


      Code:
      * Example generated by -dataex-. For more info, type help dataex
      clear
      input str9 districtp float bl_serostatus_astmh long village_astmh int _freq
      "Kenema"    0  3  73
      "Kenema"    0  4  90
      "Kenema"    0 10 159
      "Kenema"    0 13 216
      "Kenema"    0 14 323
      "Kenema"    0 21 147
      "Kenema"    0 30 219
      "Kenema"    0 32 160
      "Kenema"    0 35  43
      "Kenema"    0 37 338
      "Kenema"    1  3  71
      "Kenema"    1  4  63
      "Kenema"    1 10  91
      "Kenema"    1 13 196
      "Kenema"    1 14 528
      "Kenema"    1 21  43
      "Kenema"    1 30 142
      "Kenema"    1 32 167
      "Kenema"    1 35  29
      "Kenema"    1 37  88
      "Port Loko" 0  2 366
      "Port Loko" 0 29 300
      "Port Loko" 0 38 273
      "Port Loko" 1  2  35
      "Port Loko" 1 29  31
      "Port Loko" 1 38  40
      "Tonkolili" 0  1 250
      "Tonkolili" 0  6 174
      "Tonkolili" 0 16 147
      "Tonkolili" 0 17 175
      "Tonkolili" 0 18 162
      "Tonkolili" 0 19 252
      "Tonkolili" 0 20 177
      "Tonkolili" 0 22 216
      "Tonkolili" 0 24  80
      "Tonkolili" 0 25 730
      "Tonkolili" 0 27 234
      "Tonkolili" 0 28 249
      "Tonkolili" 0 33 209
      "Tonkolili" 1  1  68
      "Tonkolili" 1  6  32
      "Tonkolili" 1 16  63
      "Tonkolili" 1 17  57
      "Tonkolili" 1 18  49
      "Tonkolili" 1 19  50
      "Tonkolili" 1 20  53
      "Tonkolili" 1 22  34
      "Tonkolili" 1 24  50
      "Tonkolili" 1 25 302
      "Tonkolili" 1 27  47
      "Tonkolili" 1 28  31
      "Tonkolili" 1 33  33
      end
      label values bl_serostatus_astmh peerresult_
      label def peerresult_ 0 "Negative", modify
      label def peerresult_ 1 "Positive", modify
      label values village_astmh village_astmh
      label def village_astmh 1 "Bolyine", modify
      label def village_astmh 2 "Gbaria-Mamankie", modify
      label def village_astmh 3 "Gouma", modify
      label def village_astmh 4 "Guabu", modify
      label def village_astmh 6 "Karife", modify
      label def village_astmh 10 "Kotohun", modify
      label def village_astmh 13 "Kpetema", modify
      label def village_astmh 14 "Largo", modify
      label def village_astmh 16 "Mabathof", modify
      label def village_astmh 17 "Mafanta", modify
      label def village_astmh 18 "Magbodo", modify
      label def village_astmh 19 "Makgrube", modify
      label def village_astmh 20 "Makil-Bana", modify
      label def village_astmh 21 "Maleh", modify
      label def village_astmh 22 "Mamabo", modify
      label def village_astmh 24 "Masana", modify
      label def village_astmh 25 "Masonsingbi", modify
      label def village_astmh 27 "Mathan", modify
      label def village_astmh 28 "Mayan", modify
      label def village_astmh 29 "Minthormore", modify
      label def village_astmh 30 "Ngiehun", modify
      label def village_astmh 32 "Njagor", modify
      label def village_astmh 33 "Romangoro", modify
      label def village_astmh 35 "Selema", modify
      label def village_astmh 37 "Tobanda", modify
      label def village_astmh 38 "Yankoro", modify
      
      mylabels 0(10)60, myscale(@/100) local(yla)
      
      graph hbar bl_serostatus_astmh [fw=_freq], over(village_astmh, sort(1)) over(districtp) nofill ytitle("LASV prevalence (%)") name(example_graph, replace) yla(`yla') ysc(alt)
      Click image for larger version

Name:	sleone.png
Views:	1
Size:	62.6 KB
ID:	1766436



      Code:
      SJ-24-1 gr0092_1  . . . . . . . . . . . . . . . . Software update for mylabels
              (help nicelabels, mylabels, myticks if installed) . . . . .  N. J. Cox
              Q1/24   SJ 24(1):182--184
              fixes a bug that could bite if the options myscale() and
              clean were specified together
      
      SJ-22-4 gr0092  . . . . . . . . . . . . Speaking Stata: Automating axis labels
              (help nicelabels, mylabels, myticks if installed) . . . . .  N. J. Cox
              Q4/22   SJ 22(4):975--995
              provides commands to handle two common problems with graph
              axis labels: decide in advance on some "nice" numbers to
              use on one or both axes and show particular labels on some
              transformed scale
      Putting the y axis at the top is just a style choice, but one that to me works nicely for any graph with table flavour:

      Code:
      SJ-12-3 gr0053  . Speaking Stata: Axis practice, or what goes where on a graph
              (help multqplot if installed) . . . . . . . . . . . . . . .  N. J. Cox
              Q3/12   SJ 12(3):549--561
              discusses variations on what goes on each axis of a two-way
              plot; provides multiple quantile plots
      Different colours for each district can be got by


      Code:
      separate bl_serostatus_astmh, by(districtp) veryshortlabel
      
      graph hbar bl_serostatus_astmh? [fw=_freq], over(village_astmh, sort(1))  over(districtp) nofill ytitle("LASV prevalence (%)") name(example_graph, replace) yla(`yla') ysc(alt) legend(off)
      and there may well be other ways to do that.

      Comment


      • #4
        Unless there is some reason for using alphabetical order, here is one way to order the districts:

        Code:
        mylabels 0(10)60, myscale(@/100) local(yla)
        
        label define order 1 "Port Loko" 2 "Tonkolili" 3 "Kenema"
        encode districtp , gen(order) label(order)
        
        graph hbar bl_serostatus_astmh [fw=_freq], over(village_astmh, sort(1)) over(order) nofill ytitle("LASV prevalence (%)") name(example_graph, replace) yla(`yla') ysc(alt)

        Comment

        Working...
        X