Announcement

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

  • Two y-axes plot

    My data set has three variables: date, type_of_company, and sales. The variable type_of_company is a categorical variable, with four categories : "A+", "A-", "B+", and "B". I want to generate two plots in one graph: One plot would present sales of "A+" companies by month; other one would present percentage of sales by "A+" companies by month (that is, if we add up all the sales by month what percent belongs to "A+" companies).

    In short, the x-axis would represent months, and the two y-axes --left and right--would represent sales and percent of sales of "A+" companies.

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input float date str2 type_of_company float sales
    17167 "A-"  461390
    17198 "A-"  484773
    17226 "A-"  475477
    17257 "A-"  487706
    17287 "A-"  508125
    17318 "A-"  530895
    17348 "A-"  528911
    17379 "A-"  532296
    17410 "A-"  528430
    17440 "A-"  577063
    17471 "A-"  569910
    17501 "A-"  556396
    17532 "A-"  554090
    17563 "A-"  527268
    17592 "A-"  516887
    17623 "A-"  512096
    17653 "A-"  529073
    17684 "A-"  515582
    17714 "A-"  451889
    17745 "A-"  454076
    17776 "A-"  473585
    17806 "A-"  403169
    17837 "A-"  336504
    17867 "A-"  282454
    17167 "B+"   41765
    17198 "B+"   44656
    17226 "B+"   44798
    17257 "B+"   49307
    17287 "B+"   54469
    17318 "B+"   57547
    17348 "B+"   64375
    17379 "B+"   64850
    17410 "B+"   65161
    17440 "B+"   74496
    17471 "B+"   87354
    17501 "B+"   84085
    17532 "B+"   88423
    17563 "B+"   72580
    17592 "B+"   69048
    17623 "B+"   74501
    17653 "B+"   80657
    17684 "B+"   87685
    17714 "B+"   78898
    17745 "B+"  108294
    17776 "B+"  108653
    17806 "B+"   81871
    17837 "B+"   57808
    17867 "B+"   44567
    17167 "B"  1042719
    17198 "B"  1060750
    17226 "B"  1051083
    17257 "B"  1062208
    17287 "B"  1076744
    17318 "B"  1113599
    17348 "B"  1163229
    17379 "B"  1125650
    17410 "B"  1103981
    17440 "B"  1174523
    17471 "B"  1250127
    17501 "B"  1152243
    17532 "B"  1116859
    17563 "B"  1039562
    17592 "B"  1016965
    17623 "B"   995591
    17653 "B"  1018994
    17684 "B"  1014310
    17714 "B"   901925
    17745 "B"   866273
    17776 "B"   862769
    17806 "B"   791357
    17837 "B"   667862
    17867 "B"   537538
    17167 "A+"  689146
    17198 "A+"  703882
    17226 "A+"  685212
    17257 "A+"  710810
    17287 "A+"  740271
    17318 "A+"  771838
    17348 "A+"  764078
    17379 "A+"  752530
    17410 "A+"  767773
    17440 "A+"  811376
    17471 "A+"  841409
    17501 "A+"  830936
    17532 "A+"  822536
    17563 "A+"  775964
    17592 "A+"  789393
    17623 "A+"  794566
    17653 "A+"  758239
    17684 "A+"  753736
    17714 "A+"  699446
    17745 "A+"  697625
    17776 "A+"  703192
    17806 "A+"  674108
    17837 "A+"  556932
    17867 "A+"  473251
    end
    format %td date

  • #2
    You don't say what kind of graph you want, so I'm assuming a line graph. I am also assuming you only have only one "A+" company per month. If you have multiples of the same type, then the code will need to be altered.

    Code:
    gen monthly=mofd(date)
    format %tm monthly
    bysort monthly: egen all_sales=total(sales)
    gen pc_sales=(sales/all_sales)*100
    
    sort monthly
    twoway line sales monthly if type_of_company=="A+" || line pc_sales monthly if type_of_company=="A+" , yaxis(2)
    StataNow/MP 19.5 (64-bit x86-64)
    Win 11

    Comment


    • #3
      I was indeed looking for a line graph. Thanks a lot Carole J. Wilson!

      Comment

      Working...
      X