Announcement

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

  • Overlay spline curve over frequency histrogram

    I have a variable X which is associated with outcome Y. I want to plot the association of X with Y, and in the background show the distribution of variable X. Please see an example for another publication. I Click image for larger version

Name:	Capture.JPG
Views:	2
Size:	19.4 KB
ID:	1731567 appreciate your guidance.

  • #2
    Code:
    clear all
    sysuse auto, clear
    mkspline wght=weight, cubic
    reg price wght* i.foreign i.rep78
    
    frame copy default tograph
    frame change tograph
    
    predictnl yhat = _b[_cons] +       ///
                     _b[wght1]*wght1 + ///
                     _b[wght2]*wght2 + ///
                     _b[wght3]*wght3 + ///
                     _b[wght4]*wght4 , ci(lb ub)
    
    twoway__histogram_gen weight , percent gen(h x)
    
    twoway bar h x, barw(`r(width)') bcolor(%50) yaxis(1) yscale(alt axis(1)) || ///
           rarea lb ub weight, sort astyle(ci) acolor(%50) yaxis(2)|| ///
           line yhat weight, sort legend(off) yaxis(2) yscale(alt axis(2)) ///
           ytitle(predicted price, axis(2)) ylabel(,format(%9.0gc) axis(2))
    Click image for larger version

Name:	Graph.png
Views:	1
Size:	98.5 KB
ID:	1731591
    ---------------------------------
    Maarten L. Buis
    University of Konstanz
    Department of history and sociology
    box 40
    78457 Konstanz
    Germany
    http://www.maartenbuis.nl
    ---------------------------------

    Comment


    • #3
      Maarten Buis gave an excellent answer explaining how to do what you asked. If I were a reviewer, I would suggest instead two graphs with the same x axis. I can't see any benefit from superimposing the graphs and there is scope for some confusion.

      Comment

      Working...
      X