Announcement

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

  • Graph bar issues

    I am having trouble creating the following graph with STATA 12. This graph was made using excel




    I would really appreciate some guidance as I am not very experienced with STATA graphics. I have attempted to make the code however the data does not match and I am unable to make the intervals exactly the same starting with 1 and forcing the x axis (epi_week variable ranging from 1-53 for 2013,2014,2015,2016) even when no data is there. STATA completely restarts the count 1-53 by year

    gen Alive= final_outcome==1
    gen Alive_only=.
    replace Alive_only=1 if Alive==1
    gen Deceased= final_outcome==2
    gen Deceased_only=.
    replace Deaceased_only=1 if Deceased==1

    sort epi_year epi_week

    graph bar (count) Alive_only Deceased_only , over(epi_week, label(angle(90) labsize(*0.4)tick)) over(epi_year) stack bar(1, blwidth(thin) bcolor(blue)) bar(2, bcolor(red)) legend (label(1 "Alive") label(2 "Deceased"))ylabel(,nogrid)graphregion(color(white ))

    graph display, xsize(10)



    Excel graph Stata graph

  • #2
    I'm having some trouble with seeing your graph examples and understanding how some of your example code is relevant/related to your question, but here is a MWE using fake data to generate the graph you are describing (see in-line notes below):



    Code:
    **create fake data 
    clear
    set obs 2000
    g epi_week = int(runiform()*53+1)
    g epi_year = 2013+int((4)*runiform())
    su epi*
    foreach x in alive deceased {
        cap drop `x'_only
        g `x'_only = rbinomial(1, .55)
        }
        
    **from post:
    
    sort epi_year epi_week
    
    *just keep 1 obs per week/year  (this is what I'm guessing you did, may not match your data)
    collapse (sum) *_only, by(epi_year epi_week)
    
    **ts
    tsset epi_week epi_year, weekly //note that there are some gaps
    ta epi_week epi_year
    
    tsfill, full
    ta epi_week epi_year
    recode epi_* (.=0)
    
    **note change to _asis_, not count, I left the other options alone
    graph bar (asis) alive_only deceased_only , over(epi_week, label(angle(90) labsize(*0.4)tick)) over(epi_year) stack bar(1, blwidth(thin) bcolor(blue)) bar(2, bcolor(red)) legend (label(1 "Alive") label(2 "Deceased")) ylabel(,nogrid)graphregion(color(white ))
    
    graph display, xsize(10)
    
    graph export ex.png, as(png) replace



    This produces :
    Click image for larger version

Name:	ex.png
Views:	1
Size:	51.6 KB
ID:	1322614




    _____
    - Eric Booth
    [email protected]
    Eric A. Booth | Senior Director of Research | Far Harbor | Austin TX

    Comment


    • #3
      Thank you so much! The data matches with the corresponding week and year. Now the next thing I need to figure out is how i can make the data show (epi_week) in intervals of 3 like the excel graph attached at the bottom of my post under photos ( for some reason you can't view it within the test, you will need to press on photos). Is there a simple way of doing this?

      Comment


      • #4
        You are asking about changing the x labels in your bar chart so that some labels are not shown (every 4th are shown in your example).
        You can do this with the relabel() sub option of the over() option in your -graph bar- command.
        The trick is you don't want to have to manually type out the option for all of : relabel(1 "1" 2 "" 3"" 4 "" 5 "5" ....... for all data points, so use a loop to build your option and store it in a macro.

        Check out the updated example below:


        Code:
        **create fake data 
        clear
        set obs 2000
        g epi_week = int(runiform()*53+1)
        g epi_year = 2013+int((4)*runiform())
        su epi*
        foreach x in alive deceased {
            cap drop `x'_only
            g `x'_only = rbinomial(1, .55)
            }
            
        **from post:
        
        sort epi_year epi_week
        
        *just keep 1 obs per week/year  (this is what I'm guessing you did, may not match your data)
        collapse (sum) *_only, by(epi_year epi_week)
        
        **ts
        tsset epi_week epi_year, weekly //note that there are some gaps
        ta epi_week epi_year
        
        tsfill, full
        ta epi_week epi_year
        recode epi_* (.=0)
        
        
        **new:::   fix labels with the relabel option (stored in global macro)
        global relabel ""
        su epi_week , d
        forval n = 1(4)`r(max)'{
            di `"`n'"'
            global relabel `"${relabel} `=`n'+1' " " `=`n'+2' " " `=`n'+3' " " "'
            }
            di `"${relabel}"'  //make sure this is what you want, if not adjust above
            
            
        **new: added relabel to over() option::
        graph bar (asis) alive_only deceased_only , over(epi_week, label(angle(90) labsize(*0.6)tick) relabel($relabel ) ) over(epi_year) stack bar(1, blwidth(thin) bcolor(blue)) bar(2, bcolor(red)) legend (label(1 "Alive") label(2 "Deceased")) ylabel(,nogrid)graphregion(color(white ))
        
        graph display, xsize(10)
        
        graph export ex.png, as(png) replace
        ____
        - Eric Booth
        [email protected]




        Eric A. Booth | Senior Director of Research | Far Harbor | Austin TX

        Comment


        • #5
          I can't thank you enough! This is exactly what I was looking for

          Comment


          • #6

            Hi Eric Booth,

            I ended up looking more closely at the x axis and it looks like it didn't do extactly what it was supposed to.... when I do tsset epi_week epi_year, weekly, there is no gap identified, it provides me with the following output

            tsset epi_week epi_year, weekly
            panel variable: epi_week (unbalanced)
            time variable: epi_year, 1998w38 to 1998w41
            delta: 1 week

            When I do the next step- ta epi_week epi_year ( I notice a few epi_week numbers missing in between (ie. 27, 30, 32,33,34,35,36, and 40)

            Even when I do the subsequent step- tsfill, full ( it is not corrected).

            Therefore the labels on the graph are incorrect and don't actually follow the relabel macro because it using numbers with the gaps not identified.

            Would you happen to know why this is actually


            All codes used:

            **new::: fix labels with the relabel option (stored in global macro)
            global relabel ""
            su epi_week , d
            forval n = 1(4)`r(max)'{
            di `"`n'"'
            global relabel `"${relabel} `=`n'+1' " " `=`n'+2' " " `=`n'+3' " " "'
            }
            di `"${relabel}"'


            ***All years epicurve by total cases***
            sort epi_year epi_week
            preserve
            collapse (sum) case, by(epi_year epi_week)
            tsset epi_week epi_year, weekly
            ta epi_week epi_year
            tsfill, full
            ta epi_week epi_year
            recode epi_* (.=0)
            graph bar (asis) case , over(epi_week, label(angle(90) labsize(*0.6)tick) relabel($relabel ) ) over(epi_year, label(labsize(*0.6))) ytitle ("Number of cases", size(small) height(4)) bar(1, blwidth(Mediumthick) bcolor(blue))ylabel(,nogrid)graphregion(color(whit e)) caption ("onset week and year", position(6) size(small)) ylabel(0(5)50, labsize(*0.6))
            graph display, xsize(10)
            graph export ex.png, as(png) replace







            Comment

            Working...
            X