Announcement

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

  • multiple line plots--means--with condition

    My data set has four variables: code, year (1990-1999), name, and net_income. I want to plot three line plots in one graph. To be specific, one line would present mean of top 3 net_income by year; the other two would present mean of top 5 and top 7.
    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input double(code year) str244 name double net_income
     25409 1990 "BRYANT GROUP PLC"                                   22147620
     34293 1990 "FLORIDA PROGRESS CORPORATION"                      1.816e+08
     22865 1990 "MUNKSJO AB (OLD)"                                   23847792
     38149 1990 "NACCO INDUSTRIES, INC."                             30947000
     38770 1990 "OLSTEN CORPORATION (THE)"                           12403000
     39327 1990 "PEOPLES ENERGY CORPORATION"                         69655000
     27696 1990 "ROLFE & NOLAN PLC"                                1215124.41
     41645 1990 "SUNAMERICA INC."                                    38767000
     16672 1990 "TOYOTA INDUSTRIES CORPORATION"                      99002800
      7564 1990 "UNIBAIL RODAMCO SE"                              99657871.28
     29748 1991 "ANAREN, INC."                                         786487
     27352 1991 "FRENCH PLC"                                       1122589.71
     20683 1991 "FURAMA LIMITED"                                 3240460.3397
     25581 1991 "GUARDIAN ROYAL EXCHANGE PLC"                        89719680
     15810 1991 "KYOWA ELECTRONIC INSTRUMENTS COMPANY LIMITED"    -3182765.49
     15703 1991 "OAK CAPITAL CORPORATION"                          2536549.38
     59959 1991 "PHH CORPORATION"                                    47079000
     25368 1991 "REACH PLC"                                       16942066.24
     16575 1991 "SUMITOMO CHEMICAL CO LIMITED"                      161217270
     13898 1991 "T. RAD COMPANY LIMITED"                           6718793.58
     27074 1992 "BLUEBIRD TOYS PLC"                                2022725.25
       777 1992 "BTR NYLEX LIMITED"                                 251307504
     31176 1992 "CSX CORPORATION"                                    20000000
    126888 1992 "HSBC HOLDINGS PLC"                                2143294560
     28239 1992 "LONDON ELECTRICITY PLC"                            179687385
     38876 1992 "ORANGE-CO, INC."                                     5932000
     19961 1992 "ORIENTAL PETROLEUM & MINERALS CORPORATION"        6192333.13
     39171 1992 "PARAGON TRADE BRANDS INCORPORATED"                  17766000
      8151 1992 "STE SUBURBAINE CANALISATION. GD. TRAVAUX"          961450.33
     41514 1992 "STEWART & STEVENSON SERVICES, INC."                 34658000
     21354 1993 "ALPHA LIMITED"                                   19246974.36
     31875 1993 "CITY NATIONAL CORPORATION"                          -6906000
     17751 1993 "CYCLE & CARRIAGE BINTANG BERHAD"                 7822883.376
     15704 1993 "HIRANO TECSEED CO LTD"                            2628982.85
     38178 1993 "NATIONAL BANCSHARES CORPORATION"                     2000053
     16616 1993 "NISSAN SHATAI COMPANY LIMITED"                      16592550
     39756 1993 "PRINTRONIX, INC."                                   -2340000
      8119 1993 "PRODEF SA"                                         441859.53
     21680 1993 "SACYR SA"                                           26299000
     16997 1993 "WOORI BANK"                                         22565520
     31265 1994 "CALLOWAY'S NURSERY, INC."                           -4110000
     27357 1994 "GARTMORE EMERGING PACIFIC INVESTMENT TRU"          107981.55
     27395 1994 "HARTLEPOOLS WATER PLC"                            1551258.72
     35414 1994 "HI-LO AUTOMOTIVE, INC."                              9133000
      8088 1994 "LA BROSSE ET DUPONT SA"                        4477575.35248
     39392 1994 "PETROBRAS DISTRIBUIDORA SA"                   265765211.1576
      1476 1994 "POSGOLD LIMITED"                                 56833587.36
     40068 1994 "RAMCO-GERSHENSON PROPERTIES TRUST"                  15641876
     13519 1994 "SANRIO COMPANY LIMITED"                            -42880500
      9746 1994 "VICTORIA VERSICHERUNG AG"                      64426816.3074
     27055 1995 "ANS PLC"                                             3222051
     30613 1995 "BEST BUY CO., INC."                                 57651000
     11110 1995 "CITIC LTD"                                         397338900
     35140 1995 "HAEMONETICS CORPORATION"                            33645000
     37217 1995 "MSC. SOFTWARE CORPORATION"                          14407000
     27423 1995 "PHILIP HARRIS PLC"                                2533232.25
     15492 1995 "SAKURADA CO., LTD."                               5967134.25
     19384 1995 "SOLVANG ASA"                                    3582224.4246
      5192 1995 "SUTTON RESOURCES LTD"                         -1272561.68466
     13862 1995 "TOKO INC"                                         12345055.8
     25425 1996 "ASSOCIATED BRITISH PORTS HOLDINGS P.L.C."          120376799
     26336 1996 "COMMUNISIS PLC"                                  524983.2547
     18542 1996 "EMPAQUES PONDEROSA SA"                           32282705.84
     28651 1996 "ENIC PLC"                                          650382.72
     61222 1996 "IOWA FIRST BANCSHARES CORP."                         3465000
     37961 1996 "MONTEREY GOURMET FOODS, INC."                       -8673028
     28302 1996 "PLANIT HOLDINGS PLC"                             -10405761.7
     40324 1996 "RIDGEWOOD HOTELS, INC."                             -1178000
     13944 1996 "TOHTO SUISAN CO., LTD."                             -2022440
     42814 1996 "UNITED OVERSEAS BANK LIMITED"                   507795969.42
      6003 1997 "ALM. BRAND PANTEBREVE A/S"                        -5070161.6
     26931 1997 "COMPCO HOLDINGS PLC"                              3313106.14
     53271 1997 "DUQUESNE LIGHT HOLDINGS INC."                      215801000
     20875 1997 "ELANDSRAND GOLD MINING COMPANY LIMITED"             45575840
     15676 1997 "FUJITSU ACCESS"                                  25891907.52
     38342 1997 "NETSCOUT SYSTEMS INCORPORATED"                       5918000
     38528 1997 "NORSK HYDRO ASA"                                   735362400
     39426 1997 "PHARMACIA CORPORATION"                             4.700e+08
      1590 1997 "PMP LIMITED"                                     35951041.06
     28253 1997 "SSE PLC"                                           269179628
     29367 1998 "ALLSTATE CORP"                                     3.333e+09
    127272 1998 "ARCHROCK INC"                                       30377000
     31890 1998 "CLARIFY INC."                                        7318000
     15651 1998 "ENDO LIGHTING CORPORATION"                         143576.18
     34063 1998 "FIRST CINCINNATI INC."                              -6366000
     34917 1998 "GRAND COURT LIFESTYLES, INC."                      -12976000
    104220 1998 "MICROSEMI COMMUNICATIONS INC"                       52873000
     45976 1998 "PEPCO HOLDINGS, INC."                              2.320e+08
     22211 1998 "RKS AB"                                            1299828.6
     20682 1998 "SC GLOBAL DEVELOPMENT LIMITED"                 -125081.75922
     17409 1999 "HYOSUNG CORPORATION"                            81561912.651
     16577 1999 "JAPAN ENERGY CORPORATION"                          147014400
     11136 1999 "LAI SUN DEVELOPMENT CO. LTD."                  -880221055.09
     54272 1999 "LOGIBEC GROUPE INFORMATIQUE LIMITED"            470613.16359
     15307 1999 "MATSUSHITA COMMUNICATIONS INDUSTRIAL CO LTD"       223367040
     16250 1999 "NHK SPRING CO LTD"                                   6597760
     15926 1999 "NIDEC CORPORATION"                               47207954.56
     40737 1999 "SCICLONE PHARMACEUTICALS, INC."                     -5467000
      5687 1999 "SHANGHAI SMI HOLDING CO LTD"                     52572630.24
     27752 1999 "SHILOH P.L.C."                                 4074387.30244
    end
    format %ty year

  • #2
    Code:
    foreach n of numlist 3 5 7 {
        by year (net_income), sort: gen is_top_`n' = (_n == `n')
        by year (net_income): egen mean_top_`n' = mean(cond(is_top_`n', net_income, .))
    }
    
    graph twoway line mean_top_* year, sort
    Note: You don't say what you want to do with any ties. The above code, if there are ties, will assign them different but consecutive ranks in arbitrary (and not reproducible) order. As such, if two names are tied for second place, one of them will be considered third place, and the "top 3" will consist of the unique first place winner plus the two ties for second place. If two names are tied for third place, then one of them will be considered third place and the other fourth place (again, not reproducibly, and arbitrarily). The "top 3" will then consist of the unique first and second places plus one of the ties.

    If that is not how you want to handle ties, look into -egen-'s -rank()- function and the various options it offers for handling ties.

    Comment


    • #3
      Thanks a lot Clyde Schechter! The code produces what I wanted.

      Comment

      Working...
      X