Announcement

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

  • graph bar with numeric x variable

    I'm using -graph bar- to graph a couple of percentages against the year they were observed. -graph bar- treats the year like a categorical variable, but I would like to treat it as numeric, because the years are not evenly spaced. (There are 4 years between most observations, and then 6 between the last 2.) Is there a variant of graph bar that will treat the x variable as numeric and space it accordingly? Thanks!

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input float startyear double(pctyearroundsingletrack pctyearroundmultitrack)
    2000 3.6 2.4999999999999996
    2003 4.3 1.2000000000000002
    2007 4.4 1.0999999999999996
    2011 3.2  .8999999999999995
    2017 2.5                 .5
    end
    
    graph bar pctyearroundsingletrack pctyearroundmultitrack , over(startyear)

  • #2
    Yes,in the sense that it is twoway bar. For the offset technique used here, see (e.g.) https://www.stata-journal.com/articl...article=gr0026

    Code:
    clear
    input float startyear double(pctyearroundsingletrack pctyearroundmultitrack)
    2000 3.6 2.4999999999999996
    2003 4.3 1.2000000000000002
    2007 4.4 1.0999999999999996
    2011 3.2  .8999999999999995
    2017 2.5                 .5
    end
    
    twoway bar pctyearroundsingletrack startyear, fcolor(none) || bar pctyearroundmultitrack  startyear, fcolor(none)
    
    set scheme s1color
    
    gen startyear1 = startyear - 0.4
    gen startyear2 = startyear + 0.4
    
    twoway bar pctyearroundsingletrack startyear1, barw(0.8) color(orange) || bar pctyearroundmultitrack  startyear2, barw(0.8) color(blue) xla(2000 2003 2007 2011 2017) legend(ring(0) pos(1) col(1) order(1 "single track" 2 "multitrack")) ytitle(% of something) xtitle(start year) yla(, ang(h))

    Comment

    Working...
    X