Announcement

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

  • Line Graph from multiple subgroups in same graph NOT using graph combine

    Hello All,

    I am relatively new to this forum. I am trying to do something that simple in excel but is evidently difficult in STATA. I am trying to use output from two subgroups in the same line graph. Graph combine is NOT the command that I wish to use. Let me explain. After searching the forum, I have realized that graph combine merely adds multiple graphs into 1 figure. Hypothetically, with two subgroups, graph combine would produce 1 figure with 2 graph panes. My needs are different. I need ONE graph pane with 2 lines from the two subgroups. Here is the code that I am trying. I also need to keep the "if race==1" type statements.


    // prepare example data
    sysuse nlsw88, clear
    gen lnwage = ln(wage)

    // collect means and standard deviations
    mean lnwage if race == 1, over(tenure)
    local m_w = r(mean)
    local sd_w = r(sd)
    mean lnwage if race == 2, over (tenure)
    local m_b = r(mean)
    local sd_b = r(sd)

    // plot various
    twoway line white = normalden(x,`m_w',`sd_w') , range(0 4) || ///
    line black = normalden(x,`m_b',`sd_b') , range(0 4)


    Keep in mind, I am new to STATA graphics.
    I haven't really posted on this forum much, so I apologize in advance for any missteps in my post.





  • #2
    Please read the FAQ Advice https://www.statalist.org/forums/help especially for helpful advice in #12 on presenting code and results (and indeed all the way up to #18).

    Thanks for showing code, but what you give us seems confused.

    0. As you say graph combine is not applicable to your problem so why mention it at all?

    1. The command mean does not leave behind r(mean) or r(sd). It is an e-class command.

    2. Calculating over(tenure) may be an echo of your real problem, which is more complicated, but here the essence of giving that option is to return means for each level of tenure, not an overall mean and SD as you seek.

    3. You don't flag that your twoway command just doesn't work, the first reason being that you are confusing twoway line and twoway function. The second reason is that those local macros will just contain missing values.

    This may help:

    Code:
    sysuse nlsw88, clear
    gen lnwage = ln(wage)
    gen log10wage = log10(wage)
    
    // collect means and standard deviations
    su lnwage if race == 1
    local m_w = r(mean)
    local sd_w = r(sd)
    su lnwage if race == 2
    local m_b = r(mean)
    local sd_b = r(sd)
    
    set scheme s1color
    
    // plot various
    twoway function white = normalden(x,`m_w',`sd_w') , range(0 4) || ///
    function black = normalden(x,`m_b',`sd_b') , range(0 4) name(G1, replace)
    Click image for larger version

Name:	higgins1.png
Views:	1
Size:	25.9 KB
ID:	1518181


    I would do something quite different. Here I use mylabels (SSC) and qplot (Stata Journal). We keep the idea that the lognormal is a convenient reference distribution, but plot the data. Note how the fit is not so good at the top end.

    Code:
    mylabels 1 2 5 10 20 50, myscale(log10(@)) local(ylabel)
    qplot log10wage if inlist(race, 1, 2), over(race) yla(`ylabel', ang(h)) ytitle(wage (log scale)) ///
    xtitle(standard normal deviate) trscale(invnormal(@)) xla(-3/3) name(G2, replace) legend(ring(0) col(1) pos(5))
    Click image for larger version

Name:	higgins2.png
Views:	1
Size:	31.8 KB
ID:	1518182

    Last edited by Nick Cox; 27 Sep 2019, 11:30.

    Comment


    • #3
      I apologize for the delay. Thank you Nick. This does help.

      Comment

      Working...
      X