Announcement

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

  • multiple lines graph with if conditions

    Hi everyone,

    I have been trying to plot a mulitple lines graph similar to the attached picture. After many failed attempts and looking on the Internet, I would like to see if anybody here could help.

    My data is a longitudinal dataset. I would like to create a graph by only picking certain years. I got the error: ) required r(100); by running the below code


    Code:
    twoway (line pc_foreign num_exp if jahr==1985,sort by(edu)legend("1985"))
        (line pc_foreign num_exp if jahr==1995,sort by(edu)legend("1995"))
        (line pc_foreign num_exp if jahr==2005,sort by(edu)legend("2005"))
        (line pc_foreign num_exp if jahr==2014,sort by(edu)legend("2014"))
    ytitle("share")
    xtitle("Num experience");
    Would appreciate if anyone could help.

    Thanks a lot.
    Attached Files

  • #2
    There is no data example here for us to work with (please read FAQ Advice especially #12), but advice is possible.

    First off, the legend() option calls are quite illegal. See the help to learn that legend() wants suboptions, never pure text.

    Something like this may take you closer to what you want:

    Code:
    separate pc_foreign, by(jahr) veryshortlabel 
    line pc_foreign? num_exp, sort by(edu) ytitle("share") xtitle("Num experience")
    If you have years other than 1984 1994 2004 2014, you'll need different code, but as above you should tell us more about your data.

    See http://www.stata-journal.com/sjpdf.h...iclenum=gr0023 on the veryshortlabel option of separate.

    Comment


    • #3
      Thank you for your reply. The data consists of yearly data from 1984 to 2014.

      Below is some sample of my data. I have data in each year from 1984 to 2014. Therefore I used the if condition to display only certain years.
      In the graph, personal ID is not important beacuse the share (pc_foreign) is an aggregate therefore they should have the same share if they belong to the same Group.

      Because of sensitivity issue, the share here is all 1. In my data, different num. of experience & education group have different shares.

      Code:
      * Example generated by -dataex-. To install: ssc install dataex
      clear
      input int jahr long persnr float pc_foreign byte(num_exp edu)
      1975 880018  1  0 4
      1975 883060  1  0 2
      1976 880018  1  1 4
      1976 883060  1  1 2
      1977 880018  1  2 4
      1977 883060  1  2 2
      1978 880018  1  3 4
      1978 883060  1  3 2
      1979 880018  1  4 4
      1979 883060  1  4 2
      1980 880018  1  5 4
      1980 883060  1  5 2
      1981 883060  1  6 2
      1991 883060  1 16 2
      1992 883060  1 17 2
      1993 883060  1 18 2
      1994 883060  1 19 2
      1995 883060 .5 20 2
      1997 883060  1 22 2
      1998 883060  1 23 2
      1999 883060  1 24 2
      end
      label values edu education
      label def education 2 "High school", modify
      label def education 4 "University", modify
      I hope that helps clarify my question.

      Thanks a lot

      Comment


      • #4
        If you just want those four years then a condition like

        Code:
        if inlist(jahr, 1984, 1994, 2004, 2014)
        or

        Code:
        if mod(jahr, 10) == 4
        on the separate command should suffice to pin that down. Otherwise I am now not sure what you are asking. You've presented

        1. a graph command that didn't work

        2. a graph but without any code to produce it

        3. some data that aren't quite realistic (even on a individual level, when you want summaries of some kind).

        Why not try my code with an if condition or work out from your own knowledge of the data what needs to be changed?

        Comment

        Working...
        X