Announcement

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

  • Add trend line to a grouped bar graph?

    I'm wanting to add 1 or more trend lines (lfit?) to bar graph with 3 variables over month. Here's current graph code:

    Code:
    graph bar (asis) hemvol ischemicvol totvol if year == 2019, over(date, relabel(1 "January"
    2 "February" 3 "March")) blabel(bar, size(small)) bar(1, color(maroon)) bar(2, color(navy))
    bar(3, color(pink)) ytitle(, size(vsmall)) ylabel(0(10)120, labsize(tiny) angle(horizontal)
    glcolor(gs14) gmax) ymtick(##2) legend(col(3) size(small)) graphregion(fcolor(gs14))
    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input float(date hemvol ischemicvol totvol)
    708 19 75  94
    709 15 75  90
    710 18 87 105
    end
    format %tmMonth_CCYY date
    Click image for larger version

Name:	volgraph.png
Views:	1
Size:	35.0 KB
ID:	1494167

    Any ideas or fixes?

    Side issue: date labels generate as 708, etc forcing the code for relabel. Any ideas why it won't label the graph with the appropriate format?

  • #2
    You need twoway bar if you want to combine with a line chart. Then the date format can be exploited.

    In fact, a line chart would be my choice here. Here is some indicative code (which works).

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input float(date hemvol ischemicvol totvol)
    708 19 75  94
    709 15 75  90
    710 18 87 105
    end
    format %tmMonth_CCYY date
    
    gen t = "tot"
    gen h = "hem"
    gen i = "ischemic" 
    
    twoway line hemvol ischemicvol totvol date, ylabel(0(10)120, angle(horizontal) /// 
    glcolor(gs14) gmax) graphregion(fcolor(gs14)) xla(708/710) legend(off) /// 
    lc(orange blue black) xsc(r(. 710.3)) || ///
    scatter totvol date if date == 710, ms(none) mla(t) mlabcolor(black) ///
    || scatter hemvol date if date == 710, ms(none) mla(h) mlabcolor(orange) ///
    ||scatter ischemic date if date == 710, ms(none) mla(i) mlabcolor(blue)

    Comment


    • #3
      So I changed to a twoway bar (comparing visually with/without lfit - not in code below). Is there a way to offset/stagger the bars for each month on the x-axis (similar to bargap) to avoid perfectly stacked bars?

      Code:
      twoway (bar totvol date, sort fcolor(pink) fintensity(90)lcolor(pink) barwidth(.9)) ///
      (scatter totvol date, msymbol(none) mlabel(totvol) mlabsize(small) mlabcolor(black) mlabpos(12)) ///
      (bar ischemicvol date, sort fcolor(navy) fintensity(90) lcolor(navy) barwidth(.9)) ///
      (scatter ischemicvol date, msymbol(none) mlabel(ischemicvol) mlabsize(small) ///
      mlabcolor(black) mlabpos(12)) (bar hemvol date, sort fcolor(maroon) fintensity(90) ///
      lcolor(maroon) barwidth(.9)) (scatter hemvol date, msymbol(none) mlabel(hemvol) ///
      mlabsize(small) mlabcolor(white) mlabpos(12)), ytitle(, size(vsmall)) ylabel(0(10)120, ///
      labsize(tiny) angle(horizontal) glcolor(gs14) gmax) ymtick(##2) xtitle("") xlabel(#3, ///
      angle(horizontal) labsize(small) format (%tmMonth)) legend(off) graphregion(fcolor(gs14))

      Comment


      • #4
        Code:
        . search offset, sj
        
        Search of official help files, FAQs, Examples, SJs, and STBs
        
        SJ-7-1  gr0026  . . . .  Stata tip 42: The overlay problem: Offset for clarity
                . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . J. Cui
                Q1/07   SJ 7(1):141--142                                 (no commands)
                tip for graphing several quantities on a continuous axis
        
        (end of search)
        Repeating the command in Stata will give you a clickable link to a .pdf of the Tip.

        Comment

        Working...
        X