Announcement

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

  • Twoway graphs: Connecting dots across by() groups

    Dear Statalist

    I would like to make a figure that looks like this:

    Click image for larger version

Name:	statalist.png
Views:	1
Size:	14.3 KB
ID:	1435007


    But obviously the red line I drew in MS Paint, because I couldn't get Stata to do it. Is there a way to get such a figure in Stata?

    This is the code (including data) I got so far:

    Code:
    version 14
    clear
    
    set obs 1000
    generate x1 = _n
    recode x1 (   1/200 = 1 "Year 1") ///
              ( 201/400 = 2 "Year 2") ///
              ( 401/600 = 3 "Year 3") ///
              ( 601/800 = 4 "Year 4") ///
              (801/1000 = 5 "Year 5"), gen(x)
    drop x1
    
    generate y = rnormal(x, 1)
    bys x: egen avgy = mean(y)
    
    gen dot = 0
    
    twoway (histogram y, horizontal percent by(x) ///
                         by(, note("") row(1) imargin(zero)) lcolor(black) ///
                         lwidth(thin) plotregion(margin(zero)) width(1) subtitle(, size(*1))) ///
           (connect avgy dot, by(x, legend(off)) msymbol(O) mcolor(black) msize(large)), xlabel(none)

    Thanks a lot!

    Go

  • #2
    You can do this with tabplot from the Stata Journal, which must be installed first.

    SJ-17-3 gr0066_1 . . . . . . . . . . . . . . . . Software update for tabplot
    (help tabplot if installed) . . . . . . . . . . . . . . . . N. J. Cox
    Q3/17 SJ 17(3):779
    added options for reversing axis scales; improved handling of
    axis labels containing quotation marks

    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


    Code:
    clear
    set seed 42 
    set obs 1000
    generate x1 = _n
    recode x1 (   1/200 = 1 "Year 1") ///
              ( 201/400 = 2 "Year 2") ///
              ( 401/600 = 3 "Year 3") ///
              ( 601/800 = 4 "Year 4") ///
              (801/1000 = 5 "Year 5"), gen(x)
    drop x1
    
    generate y = rnormal(x, 1)
    bys x: egen avgy = mean(y)
    
    gen Y = round(y, 1) 
    set scheme s1color 
    tabplot Y x, horizontal barw(1) bfcolor(none) yasis ytitle(y, orient(horiz)) addplot(connect avgy x, sort)

    Click image for larger version

Name:	tabplot_new.png
Views:	1
Size:	27.5 KB
ID:	1435013

    Comment


    • #3
      Thank you very much!

      Go

      Comment

      Working...
      X