Announcement

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

  • line plots -- average, minimum & maximum sales for each quarter

    I want to plot three line plots in one graph. On the x-axis I want to have the quarters i.e. Q1, Q2, Q3, and Q4. And, on the y-axis I want to show the average, minimum, and maximum "sales" for each quarter.

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input int date float(quarter sales)
    13239 144  15.07396
    13330 145  15.22263
    13422 146   15.7636
    13514 147  17.03415
    13604 148  17.59395
    13695 149  18.00279
    13787 150  18.07942
    13879 151  21.87692
    13969 152  22.51151
    14060 153  22.81462
    14152 154  24.65873
    14244 155  27.38165
    14334 156  28.00771
    14425 157  28.02526
    14517 158  28.05533
    14609 159  27.08693
    14700 160  29.16289
    14791 161  30.16414
    14883 162  32.27179
    14975 163  34.43443
    15065 164  36.40239
    15156 165  35.48289
    15248 166  35.10188
    15340 167  36.21612
    15430 168  37.56783
    15521 169  36.78747
    15613 170  36.58957
    15705 171  37.74507
    15795 172  36.79951
    15886 173  42.71779
    15978 174  43.54155
    16070 175   44.6773
    16161 176  47.31126
    16252 177  50.62463
    16344 178  51.71962
    16436 179  50.26723
    16526 180  52.15678
    16617 181  52.95755
    16709 182  54.54217
    16801 183  55.01907
    16891 184  57.37235
    16982 185  59.59472
    17074 186  62.04618
    17166 187  65.14246
    17256 188    67.598
    17347 189  69.60613
    17439 190    70.948
    17531 191  75.71963
    17622 192  73.87219
    17713 193  72.65429
    17805 194   71.2222
    17897 195  69.34279
    17987 196  71.96011
    18078 197  73.72668
    18170 198  75.46033
    18262 199  76.14115
    18352 200  78.47517
    18443 201  78.30347
    18535 202  79.78409
    18627 203  80.78126
    18717 204  81.86597
    18808 205  83.78559
    18900 206   81.7859
    18992 207  73.38277
    19083 208  73.07322
    19174 209  74.47658
    19266 210  77.54887
    19358 211  80.99576
    19448 212  82.05329
    19539 213   80.5828
    19631 214  82.75167
    19723 215  83.92115
    19813 216  86.00323
    19904 217  89.75872
    19996 218  89.61819
    20088 219  90.73944
    20178 220   94.8772
    20269 221  94.64499
    20361 222  94.31632
    20453 223  96.68026
    20544 224  97.78695
    20635 225  97.12594
    20727 226  97.48424
    20819 227  98.30496
    20909 228 102.56005
    21000 229 102.88103
    21092 230 104.58755
    21184 231 104.64616
    21274 232 101.83655
    21365 233  102.9994
    21457 234  103.4964
    21549 235  102.7439
    end
    format %tdnn/dd/CCYY date
    format %tq quarter

  • #2
    Dear Budu,
    you mean something like this?
    Code:
    gen n=mod(_n,4)
    replace n=4 if n==0
    sort n sale
    bysort n: gen min=sale[1]
    bysort n:gen max=sale[_N]
    collapse (mean) sales min max, by(n)
    tw (line max n) (line sales n) (line min n)
    2B or not 2B, that's a question!

    Comment


    • #3
      Liu Qiang Thanks a lot. This is what I wanted!

      Comment


      • #4
        Originally posted by Budu Gulo View Post
        Liu Qiang Thanks a lot. This is what I wanted!
        hi Budu, this is just a pilot example since I am not clear about what you really want. One thing you still have to ensure is that there would be no missing values in quarter. To avoid this, you may use the code as follows:
        Code:
        gen n=mod(quarter,4)
        replace n=n+1
        sort n sale
        bysort n: gen min=sale[1]
        bysort n:gen max=sale[_N]
        collapse (mean) sales min max, by(n)
        tw (line max n) (line sales n) (line min n)
        2B or not 2B, that's a question!

        Comment


        • #5
          Liu Qiang thanks again. I tried using egen;got the same result.
          Code:
          gen n = mod(quarter,4)
          replace n = n+1
          sort n sale
          bysort n: egen min = min(sales)
          bysort n: egen max = max(sales)
          bysort n: egen avg = mean(sales)
          tw (line max n) (line avg n) (line min n)
          Now I have two more questions :
          1. How do I change the x-axis labels? Instead of showing 1, 2, 3, and 4, I want to show Q1, Q2, Q3, and Q4.
          2. Suppose I want to print it black and white. How do I use dotted and dashed lines?

          Comment


          • #6
            Originally posted by Budu Gulo View Post
            Liu Qiang thanks again. I tried using egen;got the same result.
            Code:
            gen n = mod(quarter,4)
            replace n = n+1
            sort n sale
            bysort n: egen min = min(sales)
            bysort n: egen max = max(sales)
            bysort n: egen avg = mean(sales)
            tw (line max n) (line avg n) (line min n)
            Now I have two more questions :
            1. How do I change the x-axis labels? Instead of showing 1, 2, 3, and 4, I want to show Q1, Q2, Q3, and Q4.
            2. Suppose I want to print it black and white. How do I use dotted and dashed lines?
            Hi Budu, you need to add options. This may work as a reference for you:
            Code:
            tw (line max n,lp(dash)) (line sales n,lp(solid)lc(black)) (line min n,lp(dot)), scheme(s1mono) legend(order(1 "Max" 2 "Mean" 3 "Min") rows(1)) xlabel( 1 "Q1" 2 "Q2" 3 "Q3" 4 "Q4") xtitle("")
            I favor the scheme s1mono because it's the plainest scheme in stata in my opinion. You can also change it to other scheme styles if you want to.
            2B or not 2B, that's a question!

            Comment


            • #7
              Many thanks Liu Qiang!

              Comment

              Working...
              X