Announcement

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

  • Drawing graphs to show subsets of sample

    Would be grateful for some advice on how to add an extra layer of code to this existing one below if I wanted to include an additional condition (would it be an if statement?) e.g. show the different lines to indicate Female and Male, or level of education (which is 4 categories). The code shows both genders and I coded gender as a binary variable (female = 0 and male =1). Another graph I want to show is difference in level of education where 0 is no high school, 1 is high school, 2 is undergrad and 3 is postgrad. Thank you.

    collapse (mean) Fin_Distress, by(subfin)
    twoway connected Fin_Distress subfin, mlabel(Fin_Distress)xlabel(1(1)7) legend(off) plotregion(margin(large)) xtitle(Subjective Knowledge)

  • #2
    Code:
    // open example data
    sysuse nlsw88, clear
    
    // prepare the data
    
    gen byte marst = !never_married + married if !missing(never_married)
    label variable marst "marital status"
    label define marst 0 "never married"    ///
                       1 "widowed/divorced" ///
                       2 "married"
    label value marst marst
    
    gen byte edcat = cond(grade <  12, 1,     ///
                     cond(grade == 12, 2,     ///
                     cond(grade <  16, 3,4))) ///
                     if !missing(grade)
    label variable edcat "respondent's education"
    label define edcat 1 "< highschool"    ///
                       2 "highschool"      ///
                       3 "some college"    ///
                       4 "college"            
    label value edcat edcat
    
    gen byte highoc = occupation < 3 if !missing(occupation)
    label variable highoc "high occupation"
    label define highoc 1 "higher" ///
                        0 "lower"
    label value highoc highoc
    
    // make the equivalent of your graph
    preserve
    collapse (mean) hours , by(edcat)
    format hours %9.1f // limit the number of decimal places shown
    twoway connected hours edcat, mlab(hours) xlab(1/4) legend(off) plotregion(margin(large))
    restore
    
    // make the graph separated by highoc
    preserve
    collapse (mean) hours , by(edcat highoc)
    format hours %9.1f
    separate hours, by(highoc) veryshortlabel
    twoway connected hours0 hours1 edcat, mlab(hours hours) mlabpos(12 12) xlab(1/4) plotregion(margin(large))
    restore
    ---------------------------------
    Maarten L. Buis
    University of Konstanz
    Department of history and sociology
    box 40
    78457 Konstanz
    Germany
    http://www.maartenbuis.nl
    ---------------------------------

    Comment


    • #3
      GenderDiffHHIncome.gph
      Dr Buis
      Thank you for the tutorial. I applied it to my own dataset and it worked. However, I would like to make it "look" better and I know I just need a few more lines of code. Can you help please? See attached image. Instead of 0 and 1, I would like to say Male Female on x-axis. On the y-axis is the DV: Financial Stress which is ranked from 1-7. So instead of it showing up as 2.5 as the starting point to 4.5 at the end of the y-axis, is it possible to start at 1 and end at 7? Thank you for your kind advice. Lena

      Comment


      • #4
        Hi Lena
        Perhaps this will help
        Code:
        scatter price foreign if price<10000, ylabel(0 (3000)15000) xlabel(0 "Domestic" 1 "foreign")

        Comment

        Working...
        X