Announcement

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

  • Ovelay two histograms (with just the normal plots)

    Hi,

    I am trying to overlay two graphs. (Stata version 13)

    Graph 1:

    Code:
    histogram depvar if (cat==0), normal lcolor(white) fcolor(white) xtitle("X variable") title("Results - Cat=0") legend(off) addplot(pci 0 0 8 0, lpattern(shortdash) lcolor(black))
    As you can see, I am completely removing the histogram bars and only the normal density plot is visible (with a line going through the '0' value (mean) added via addplot()).

    The second graph is the same, but for cat==1

    Code:
    histogram depvar if (cat==1), normal lcolor(white) fcolor(white) xtitle("X variable") title("Results - Cat=1") legend(off) addplot(pci 0 0 8 0, lpattern(shortdash) lcolor(black))
    How do I combine both, so that the normal curves appear on the same graph rather than one by one.
    Also, please suggest how to make one 'solid' and other 'dashed' with some legend.

    Thanks in advance.

  • #2
    There is no need to draw a histogram only to remove the bars. If you want two normal density function curves, you can do that directly.

    Code:
    sysuse auto, clear 
    su mpg if foreign == 0 
    local m0 = r(mean) 
    local SD0 = r(sd) 
    su mpg if foreign == 1
    local m1 = r(mean) 
    local SD1 = r(sd) 
    twoway function normden(x, `m1', `SD1'), ra(mpg) lp(solid) || /// 
    function normden(x, `m0', `SD0'), ra(mpg) lp(dash) ///
    legend(order(1 "Foreign" 2 "Domestic")) ytitle(Density) xtitle("`: var label mpg'")
    Your titles for your real problem are your concern but I am surprised to see anything called depvar being called an X variable.

    Comment


    • #3
      Thanks Nick!
      Good catch, I wrote 'X Variable' in title by mistake (as it is xtitle).

      Comment


      • #4
        Hi NIck,

        When I run the code, I get the following error:

        Code:
        error in expression: normden(x, 24.77272727272727, 6.611186898567625)
        r(133);
        This error came up when I used the auto dataset.

        Kindly let me know what has to be done.
        Thanks.

        Comment


        • #5
          No problem. Got it sorted out.
          Two issues:
          mpg instead of x
          normalden instead of normden

          Thanks.

          Comment


          • #6
            I was using Stata 10 on an old computer. normden() should work under version control. But you do need x in twoway function.

            Comment

            Working...
            X