Announcement

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

  • Solving graphing errors in "graph bar" using "over"

    Click image for larger version

Name:	Hospital.png
Views:	1
Size:	69.8 KB
ID:	1704814

    I'm having data graphing incorrectly with graph bar when I use:

    graph bar (sum) temp2, over(catfinal) over(hospital, label(labsize(small))) asy stack nofill ///
    title("Influenza Samples Tested by Hospital - Vietnam, 2023", size(medlarge)) ytitle("Number of Samples Tested") ///
    note("As of `c(current_date)'")

    the graph incorrectly labels the names of the hospitals since the data if you tab hospital by catfinal:
    catfinal
    Negative Influenza A Influenza B Influenza A&B Total
    hospital
    Bach Mai 0 3 1 0 4
    Viet Tiep 37 14 3 0 54
    VNCH 9 12 1 0 22
    Trop Dis Hosp 7 2 0 0 9
    VN Swedish 12 29 7 4 52
    Total 65 60 12 4 141
    Attached Files

  • #2
    Please don't post .dta attachments (FAQ Advice #12).

    I confirm your problem. There should always be reluctance to suggest a bug, but I suspect a subtle bug in graph bar (sum) as it is evidently not handling well the zeros for Cho Ray. I would send the dataset to StataCorp technical services.

    The data you are plotting can be shown by the results of contract catfinal hospital if temp2 and so showing that as a data example removes any need for an attachment.

    I don't think the stacked bar design works at all well when there are several zeros or other very small counts, or indeed usually. In particular, the usual design commits the reader to a mental back and forth between the graph and its legend. Direct labelling is preferable. So, I used tabplot from the Stata Journal. There is a sales pitch at https://www.statalist.org/forums/for...updated-on-ssc and the most important references selected from the results of

    Code:
    search tabplot, sj
    are (1) the most recent as where to download the software (2) the 2016 paper.

    Code:
    SJ-22-2 gr0066_3  . . . . . . . . . . . . . . . .  Software update for tabplot
            (help tabplot if installed) . . . . . . . . . . . . . . . .  N. J. Cox
            Q2/22   SJ 22(2):467
            bug fixed; help file updated to include further references
    
    
    SJ-16-2 gr0066  . . . . . .  Speaking Stata: Multiple bar charts in table form
            (help tabplot if installed) . . . . . . . . . . . . . . . .  N. J. Cox
            Q2/16   SJ 16(2):491--510
            provides multiple bar charts in table form representing
            contingency tables for one, two, or three categorical variables

    Here is the code I used following a
    contract as mentioned above.

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input long hospital float catfinal byte _freq
    3 0 37
    4 0  9
    5 0  7
    6 0 12
    1 1  3
    3 1 14
    4 1 12
    5 1  2
    6 1 29
    1 2  1
    3 2  3
    4 2  1
    6 2  7
    6 3  4
    end
    label values hospital hospital
    label def hospital 1 "Bach Mai", modify
    label def hospital 3 "Viet Tiep", modify
    label def hospital 4 "VNCH", modify
    label def hospital 5 "Trop Dis Hosp", modify
    label def hospital 6 "VN Swedish", modify
    label values catfinal catfinal
    label def catfinal 0 "Negative", modify
    label def catfinal 1 "Influenza A", modify
    label def catfinal 2 "Influenza B", modify
    label def catfinal 3 "Influenza A & B", modify
    
    set scheme s1color
    
    local OK1 230 159 0
    local OK2 86 180 233
    local OK3 0 158 115
    local OK4 240 228 66
    
    
    tabplot catfinal hospital [w=_freq] , showval yasis ytitle("") yla(, tlc(none)) subtitle(#cases) xasis xla(1 3 4 5 6 2 "Cho Ray", valuelabel noticks labsize(small)) name(G1, replace) separate(catfinal) bar1(color("`OK1'")) bar2(color("`OK2'")) bar3(color("`OK3'")) bar4(color("`OK4'")) xsc(titlegap(*1.2))
    
    
    graph bar (asis) _freq, over(catfinal) over(hospital) asyvars stack name(G2, replace)
    With
    tabplot it was possible to insist on adding an axis label for Cho Ray. The second command shows that you can get reasonable results with graph bar (asis) which leads to suspect the problem mentioned above. Getting graph bar to show anything for Cho Ray would be harder work, I guess.

    The colours are naturally at choice. I used a quite often suggested palette: more at https://www.statalist.org/forums/for...ailable-on-ssc

    Click image for larger version

Name:	influenza_G1.png
Views:	1
Size:	16.8 KB
ID:	1704826

    Click image for larger version

Name:	influenza_G2.png
Views:	1
Size:	17.6 KB
ID:	1704827




    I didn't work hard at the colours for the second graph, as I don't think it's better. I think that s2color is better than s1color but I find the default colours too lurid for many purposes.

    I hope that helps.
    Last edited by Nick Cox; 08 Mar 2023, 02:16.

    Comment

    Working...
    X