Announcement

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

  • two graphs

    Hello


    I want to combine these two graphs into one graphs please

    twoway (scatter ed yearofbirth) || (lfit ed yearofbirth if yearofbirth>=1967, lstyle(dash)) || (lfit ed yearofbirth if yearofbirth < 1964, lstyle(dash)) if rural==1 , xline(1967 1965) legend(off) title(Years of School) xtitle(Birth year )
    graph save ed_rural, replace




    twoway (scatter ed yearofbirth) || (lfit ed yearofbirth if yearofbirth>=1967, lstyle(dash)) || (lfit ed yearofbirth if yearofbirth < 1964, lstyle(dash)) if rural==0 , xline(1967 1965) legend(off) title(Years of School) xtitle(Birth year )
    graph save ed_urban, replace



    data



    ----------------------- copy starting from the next line -----------------------
    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input int yearofbirth float(rural edu)
    1945 0  5.461538
    1945 1 3.0535715
    1946 0 4.4444447
    1946 1  2.981132
    1947 0 4.4583335
    1947 1 3.0229886
    1948 0  7.466667
    1948 1  3.486111
    1949 0  6.466667
    1949 1 3.4285715
    1950 0  5.333333
    1950 1  3.837838
    1951 0  7.793103
    1951 1  3.505618
    1952 0      7.36
    1952 1  3.639535
    1953 0  6.193548
    1953 1         4
    1954 0  6.254546
    1954 1  3.862857
    1955 0      6.86
    1955 1  3.469388
    1956 0  7.218391
    1956 1 3.8533835
    1957 0  6.893617
    1957 1 4.0705395
    1958 0  6.971428
    1958 1 4.1626296
    1959 0  7.011765
    1959 1  3.993243
    1960 0    7.4375
    1960 1 3.8481014
    1961 0  7.695652
    1961 1 4.3138685
    1962 0  7.971292
    1962 1 4.3483872
    1963 0  8.376344
    1963 1  4.818792
    1967 0  9.774648
    1967 1  7.380208
    1968 0  9.903571
    1968 1  7.065299
    1969 0  9.743494
    1969 1  7.844485
    1970 0  9.916924
    1970 1  7.581955
    1971 0 10.474684
    1971 1  7.606625
    1972 0 10.030556
    1972 1  7.788915
    1973 0 10.155216
    1973 1  7.918845
    1974 0  9.981396
    1974 1  7.819808
    1975 0   10.1309
    1975 1  7.725707
    1976 0 10.116773
    1976 1  7.886999
    1977 0   10.1833
    1977 1  7.752955
    1978 0  10.25701
    1978 1   7.91217
    1979 0 10.060362
    1979 1  7.930493
    1980 0 10.401316
    1980 1  8.386111
    1981 0 10.675324
    1981 1  8.188822
    1982 0 10.276224
    1982 1  8.335929
    1983 0 10.234568
    1983 1   8.07248
    1984 0 10.370723
    1984 1  8.129973
    1985 0  10.37156
    1985 1  8.451258
    1986 0     10.67
    1986 1  8.335877
    1987 0  10.58913
    1987 1  8.426045
    1988 0 10.391775
    1988 1  8.287082
    1989 0 10.039095
    1989 1   8.25811
    1990 0 10.204603
    1990 1  8.161189
    1991 0 10.863905
    1991 1  8.934037
    1992 0  10.68661
    1992 1 8.7576475
    1993 0  10.43131
    1993 1  8.633952
    1994 0 10.023728
    1994 1  8.825364
    1995 0  9.856209
    1995 1  8.348605
    1996 0 10.740332
    1996 1  9.220834
    1997 0  10.17801
    1997 1   8.86036
    end

  • #2
    Olivia:
    you may want to try:
    Code:
    . twoway (scatter ed yearofbirth) || (lfit ed yearofbirth if yearofbirth>=1967, lstyle(dash)) || (lfit ed yearofbirth if yearofbirth < 1964, lstyle(dash)) if rural==1 , xline(1
    > 967 1965) legend(off) title(Years of School) xtitle(Birth year ) saving(1)
    
    file 1.gph saved
    
    . twoway (scatter ed yearofbirth) || (lfit ed yearofbirth if yearofbirth>=1967, lstyle(dash)) || (lfit ed yearofbirth if yearofbirth < 1964, lstyle(dash)) if rural==0 , xline(1
    > 967 1965) legend(off) title(Years of School) xtitle(Birth year ) saving(2)
    
    file 2.gph saved
    
    . gr combine 1.gph 2.gph
    
    .
    Kind regards,
    Carlo
    (Stata 19.0)

    Comment


    • #3
      My guess differs from that of Carlo Lazzaro. I find the parentheses syntax for different twoway calls much harder to read and write than the alternative. I think you need a legend or at least some other way to distinguish rural and urban.

      Code:
      * Example generated by -dataex-. For more info, type help dataex
      clear
      input int yearofbirth float(rural edu)
      1945 0  5.461538
      1945 1 3.0535715
      1946 0 4.4444447
      1946 1  2.981132
      1947 0 4.4583335
      1947 1 3.0229886
      1948 0  7.466667
      1948 1  3.486111
      1949 0  6.466667
      1949 1 3.4285715
      1950 0  5.333333
      1950 1  3.837838
      1951 0  7.793103
      1951 1  3.505618
      1952 0      7.36
      1952 1  3.639535
      1953 0  6.193548
      1953 1         4
      1954 0  6.254546
      1954 1  3.862857
      1955 0      6.86
      1955 1  3.469388
      1956 0  7.218391
      1956 1 3.8533835
      1957 0  6.893617
      1957 1 4.0705395
      1958 0  6.971428
      1958 1 4.1626296
      1959 0  7.011765
      1959 1  3.993243
      1960 0    7.4375
      1960 1 3.8481014
      1961 0  7.695652
      1961 1 4.3138685
      1962 0  7.971292
      1962 1 4.3483872
      1963 0  8.376344
      1963 1  4.818792
      1967 0  9.774648
      1967 1  7.380208
      1968 0  9.903571
      1968 1  7.065299
      1969 0  9.743494
      1969 1  7.844485
      1970 0  9.916924
      1970 1  7.581955
      1971 0 10.474684
      1971 1  7.606625
      1972 0 10.030556
      1972 1  7.788915
      1973 0 10.155216
      1973 1  7.918845
      1974 0  9.981396
      1974 1  7.819808
      1975 0   10.1309
      1975 1  7.725707
      1976 0 10.116773
      1976 1  7.886999
      1977 0   10.1833
      1977 1  7.752955
      1978 0  10.25701
      1978 1   7.91217
      1979 0 10.060362
      1979 1  7.930493
      1980 0 10.401316
      1980 1  8.386111
      1981 0 10.675324
      1981 1  8.188822
      1982 0 10.276224
      1982 1  8.335929
      1983 0 10.234568
      1983 1   8.07248
      1984 0 10.370723
      1984 1  8.129973
      1985 0  10.37156
      1985 1  8.451258
      1986 0     10.67
      1986 1  8.335877
      1987 0  10.58913
      1987 1  8.426045
      1988 0 10.391775
      1988 1  8.287082
      1989 0 10.039095
      1989 1   8.25811
      1990 0 10.204603
      1990 1  8.161189
      1991 0 10.863905
      1991 1  8.934037
      1992 0  10.68661
      1992 1 8.7576475
      1993 0  10.43131
      1993 1  8.633952
      1994 0 10.023728
      1994 1  8.825364
      1995 0  9.856209
      1995 1  8.348605
      1996 0 10.740332
      1996 1  9.220834
      1997 0  10.17801
      1997 1   8.86036
      end
      
      
      local opts xline(1967 1965) title(Years of School) xtitle(Birth year) xla(1945(5)1995) legend(order(4 "urban" 1 "rural" ) pos(11) ring(0))
      
      twoway scatter ed yearofbirth if rural==1, ms(Oh) mc(blue) /// 
      || lfit ed yearofbirth if yearofbirth>=1967 & rural==1, lstyle(dash) lc(blue) ///
      || lfit ed yearofbirth if yearofbirth<1964 & rural==1, lstyle(dash) lc(blue)  ///
      || scatter ed yearofbirth if rural==0, ms(+) mc(red)                          ///
      || lfit ed yearofbirth if yearofbirth>=1967 & rural==0, lstyle(dash) lc(red) ///
      || lfit ed yearofbirth if yearofbirth < 1964 & rural==0, lstyle(dash) lc(red) `opts'
      Click image for larger version

Name:	twographs.png
Views:	1
Size:	51.1 KB
ID:	1714025

      Comment


      • #4
        Thanks @Nick Cox this is what I am looking please

        is there a
        way to estimate the jump please in each case(rural , urban) please

        Comment


        • #5
          Many ways. Some large fraction of applied work in economics is currently focused on such jumps, but I am not qualified to suggest which methods might work well or even best in your set-up. I'd ask a new question if you don't get a good answer.

          Comment


          • #6
            I mean we can see that in case of urban on average the jump is approximately 2. how we can show this please in graph or calculate this please for the other case

            Comment


            • #7
              I am not following you. The jump looks very clear to me. How else would it be shown? Adding extra lines or text would just complicate the graph. You might be better off calculating the gaps and commenting in a caption to the graph.

              Comment


              • #8
                Nick Cox
                local opts xline(1967 1965) title(Years of School) xtitle(Birth year) xla(1945(5)1995) legend(order(4 "urban" 1 "rural" ) pos(11) ring(0)) can you please let me know what does the 4 "urban" 1 "rural" means please , because in my data rural is 1 and urban is 0. Thanks

                Comment


                • #9
                  Start with the help!


                  Code:
                  help legend option
                  shows that in my order() call 4 refers to the 4th variable plotted and 1 to the 1st variable plotted. The 4th variable plotted is ed and 1st variable plotted is also ed but the 4th variable is plotted only for urban observations and the 1st variable for only for rural. The legend specification has nothing to do with the values of the variables. That explains the text in the legend. I show urban above rural in the legend, because the urban values are higher.

                  Comment


                  • #10
                    @Nick Cox
                    I am trying the same code for the overall data but not getting the same result please can you please help



                    local opts xline(1967 1965) title(Years of School) xtitle(Birth year) xla(1945(5)1995) legend() pos(11) ring(0))

                    twoway scatter ed yearofbirth , ms(Oh) mc(blue) ///
                    || lfit ed yearofbirth if yearofbirth>=1967 , lstyle(dash) lc(blue) ///
                    || lfit ed yearofbirth if yearofbirth<1964 , lstyle(dash) lc(blue) ///
                    || scatter ed yearofbirth , ms(*) mc(red) ///
                    || lfit ed yearofbirth if yearofbirth>=1967 , lstyle(dash) lc(red) ///
                    || lfit ed yearofbirth if yearofbirth < 1964 , lstyle(dash) lc(red) `opts'

                    graph save ed, replace



                    Data


                    ----------------------- copy starting from the next line -----------------------
                    Code:
                    * Example generated by -dataex-. For more info, type help dataex
                    clear
                    input int yearofbirth float(edu literacy)
                    1945 3.5072465  .6376812
                    1946 3.1935484  .5967742
                    1947  3.333333  .5585586
                    1948  4.172414  .6666667
                    1949 4.2280703  .6403509
                    1950 4.1037035  .3407407
                    1951  4.559322  .3813559
                    1952 4.4774776  .4684685
                    1953  4.626728   .437788
                    1954 4.4347825  .4086956
                    1955 4.3299494  .4111675
                    1956 4.6827197  .4929178
                    1957  4.862687   .519403
                    1958  4.911168 .52284265
                    1959 4.6666665  .5249344
                    1960  4.787383  .5233645
                    1961  5.446602   .526699
                    1962   5.47181 .56973296
                    1963  5.864139  .5908373
                    1964  5.849765  .5978091
                    1965  6.780075  .6484963
                    1966    7.5573  .6923077
                    1967  8.398204  .7410179
                    1968  8.039216  .7230392
                    1969  8.465937  .7396594
                    1970  8.348485  .7444444
                    1971  8.740926  .7546934
                    1972  8.456954  .7574503
                    1973  8.703571  .7455357
                    1974  8.623163  .7675022
                    1975  8.602033  .7818608
                    1976  8.698609  .7836167
                    1977  8.645475  .7584144
                    1978  8.731428  .7665306
                    1979  8.692585  .7638589
                    1980  9.167517  .7253401
                    1981  9.210855  .7224199
                    1982  9.090415  .7457512
                    1983  8.917931       .72
                    1984  9.050781  .8015625
                    1985  9.232276  .9580224
                    1986   9.34632   .965368
                    1987  9.345656  .9630314
                    1988  9.179981  .9540863
                    1989  8.982427  .9615062
                    1990  8.936893  .9495146
                    1991  9.843794  .9595537
                    1992  9.630155  .9639176
                    1993  9.449275   .957971
                    1994  9.280928  .9626288
                    1995  8.919555  .9418317
                    end
                    ------------------ copy up to and including the previous line ------------------


                    Comment


                    • #11
                      Please see the attached file I donot want to have legend in this please
                      Attached Files

                      Comment


                      • #12
                        The help is your friend. pos() and ring() are suboptions of legend() and must appear inside the legend() call.

                        Code:
                        legend() pos(11) ring(0))
                        is quite illegal.

                        Code:
                        legend(pos(11) ring(0))
                        would be legal.

                        That said, I don't understand what you're trying to do, as each data point and each fitted line now appears twice.

                        If you don't want a legend, then don't ask for one in the code or switch it off with legend(off).

                        Please don't post .gph attachments. This is explained in the FAQ Advice.
                        Last edited by Nick Cox; 18 May 2023, 06:55.

                        Comment


                        • #13
                          Posts are crossing, which is OK. Please see revisions to #12.

                          What's not so OK: Please see https://www.statalist.org/forums/help#before and the whole of that document. You are expected to try looking at the help!

                          Comment


                          • #14
                            Thanks Nick Cox

                            I will try to do search help before posting

                            Yes you are right each data point and each fitted line now appears twice. By mistake I did that Please

                            Comment

                            Working...
                            X