Announcement

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

  • Generating area chart for xtset data reflecting percentage of panel per date

    Colleagues,

    I'm working with the attached data* that I xtset in the following manner:
    Code:
    import delimited sample.txt, delimiter(tab) clear
    encode cohort, gen(cohort_num)
    xtset cohort_num date
    I would like generate an area chart with the year on x-axis and percentage of total population for each cohort on y-axis. I was looking on the net but couldn't find much guidance on generating area charts for panel data. Naturally, I will be grateful for any help/

    * The data is publicly available and corresponds to ONS demographic projections and can be found on the ONS website.
    Attached Files
    Kind regards,
    Konrad
    Version: Stata/IC 13.1

  • #2
    Something like this: ... ?

    Code:
    import delimited sample.txt, delimiter(tab) clear
    encode cohort, gen(cohort_num)
    xtset cohort_num date
    
    drop cohort
    reshape wide somevalue , i(date) j(cohort_num)
    egen sometotal=rowtotal(somevalue?)
    
    gen cumpct0 = 0
    forvalues C=1/6 {
       local C0 = `C'-1
       gen cumpct`C' = cumpct`C0' + 100*somevalue`C'/sometotal
    }
    
    twoway (rarea cumpct0 cumpct1 date , vertical) ///
           (rarea cumpct1 cumpct2 date , vertical) ///
           (rarea cumpct2 cumpct3 date , vertical) ///
           (rarea cumpct3 cumpct4 date , vertical) ///
           (rarea cumpct4 cumpct5 date , vertical) ///
           (rarea cumpct5 cumpct6 date , vertical)

    Comment


    • #3
      Thanks, I think it starts to look nice.
      Click image for larger version

Name:	Graph.png
Views:	1
Size:	24.7 KB
ID:	567818
      I was wondering if it would be very difficult to make it presentable in monochromatic scheme, I'm using Tufte's scheme for most of my graphs which produces beautiful graphics but doesn't work nicely for this one:
      Code:
      import delimited sample.txt, delimiter(tab) clear
      encode cohort, gen(cohort_num)
      xtset cohort_num date
      
      drop cohort
      reshape wide somevalue , i(date) j(cohort_num)
      egen sometotal=rowtotal(somevalue?)
      
      gen cumpct0 = 0
      forvalues C=1/6 {
         local C0 = `C'-1
         gen cumpct`C' = cumpct`C0' + 100*somevalue`C'/sometotal
      }
      
      twoway (rarea cumpct0 cumpct1 date , vertical) ///
             (rarea cumpct1 cumpct2 date , vertical) ///
             (rarea cumpct2 cumpct3 date , vertical) ///
             (rarea cumpct3 cumpct4 date , vertical) ///
             (rarea cumpct4 cumpct5 date , vertical) ///
             (rarea cumpct5 cumpct6 date , vertical), ///
             scheme(tufte)
      Click image for larger version

Name:	Graph2.png
Views:	1
Size:	22.4 KB
ID:	567819
      Kind regards,
      Konrad
      Version: Stata/IC 13.1

      Comment


      • #4
        Konrad,

        Her is my suggestion for a black-grey-white graph

        Code:
        twoway (rarea cumpct0 cumpct1 date , vertical lcolor(black) fcolor(black)) ///
               (rarea cumpct1 cumpct2 date , vertical lcolor(black) fcolor(gs5))   ///
               (rarea cumpct2 cumpct3 date , vertical lcolor(black) fcolor(gs8))   ///
               (rarea cumpct3 cumpct4 date , vertical lcolor(black) fcolor(gs11))  ///
               (rarea cumpct4 cumpct5 date , vertical lcolor(black) fcolor(gs14))  ///
               (rarea cumpct5 cumpct6 date , vertical lcolor(black) fcolor(white)) ///
           ,                                                                       ///
           xlabel(2020(20)2100)                                                    ///
           legend(title("Age group", size(*0.8))                                   ///
              order(6 "0-14" 5 "15-29" 4 "30-44" 3 "45-59" 2 "60-74" 1 "75+"))     ///
           plotregion(margin(zero)) xsize(4.4) ysize(2.5) scale(1.3) scheme(lean1)

        Comment


        • #5
          Sorry - I made the legend wrong; it should be:

          Code:
             legend(title("Age group", size(*0.8))                                   ///
                order(6 "75+" 5 "60-74" 4 "45-59" 3 "30-44" 2 "15-29" 1 "0-14"))     ///

          Comment


          • #6
            Here is the result:

            Comment


            • #7
              Setting aside the tuning, does it really work? I can't read much structure off (*) that graph except that the biggest change is in the oldest category.

              (* At StataCorp, and perhaps elsewhere, that would be "off of".)

              Incidentally, are these quantities really additive?

              Here is another take using subsetplot (SSC). Here's the code:

              Code:
              sort cohort date
              set scheme s1color
              subsetplot line somevalue date, by(cohort) xtitle("") ytitle("") combine(imargin(small) l1title(some value) b2title(marching into the future...))
              http://www.statalist.org/forums/foru...ailable-on-ssc was the announcement of subsetplot.

              There's a long-term agenda to give subsetplot more handles for better combination of plots. Here we need y axis labels only on the left and x axis labels only on the bottom. That can be fixed in the graph editor but the first column of graphs ends up too small.
              Click image for larger version

Name:	konrad.png
Views:	1
Size:	43.5 KB
ID:	571523
              Last edited by Nick Cox; 21 Dec 2014, 04:57.

              Comment


              • #8
                Svend/Nick,

                Thanks very much for the useful comments. The subsetplot approach is definitely interesting, although overlapping lines developed with Tufte's scheme via the twoway command also look nice.
                Kind regards,
                Konrad
                Version: Stata/IC 13.1

                Comment


                • #9
                  I have a similar project - attempting to get a stacked area graph much like the one produced here.

                  I am having some issues though with the graph not stacking and merely overlapping. See file below.

                  Code I have been attempting to use is:

                  twoway (rarea zero Comms2 Year2 , vertical) (rarea Comms2 EGW2 Year2 , vertical) (rarea EGW2 NCE2 Year2 , vertical) (rarea NCE2 PAD2 Year2 , vertical) (rarea PAD2 AGFFH2 Year2 , vertical) (rarea AGFFH2 Mining2 Year2, vertical) (rarea Mining2 ERRHPS2 Year2, vertical) (rarea ERRHPS2 FIRBS2 Year2, vertical) (rarea FIRBS2 TS2 Year2 , vertical) (rarea TS2 Construct2 Year2 , vertical) (rarea Construct2 Comms2 Year2 , vertical) (rarea Comms2 WRT2 Year2 , vertical) (rarea WRT2 Man2 Year2 , vertical)

                  Any help for a Stata novice?


                  Attached Files

                  Comment

                  Working...
                  X