Announcement

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

  • Two way graph with multiple lines in a loop

    Hello everyone, Iam trying to do a graph of multiple lines. What I want is a graph that shows in a same graph diffmain, diff4,diff5,diff21 and diff34 as shown in the image. Iam trying to do it with a loop but it does not work, anyone has an idea of how to run it?

    Code:
    levelsof state if (state ==4|state ==5|state==21|state==34), local(levels) 
    4 5 21 34
    
    use "PS2_data", clear
    levelsof state if (state ==4|state ==5|state==21|state==34), local(levels) 
    use "total_results34",clear
     
      foreach l of local levels {
      drop if rmspe`l'>=2*rmspemain
      twoway (line diffmain _time,color(black)), name(gmain,replace)
      twoway ((line diff`l' _time,color(gray)), xline(1989,lcolor(gray) lpattern(dash)) yline(0,lcolor(gray) lpattern(dash))), name(g`l',replace)
      graph combine gmain g`l'
     }


    Code:
     _time rmspemain diffmain rmspe4 diff4 rmspe5 diff5 rmspe21 diff21 rmspe34 diff34)
    1970 1.954279   4.857199 2.680468   7.661901 2.1965363   6.316099 3.5513296    10.3319 3.185257   9.161399
    1971 1.954279  1.8468013 2.680468   4.467302 2.1965363   1.678701 3.5513296     4.8542 3.185257  4.4762006
    1972 1.954279  -.8392038 2.680468   2.714297 2.1965363   .7133963 3.5513296  -2.419502 3.185257  -2.767302
    1973 1.954279 -2.0618958 2.680468  2.5514026 2.1965363  -1.788496 3.5513296 -1.9223986 3.185257 -1.2888986
    1974 1.954279  -.3828028 2.680468  1.7265977 2.1965363  -.2369025 3.5513296  -.5969042 3.185257   .1664961
    1975 1.954279   .5749984 2.680468  .24679898 2.1965363   .3920981 3.5513296  1.0028981 3.185257  1.4219983
    1976 1.954279   .3586017 2.680468   .6195006 2.1965363 -.09239785 3.5513296  -.3425991 3.185257   .9643013
    1977 1.954279  1.5313005 2.680468  1.0875012 2.1965363   1.366001 3.5513296  -.9009992 3.185257  .35590065
    1978 1.954279     2.3699 2.680468   1.039699 2.1965363  2.0144997 3.5513296   .8982986 3.185257  1.6601986
    1979 1.954279 -1.4309988 2.680468    -2.4031 2.1965363  -1.730199 3.5513296 -1.1398989 3.185257  -.5653989
    1980 1.954279 -.13320343 2.680468   .1253957 2.1965363  -.3608031 3.5513296  -1.229802 3.185257  -1.349701
    1981 1.954279 -2.0308006 2.680468     -1.513 2.1965363  -2.757701 3.5513296 -2.3394022 3.185257 -2.7693014
    1982 1.954279  -.9976991 2.680468  -.5988988 2.1965363 -1.8019996 3.5513296  -2.633698 3.185257 -2.3449986
    1983 1.954279  -.9086969 2.680468  1.0149046 2.1965363 -1.1320968 3.5513296  -4.579699 3.185257  -4.761999
    1984 1.954279  1.3141013 2.680468   3.783601 2.1965363   1.093001 3.5513296 -4.6354966 3.185257 -3.2002964
    1985 1.954279  -.8733967 2.680468   .3187043 2.1965363 -1.5047966 3.5513296  -3.321697 3.185257  -2.688096
    1986 1.954279 -1.2375056 2.680468  .05029415 2.1965363 -1.1422058 3.5513296 -3.9199016 3.185257  -3.229702
    1987 1.954279 -3.6921985 2.680468  -2.598498 2.1965363  -4.066399 3.5513296 -3.0094006 3.185257 -2.0427008
    1988 1.954279    -2.3528 2.680468 -2.7908006 2.1965363 -1.5935994 3.5513296 -2.8035014 3.185257 -1.9126003
    1989 1.954279  -7.688198 2.680468  -9.372597 2.1965363  -7.157198 3.5513296    -5.6707 3.185257 -4.5439997
    1990 1.954279  -9.518498 2.680468  -9.311898 2.1965363  -9.742398 3.5513296 -10.726597 3.185257 -11.082798
    1991 1.954279 -13.776502 2.680468 -13.709203 2.1965363   -13.9996 3.5513296 -18.012802 3.185257 -16.981604
    1992 1.954279   -13.3233 2.680468   -14.2893 2.1965363   -13.5949 3.5513296   -18.1126 3.185257 -17.167501
    1993 1.954279 -17.057299 2.680468   -17.5239 2.1965363 -17.500498 3.5513296 -21.837196 3.185257 -20.791197
    1994 1.954279   -20.9162 2.680468   -21.6492 2.1965363   -21.8273 3.5513296   -26.5969 3.185257   -27.2637
    1995 1.954279   -19.8731 2.680468   -22.1855 2.1965363   -20.6644 3.5513296   -24.5541 3.185257   -23.0643
    1996 1.954279   -21.0376 2.680468   -23.1864 2.1965363    -21.874 3.5513296   -24.3403 3.185257   -22.0078
    1997 1.954279   -21.4709 2.680468   -24.2357 2.1965363   -22.5902 3.5513296   -24.3264 3.185257   -21.6234
    1998 1.954279   -19.1829 2.680468   -20.5654 2.1965363   -20.3489 3.5513296   -26.0876 3.185257   -25.0846
    1999 1.954279   -24.5438 2.680468   -26.1011 2.1965363   -25.6143 3.5513296   -27.8567 3.185257   -27.7196
    2000 1.954279   -24.2594 2.680468   -25.1009 2.1965363   -25.0719 3.5513296   -27.6924 3.185257   -26.4853
    end
    Click image for larger version

Name:	graphmultiple.png
Views:	1
Size:	338.3 KB
ID:	1691408


  • #2
    I can't follow all of this easily but let's see how far I can go.

    A bundle of small points and larger ones follow.

    It seems that you used dataex (thanks!) but then edited the output. The point of dataex is that the entirety of the output is code to copy and paste, so I reversed your edits.

    "does not work" is not a good problem report and indeed is specifically warned against at https://www.statalist.org/forums/help#stata

    Never say just that something "doesn't work" or "didn't work", but explain precisely in what sense you didn't get what you wanted.
    Your code refers to dataset files we can't see and is a bit repetitive but seems to boil down to

    Code:
    foreach x in 4 5 21 34 {
      drop if rmspe`l'>=2*rmspemain
      twoway (line diffmain _time,color(black)), name(gmain,replace)
      twoway ((line diff`l' _time,color(gray)), xline(1989,lcolor(gray) lpattern(dash)) yline(0,lcolor(gray) lpattern(dash))), name(g`l',replace)
      graph combine gmain g`l'
     }
    I can imagine two or perhaps three problems there.

    1. Each time around the loop you attempt to drop observations and never read them in again. So there is a high risk of dropping observations you need for later graphs.

    2. You never save the combined graphs, so even if you reach the end of the loop the only combined graph visible will be the last.

    3. I don't like the graph syntax with parentheses around each command. It seems to me that there are quite enough parentheses to keep track of. But, personal preferences aside, there may be further typos in your code.

    Here are the data again.

    Code:
    clear 
    input _time rmspemain diffmain rmspe4 diff4 rmspe5 diff5 rmspe21 diff21 rmspe34 diff34
    1970 1.954279   4.857199 2.680468   7.661901 2.1965363   6.316099 3.5513296    10.3319 3.185257   9.161399
    1971 1.954279  1.8468013 2.680468   4.467302 2.1965363   1.678701 3.5513296     4.8542 3.185257  4.4762006
    1972 1.954279  -.8392038 2.680468   2.714297 2.1965363   .7133963 3.5513296  -2.419502 3.185257  -2.767302
    1973 1.954279 -2.0618958 2.680468  2.5514026 2.1965363  -1.788496 3.5513296 -1.9223986 3.185257 -1.2888986
    1974 1.954279  -.3828028 2.680468  1.7265977 2.1965363  -.2369025 3.5513296  -.5969042 3.185257   .1664961
    1975 1.954279   .5749984 2.680468  .24679898 2.1965363   .3920981 3.5513296  1.0028981 3.185257  1.4219983
    1976 1.954279   .3586017 2.680468   .6195006 2.1965363 -.09239785 3.5513296  -.3425991 3.185257   .9643013
    1977 1.954279  1.5313005 2.680468  1.0875012 2.1965363   1.366001 3.5513296  -.9009992 3.185257  .35590065
    1978 1.954279     2.3699 2.680468   1.039699 2.1965363  2.0144997 3.5513296   .8982986 3.185257  1.6601986
    1979 1.954279 -1.4309988 2.680468    -2.4031 2.1965363  -1.730199 3.5513296 -1.1398989 3.185257  -.5653989
    1980 1.954279 -.13320343 2.680468   .1253957 2.1965363  -.3608031 3.5513296  -1.229802 3.185257  -1.349701
    1981 1.954279 -2.0308006 2.680468     -1.513 2.1965363  -2.757701 3.5513296 -2.3394022 3.185257 -2.7693014
    1982 1.954279  -.9976991 2.680468  -.5988988 2.1965363 -1.8019996 3.5513296  -2.633698 3.185257 -2.3449986
    1983 1.954279  -.9086969 2.680468  1.0149046 2.1965363 -1.1320968 3.5513296  -4.579699 3.185257  -4.761999
    1984 1.954279  1.3141013 2.680468   3.783601 2.1965363   1.093001 3.5513296 -4.6354966 3.185257 -3.2002964
    1985 1.954279  -.8733967 2.680468   .3187043 2.1965363 -1.5047966 3.5513296  -3.321697 3.185257  -2.688096
    1986 1.954279 -1.2375056 2.680468  .05029415 2.1965363 -1.1422058 3.5513296 -3.9199016 3.185257  -3.229702
    1987 1.954279 -3.6921985 2.680468  -2.598498 2.1965363  -4.066399 3.5513296 -3.0094006 3.185257 -2.0427008
    1988 1.954279    -2.3528 2.680468 -2.7908006 2.1965363 -1.5935994 3.5513296 -2.8035014 3.185257 -1.9126003
    1989 1.954279  -7.688198 2.680468  -9.372597 2.1965363  -7.157198 3.5513296    -5.6707 3.185257 -4.5439997
    1990 1.954279  -9.518498 2.680468  -9.311898 2.1965363  -9.742398 3.5513296 -10.726597 3.185257 -11.082798
    1991 1.954279 -13.776502 2.680468 -13.709203 2.1965363   -13.9996 3.5513296 -18.012802 3.185257 -16.981604
    1992 1.954279   -13.3233 2.680468   -14.2893 2.1965363   -13.5949 3.5513296   -18.1126 3.185257 -17.167501
    1993 1.954279 -17.057299 2.680468   -17.5239 2.1965363 -17.500498 3.5513296 -21.837196 3.185257 -20.791197
    1994 1.954279   -20.9162 2.680468   -21.6492 2.1965363   -21.8273 3.5513296   -26.5969 3.185257   -27.2637
    1995 1.954279   -19.8731 2.680468   -22.1855 2.1965363   -20.6644 3.5513296   -24.5541 3.185257   -23.0643
    1996 1.954279   -21.0376 2.680468   -23.1864 2.1965363    -21.874 3.5513296   -24.3403 3.185257   -22.0078
    1997 1.954279   -21.4709 2.680468   -24.2357 2.1965363   -22.5902 3.5513296   -24.3264 3.185257   -21.6234
    1998 1.954279   -19.1829 2.680468   -20.5654 2.1965363   -20.3489 3.5513296   -26.0876 3.185257   -25.0846
    1999 1.954279   -24.5438 2.680468   -26.1011 2.1965363   -25.6143 3.5513296   -27.8567 3.185257   -27.7196
    2000 1.954279   -24.2594 2.680468   -25.1009 2.1965363   -25.0719 3.5513296   -27.6924 3.185257   -26.4853
    end
    It seems that you were trying something like this. I recommend almost any scheme over the default s2color

    You don't need to re-create the graph for diffmain every time around the loop.


    Code:
     
    
    set scheme s1color 
    
    line diffmain _time, color(black) name(gmain)
    
    foreach x in 4 5 21 34 {
        line diff`x' _time, color(gray) xline(1989,lcolor(gray) lpattern(dash)) yline(0,lcolor(gray) lpattern(dash))  name(gthis,replace)
        graph combine gmain gthis, name(graph`x')
    }
    Your code would put two graphs side by side with a sample graph like this for 34. Is that what you intend? Your different colours imply an interest in superimposing the graphs.


    Click image for larger version

Name:	graph34.png
Views:	1
Size:	27.4 KB
ID:	1691416


    I tried your data as a front-and-back plot for which see https://journals.sagepub.com/doi/epu...6867X211025838


    Code:
    reshape long diff, i(_time) j(which) string 
    
    tab which 
    
    label var diff "better text than diff needed here"
    
    fabplot line diff _time, by(which) front(connect) frontopts(lc(blue)) xtitle("") xli(1989) xla(1970 1980 1989 2000)
    The differences between the variables are so small that even the front-and-back plot is a distinctly limited success.

    Detail: Every Stata user, I guess, has some teacher in their history who was fearsome about labelling your axes. That teacher was mostly right, but dates like 1970 1980 1989 2000 will be clear enough to anyone likely to read your graph. You can safely cut the variable name _time.


    Click image for larger version

Name:	21etc.png
Views:	1
Size:	64.1 KB
ID:	1691417


    I ignored the drop. If you (think you) need to drop observations it is better to use an if qualifier to exclude them from the graph.

    Comment


    • #3
      Hi Nick, no, actually this is the issue, I don't want to put two graphs side by side. This is because I said my loop "does not work" because it does not help me to fulfill my objective. Perhaps the combine command is not the correct command here . My objective is to have an unique graphic combining the 5 lines diffmain,diff4,diff5,diff21 and diff34 as shown in the figure in my first post.

      Comment


      • #4
        Thanks for the thanks!

        As said I couldn't follow #1 easily. Why ask how to create a graph you already have? Your code was trying something completely different and I think that it is fair to say that your code was pointing in a completely different direction, as if it worked it would have created four graphs with two panels side by side.

        The graph shown in #1 is easy enough to create but contradictory in labelling different curves with the same colour. I leave that between you and your readers, but I wouldn't accept #1 from a student or if I were reviewing a paper.

        This should get you started.


        Code:
        clear 
        input _time rmspemain diffmain rmspe4 diff4 rmspe5 diff5 rmspe21 diff21 rmspe34 diff34
        1970 1.954279   4.857199 2.680468   7.661901 2.1965363   6.316099 3.5513296    10.3319 3.185257   9.161399
        1971 1.954279  1.8468013 2.680468   4.467302 2.1965363   1.678701 3.5513296     4.8542 3.185257  4.4762006
        1972 1.954279  -.8392038 2.680468   2.714297 2.1965363   .7133963 3.5513296  -2.419502 3.185257  -2.767302
        1973 1.954279 -2.0618958 2.680468  2.5514026 2.1965363  -1.788496 3.5513296 -1.9223986 3.185257 -1.2888986
        1974 1.954279  -.3828028 2.680468  1.7265977 2.1965363  -.2369025 3.5513296  -.5969042 3.185257   .1664961
        1975 1.954279   .5749984 2.680468  .24679898 2.1965363   .3920981 3.5513296  1.0028981 3.185257  1.4219983
        1976 1.954279   .3586017 2.680468   .6195006 2.1965363 -.09239785 3.5513296  -.3425991 3.185257   .9643013
        1977 1.954279  1.5313005 2.680468  1.0875012 2.1965363   1.366001 3.5513296  -.9009992 3.185257  .35590065
        1978 1.954279     2.3699 2.680468   1.039699 2.1965363  2.0144997 3.5513296   .8982986 3.185257  1.6601986
        1979 1.954279 -1.4309988 2.680468    -2.4031 2.1965363  -1.730199 3.5513296 -1.1398989 3.185257  -.5653989
        1980 1.954279 -.13320343 2.680468   .1253957 2.1965363  -.3608031 3.5513296  -1.229802 3.185257  -1.349701
        1981 1.954279 -2.0308006 2.680468     -1.513 2.1965363  -2.757701 3.5513296 -2.3394022 3.185257 -2.7693014
        1982 1.954279  -.9976991 2.680468  -.5988988 2.1965363 -1.8019996 3.5513296  -2.633698 3.185257 -2.3449986
        1983 1.954279  -.9086969 2.680468  1.0149046 2.1965363 -1.1320968 3.5513296  -4.579699 3.185257  -4.761999
        1984 1.954279  1.3141013 2.680468   3.783601 2.1965363   1.093001 3.5513296 -4.6354966 3.185257 -3.2002964
        1985 1.954279  -.8733967 2.680468   .3187043 2.1965363 -1.5047966 3.5513296  -3.321697 3.185257  -2.688096
        1986 1.954279 -1.2375056 2.680468  .05029415 2.1965363 -1.1422058 3.5513296 -3.9199016 3.185257  -3.229702
        1987 1.954279 -3.6921985 2.680468  -2.598498 2.1965363  -4.066399 3.5513296 -3.0094006 3.185257 -2.0427008
        1988 1.954279    -2.3528 2.680468 -2.7908006 2.1965363 -1.5935994 3.5513296 -2.8035014 3.185257 -1.9126003
        1989 1.954279  -7.688198 2.680468  -9.372597 2.1965363  -7.157198 3.5513296    -5.6707 3.185257 -4.5439997
        1990 1.954279  -9.518498 2.680468  -9.311898 2.1965363  -9.742398 3.5513296 -10.726597 3.185257 -11.082798
        1991 1.954279 -13.776502 2.680468 -13.709203 2.1965363   -13.9996 3.5513296 -18.012802 3.185257 -16.981604
        1992 1.954279   -13.3233 2.680468   -14.2893 2.1965363   -13.5949 3.5513296   -18.1126 3.185257 -17.167501
        1993 1.954279 -17.057299 2.680468   -17.5239 2.1965363 -17.500498 3.5513296 -21.837196 3.185257 -20.791197
        1994 1.954279   -20.9162 2.680468   -21.6492 2.1965363   -21.8273 3.5513296   -26.5969 3.185257   -27.2637
        1995 1.954279   -19.8731 2.680468   -22.1855 2.1965363   -20.6644 3.5513296   -24.5541 3.185257   -23.0643
        1996 1.954279   -21.0376 2.680468   -23.1864 2.1965363    -21.874 3.5513296   -24.3403 3.185257   -22.0078
        1997 1.954279   -21.4709 2.680468   -24.2357 2.1965363   -22.5902 3.5513296   -24.3264 3.185257   -21.6234
        1998 1.954279   -19.1829 2.680468   -20.5654 2.1965363   -20.3489 3.5513296   -26.0876 3.185257   -25.0846
        1999 1.954279   -24.5438 2.680468   -26.1011 2.1965363   -25.6143 3.5513296   -27.8567 3.185257   -27.7196
        2000 1.954279   -24.2594 2.680468   -25.1009 2.1965363   -25.0719 3.5513296   -27.6924 3.185257   -26.4853
        end
         
        line diffmain diff? diff?? _time, lcolor(black gray ..) xline(1989,lcolor(gray) lpattern(dash)) yline(0,lcolor(gray) lpattern(dash))

        Comment


        • #5
          Sorry Nick, thank you very much for your help! These last lines that you have posted I think are in line of what I want to do, is there a way to introduce a loop for adding the diff? diff?? one after the other to have a graph with 5 lines. I was trying to do this code but it only give me tha mainline with the last diff(diff34). Thanks for your help.


          Code:
          use "PS2_data", clear
          levelsof state if (state ==4|state ==5|state==21|state==34), local(levels) 
          use "total_results34",clear
           
            foreach l of local levels {
           line diffmain diff`l' _time, lcolor(black gray) xline(1989,lcolor(gray) lpattern(dash)) yline(0,lcolor(gray) lpattern(dash))
             }

          Comment


          • #6
            Sorry. but why try to rewrite a one-line command with a loop? I can think of only one reason, that it's an assignment to show that you can write a loop. In the event that it is an assignment, it is off-limits here but feel free to tell your teachers from me that they should set a better question.

            As with your loop in #1 your code produces 4 graphs each with two lines, one after the other. and does not save the first three; hence you see only the last.

            Your code has nothing at all to do with the goal of one graph with all series together, a problem already solved in essence in #4.

            As before, you don't need levelsof to find a list of values you already know, namely 4 5 21 34.

            Comment


            • #7
              ok thank you Nick!

              Comment

              Working...
              X