Announcement

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

  • Scatter Plot

    My data set has four variables : company_name, sales_2000, sales_2005, and country. The sales variables represent sales in year 2000 and 2005 respectively. I want to generate a scatter plot which would represent sum of sales for each country in 2000 and 2005.

    In the scatter diagram, I would like to have the names of the countries and a 45 degree line.


    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input str244 company_name float(sales_2000 sales_2005) str244 country
    "INDUSTRIAL AND COMMERCIAL BANK OF CHINA LTD"      371789.5  339589.6 "CHINA"             
    "CHINA CONSTRUCTION BANK CORP"                     315144.6 295078.56 "CHINA"             
    "BANK OF CHINA LTD"                               229718.14  179598.3 "CHINA"             
    "BANCO SANTANDER SA"                               222218.8  105981.4 "SPAIN"             
    "HSBC HOLDINGS PLC"                                197620.6  204579.2 "UNITED KINGDOM"    
    "BANK OF AMERICA CORPORATION"                     183107.16 303681.16 "UNITED STATES"     
    "CITIGROUP INC."                                  147041.03  191233.7 "UNITED STATES"     
    "JPMORGAN CHASE & CO."                            146987.02  366301.6 "UNITED STATES"     
    "BANCO BILBAO VIZCAYA ARGENTARIA SA"               113225.5  56747.13 "SPAIN"             
    "UNICREDIT SPA"                                   110084.05  41582.89 "ITALY"             
    "ING GROEP N.V."                                  106393.43   71717.1 "NETHERLANDS"       
    "BANK OF COMMUNICATIONS CO LTD"                   104776.16  70872.84 "CHINA"             
    "LLOYDS BANKING GROUP PLC"                         104546.3  67474.64 "UNITED KINGDOM"    
    "ROYAL BANK OF SCOTLAND GROUP PLC"                 104339.6  45704.64 "UNITED KINGDOM"    
    "INTESA SANPAOLO SPA"                             102284.73  52713.32 "ITALY"             
    "ABN AMRO HOLDING N.V."                            99656.66         . "NETHERLANDS"       
    "WELLS FARGO & COMPANY"                            99539.52 296774.38 "UNITED STATES"     
    "BNP PARIBAS SA"                                   97112.83  93215.16 "FRANCE"            
    "MITSUBISHI UFJ FINANCIAL GROUP INCORPORATED"       95257.9  84384.45 "JAPAN"             
    "SBERBANK ROSSII PAO"                              89609.09  84037.65 "RUSSIAN FEDERATION"
    "CHINA MERCHANTS BANK CO LTD"                      79785.88  112475.3 "CHINA"             
    "MIZUHO FINANCIAL GROUP INC"                       76661.53  46469.06 "JAPAN"             
    "ROYAL BANK OF CANADA"                             75062.37 113668.05 "CANADA"            
    "WACHOVIA CORPORATION"                              74538.8         . "UNITED STATES"     
    "SUMITOMO MITSUI FINANCIAL GROUP INC"              69802.45  51217.95 "JAPAN"             
    "BARCLAYS PLC - ADR"                               66039.76   46488.5 "UNITED KINGDOM"    
    "DEUTSCHE BANK AKTIENGESELLSCHAFT"                 65394.32  39333.43 "GERMANY"           
    "BANCO BRADESCO S.A."                               64549.7  62191.46 "BRAZIL"            
    "BARCLAYS PLC"                                     63887.91  46822.74 "UNITED KINGDOM"    
    "SOCIETE GENERALE"                                 63007.52  41072.48 "FRANCE"            
    "LANDESBANK HESSEN-THUERINGEN"                     62671.36         . "GERMANY"           
    "ITAU UNIBANCO HOLDING S.A."                       61238.28  83110.45 "BRAZIL"            
    "COMMONWEALTH BANK OF AUSTRALIA"                   60694.04 109946.95 "AUSTRALIA"         
    "MIZUHO FINANCIAL GROUP INC - ADR"                 56673.43  46166.69 "JAPAN"             
    "NATIONAL AUSTRALIA BANK LIMITED"                  56110.35  66057.98 "AUSTRALIA"         
    "BANK OF NEW YORK MELLON CORP."                    55878.14  54584.01 "UNITED STATES"     
    "CREDIT AGRICOLE SA"                               55812.54  47058.27 "FRANCE"            
    "SUMITOMO MITSUI FINANCIAL GROUP INC - ADR"        55807.01  61265.66 "JAPAN"             
    "BANK OF NOVA SCOTIA"                              55322.43  77480.71 "CANADA"            
    "U.S. BANCORP"                                     54842.18  88709.48 "UNITED STATES"     
    "HBOS PLC"                                         54438.98         . "UNITED KINGDOM"    
    "CHINA CITIC BANK CORPORATION LIMITED"             54242.13  46625.86 "CHINA"             
    "TORONTO-DOMINION BANK"                            53853.63 104833.04 "CANADA"            
    "NATIONAL AUSTRALIA BANK LIMITED - ADR"            53350.14  61330.92 "AUSTRALIA"         
    "STANDARD CHARTERED PLC"                            51564.8  34746.04 "UNITED KINGDOM"    
    "BAYERISCHE HYPO- UND VEREINSBANK AG"              50531.07         . "GERMANY"           
    "KBC GROUP NV"                                     47702.96  35685.57 "BELGIUM"           
    "AL RAJHI BANKING & INVESTMENT CORPORATION SJSC"   47142.04 27993.684 "SAUDI ARABIA"      
    "WESTPAC BANKING CORPORATION"                      47122.97  82691.64 "AUSTRALIA"         
    "AUSTRALIA AND NEW ZEALAND BANKING GROUP LIMITED"  44789.59  65278.12 "AUSTRALIA"         
    "KOMERCNI BANKA, A.S."                             44757.05  8114.836 "CZECH REPUBLIC"    
    "NORDEA BANK AB"                                   43309.59  48862.09 "SWEDEN"            
    "ABSA BANK LIMITED"                                42277.04  24805.85 "SOUTH AFRICA"      
    "BANCO DO BRASIL SA"                               42261.96   26756.2 "BRAZIL"            
    "BANK AUSTRIA CREDITANSTALT AG"                    41615.48         . "AUSTRIA"           
    "HANG SENG BANK LIMITED- ADR"                      39747.21  47311.42 "HONG KONG"         
    "HANG SENG BANK LIMITED"                           39442.45  47471.17 "HONG KONG"         
    "CANADIAN IMPERIAL BANK OF COMMERCE"              35929.215 38703.434 "CANADA"            
    "INDUSTRIAL BANK CO LTD"                           35500.76  54241.39 "CHINA"             
    "BANK VTB PAO"                                     34436.82   10604.9 "RUSSIAN FEDERATION"
    "BANK OF MONTREAL"                                33027.664  49669.57 "CANADA"            
    "NATIONAL BANK OF GREECE, S.A."                     32693.6 3498.4656 "GREECE"            
    "SHANGHAI PUDONG DEVELOPMENT BANK"                31480.785  56791.32 "CHINA"             
    "RESONA HOLDINGS INC"                              30744.04  12465.78 "JAPAN"             
    "ITAU UNIBANCO HOLDING S.A. - ADR"                29850.615  41068.35 "BRAZIL"            
    "BOC HONG KONG (HOLDINGS) LTD"                    29620.775  53587.12 "HONG KONG"         
    "DEXIA SA"                                         29419.44  5007.352 "BELGIUM"           
    "TURKIYE GARANTI BANKASI AS"                      29400.006 11879.582 "TURKEY"            
    "CHINA MINSHENG BANKING CORPORATION LIMITED"      29378.145   47043.3 "CHINA"             
    "BOC HONG KONG (HOLDINGS) LIMITED - ADR"           29128.01  53693.86 "HONG KONG"         
    "SAMBA FINANCIAL GROUP SJSC"                        28954.8 12544.157 "SAUDI ARABIA"      
    "DANSKE BANK AS"                                  26732.344 34810.992 "DENMARK"           
    "CAIXABANK, S.A."                                 25381.156  27873.89 "SPAIN"             
    "COMMERZBANK AKTIENGESELLSCHAFT"                   25212.66 18777.844 "GERMANY"           
    "KB FINANCIAL GROUP INC - ADR"                     25208.13  23347.69 "KOREA (SOUTH)"     
    "KB FINANCIAL GROUP INCORPORATION"                 24602.77 23705.146 "KOREA (SOUTH)"     
    "BANK PEKAO S.A."                                 24095.516   9764.27 "POLAND"            
    "SHINHAN FINANCIAL GROUP COMPANY LIMITED - ADR"   23504.346  22002.86 "KOREA (SOUTH)"     
    "NATIXIS"                                         23303.125  24801.64 "FRANCE"            
    "RAIFFEISEN BANK INTERNATIONAL AG"                23267.025 11896.934 "AUSTRIA"           
    "SHINHAN FINANCIAL GROUP COMPANY LIMITED"           22468.5 21949.656 "KOREA (SOUTH)"     
    "PNC FINANCIAL SERVICES GROUP INCORPORATED"        22386.65  68249.17 "UNITED STATES"     
    "AKBANK TAS"                                       22289.66  10395.69 "TURKEY"            
    "DBS GROUP HOLDINGS LTD"                           21848.52  47490.86 "SINGAPORE"         
    "SUNTRUST BANKS, INC."                             21772.21 30417.434 "UNITED STATES"     
    "POWSZECHNA KASA OSZCZEDNOSCI BANK POLSKI SA"      21321.41 15911.167 "POLAND"            
    "UNITED OVERSEAS BANK LIMITED"                     20926.31   32867.7 "SINGAPORE"         
    "ERSTE GROUP BANK AG"                              20868.73 17841.422 "AUSTRIA"           
    "BANCO POPULAR ESPANOL"                             20747.6         . "SPAIN"             
    "BANK OF IRELAND"                                  20627.98  9141.989 "IRELAND"           
    "DNB ASA"                                          20347.87  30174.77 "NORWAY"            
    "AIB GROUP PLC"                                   20146.596 17900.584 "IRELAND"           
    "SVENSKA HANDELSBANKEN AB"                         19962.72  26592.99 "SWEDEN"            
    "STANDARD BANK GROUP LIMITED"                     19420.904 25352.623 "SOUTH AFRICA"      
    "AKBANK TAS - ADR"                                 19221.08     10320 "TURKEY"            
    "MEDIOBANCA SPA"                                  18649.059  8545.763 "ITALY"             
    "NATIONAL BANK OF KUWAIT SAKP"                    18543.277 14021.066 "KUWAIT"            
    "EUROBANK ERGASIAS SA"                            18308.305 2226.0977 "GREECE"            
    "KUWAIT FINANCE HOUSE"                            18095.982  10842.91 "KUWAIT"            
    "OVERSEA-CHINESE BANKING CORPORATION LIMITED"     18024.188  38769.13 "SINGAPORE"         
    end

  • #2
    This should get you started:

    Code:
    *generate totals by country
    bysort country: egen total_2000=total(sales_2000)
    bysort country: egen total_2005=total(sales_2005)
    
    *tag country so you only plot it one time
    egen tag=tag(country)
    
    *get the min and max for totals for the range of the function graph
    sum total_2000 total_2005
    
    *graph
    twoway scatter total_2005 total_2000  if tag==1 ,mlabel(country) ytitle("Total Sales 2005") xtitle("Total Sales 2000") || function y=x, range(20000 1400000)
    Stata/MP 14.1 (64-bit x86-64)
    Revision 19 May 2016
    Win 8.1

    Comment


    • #3
      Many thanks Carole J. Wilson! When I use the code, I get the following graph. The points in the lower-left corner are tightly packed. Kindly, give some hints to improve the plot i.e. to see the country names more clearly. I am new to Stata graphics; which "buttons" of the graph editor should I focus on?


      Click image for larger version

Name:	scatter.png
Views:	1
Size:	17.9 KB
ID:	1459691

      Comment


      • #4
        One option is to change to log scales:

        Code:
        twoway scatter total_2005 total_2000  if tag==1 , yscale(log) xscale(log) mlabel(country) ytitle("Total Sales 2005 (log)") xtitle("Total Sales 2000 (log)") || function y=(x), range(20000 1400000)
        Beyond that, you can play with the individual labels in the graph editor, but I don't use that much and cannot advise.
        Stata/MP 14.1 (64-bit x86-64)
        Revision 19 May 2016
        Win 8.1

        Comment


        • #5
          Another option is to create a cutout or inset of the portion of the graph that is cluttered.

          You could append this to Carole Wilson's code in #2 above.
          Code:
          *graph extracted
          twoway scatter total_2005 total_2000 if tag==1 & total_2005<400000, msym(oh) msize(vsmall) mlabsize(vsmall) mlabel(country) ytitle("Total Sales 2005") xtitle("Total Sales 2000") ylabels(0(100000)400000) xlabels(0(100000)400000) xscale(range(0 400000))   || function y=x, range(20000 400000) name(extracted, replace)
          You could then use -graph combine- to show both the full and partial scatterplots side by side.

          Red Owl
          Stata/IC 15.1, Windows 10 (64-bit)

          * Edited to add comment about -graph combine-
          Last edited by Red Owl; 26 Aug 2018, 16:40.

          Comment


          • #6
            Thanks a lot Carole J. Wilson and Red Owl!

            Comment

            Working...
            X