Announcement

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

  • stacked twoway barcharts with subgroups



    Dear Statalist,

    I am looking to get some help plotting three twoway barcharts stacked with subgroups on a timeseries data. Below is a sample data I'm using.

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input byte _ID float date str2 scode float(cum_a cum_r cum_d)
    35 21988 "AP"    1    0  0
    35 21989 "AP"    1    0  0
    35 21990 "AP"    1    0  0
    35 21991 "AP"    1    0  0
    35 21992 "AP"    1    0  0
    35 21993 "AP"    3    0  0
    35 21994 "AP"    3    0  0
    35 21995 "AP"    5    0  0
    35 21996 "AP"    6    0  0
    35 21997 "AP"    7    0  0
    35 21998 "AP"    8    0  0
    35 21999 "AP"    9    1  0
    35 22000 "AP"   10    1  0
    35 22001 "AP"   12    1  0
    35 22002 "AP"   18    1  0
    35 22003 "AP"   20    1  0
    35 22004 "AP"   22    1  0
    35 22005 "AP"   43    1  0
    35 22006 "AP"  109    2  0
    35 22007 "AP"  147    2  0
    35 22008 "AP"  161    2  1
    35 22009 "AP"  189    2  1
    35 22010 "AP"  250    5  1
    35 22011 "AP"  295    5  3
    35 22012 "AP"  305    5  4
    35 22013 "AP"  338    6  4
    35 22014 "AP"  347   10  6
    35 22015 "AP"  365   10  6
    35 22016 "AP"  389   10  6
    35 22017 "AP"  401   12  7
    35 22018 "AP"  420   12  7
    35 22019 "AP"  457   16 11
    35 22020 "AP"  491   20 14
    35 22021 "AP"  500   20 14
    35 22022 "AP"  523   35 14
    35 22023 "AP"  545   42 16
    35 22024 "AP"  565   65 17
    35 22025 "AP"  610   92 20
    35 22026 "AP"  639   96 22
    35 22027 "AP"  669  120 24
    35 22028 "AP"  725  141 27
    35 22029 "AP"  781  145 29
    35 22030 "AP"  814  171 31
    35 22031 "AP"  835  231 31
    35 22032 "AP"  911  235 31
    35 22033 "AP"  970  258 31
    35 22034 "AP" 1014  287 31
    23 21988 "DL"    5    1  1
    23 21989 "DL"    4    2  1
    23 21990 "DL"    4    2  1
    23 21991 "DL"    5    2  1
    23 21992 "DL"    7    2  1
    23 21993 "DL"   10    3  1
    23 21994 "DL"   14    5  1
    23 21995 "DL"   21    5  1
    23 21996 "DL"   21    5  1
    23 21997 "DL"   24    5  1
    23 21998 "DL"   23    6  1
    23 21999 "DL"   28    6  1
    23 22000 "DL"   29    6  1
    23 22001 "DL"   33    6  1
    23 22002 "DL"   42    6  1
    23 22003 "DL"   64    6  2
    23 22004 "DL"   89    6  2
    23 22005 "DL"  112    6  2
    23 22006 "DL"  144    6  2
    23 22007 "DL"  281    8  4
    23 22008 "DL"  372    8  6
    23 22009 "DL"  423   16  6
    23 22010 "DL"  480   16  7
    23 22011 "DL"  502   16  7
    23 22012 "DL"  547   20  9
    23 22013 "DL"  639   21  9
    23 22014 "DL"  683   25 12
    23 22015 "DL"  862   27 14
    23 22016 "DL" 1023   27 19
    23 22017 "DL" 1102   28 24
    23 22018 "DL" 1451   31 28
    23 22019 "DL" 1500   31 30
    23 22020 "DL" 1505   41 32
    23 22021 "DL" 1550   52 38
    23 22022 "DL" 1593   72 42
    23 22023 "DL" 1643  207 43
    23 22024 "DL" 1668  290 45
    23 22025 "DL" 1603  431 47
    23 22026 "DL" 1498  611 47
    23 22027 "DL" 1476  724 48
    23 22028 "DL" 1518  808 50
    23 22029 "DL" 1604  857 53
    23 22030 "DL" 1702  869 54
    23 22031 "DL" 1987  877 54
    23 22032 "DL" 2177  877 54
    23 22033 "DL" 2182 1078 54
    23 22034 "DL" 2291 1092 56
    end
    format %tdDD/NN/YY date



    I'm able to create line graphs to my full satisfaction using
    Code:
    twoway (line cum_a date) (line cum_r date) (line cum_d date), ytitle("") ///
        ylabel(, labels labsize(vsmall)  angle(horizontal)) xtitle("") ///
        xlabel(#4, labels angle(vertical) format(%tdMon_DD)) ///
        by(, title("Cases by scode", size(medsmall) position(12))) ///
        by(, legend(on)) legend(order(1 "cum_a" 2 "cum_r" 3 "cum_d") size(medsmall)) ///
        scheme(_grstyle_) by(scode, style(default) imargin(small) cols(3) yrescale)
    and get graph like the ones below:
    Click image for larger version

Name:	Graph.png
Views:	1
Size:	84.4 KB
ID:	1550216



    However, instead of these line plots, I actually like to show stacked bars that will enable me to see a total value which is the sum of cum_a, cum_r and cum_d. The data is a panel data with
    Code:
    tsset _ID date
    I couldn't find anyways to stack multiple twoway bar-charts just the same way the above line graph is implemented. Was able to find these in R with ggplot2 command. Wondering if this can be done in Stata as this is my preferred software. I use Stata 15.1.

    Any help will be appreciated.

    Thanks,
    Rijo.
    Last edited by Rijo John; 30 Apr 2020, 07:14. Reason: Typos

  • #2
    I couldn't find anyways to stack multiple twoway bar-charts just the same way the above line graph is implemented. Was able to find these in R with ggplot2 command. Wondering if this can be done in Stata as this is my preferred software. I use Stata 15.1.
    I am finding it difficult to understand what you mean. In a line graph, both axes are continuous (usually time and some other variable), while in a bar chart, one axis is categorical. Unless you categorize time, I do not know how you achieve what you want. Can you post an example of such a graph?
    Last edited by Andrew Musau; 30 Apr 2020, 07:45.

    Comment


    • #3
      Originally posted by Andrew Musau

      I am finding it difficult to understand what you mean. In a line graph, both axes are continuous (usually time and some other variable), while in a bar chart, one axis is categorical. Unless you categorize time, I do not know how you achieve what you want. Can you post an example of such a graph?
      Actually, after some more searching in the forum, I stumble on a post you made using stacked area plots. And wanted to implement exactly that. But instead of using bars instead of area.
      Using your suggestion "The trick is to create cumulative categories and plot the last category first." I was able to get exactly what I wanted.
      https://www.statalist.org/forums/for...47#post1434547

      Comment


      • #4
        Originally posted by Andrew Musau

        I am finding it difficult to understand what you mean. In a line graph, both axes are continuous (usually time and some other variable), while in a bar chart, one axis is categorical. Unless you categorize time, I do not know how you achieve what you want. Can you post an example of such a graph?
        Is this possible to sort the display of sub-graphs for categories in graphs like ones below with a third variable? For example, in bar graphs with over() option you can use the sort command within over() to sort the categories by a specified third variable. However' I dont see such a sort option for the situations below. At present, as shown in the graph in my first post, the sorting is done alphabetically by the categorical variable specified in by(). I would like that categories be sorted by the values of another variable say "cum_total"

        twoway (line cum_a date) (line cum_r date), ytitle("") ///
        ylabel(, angle(horizontal)) ///
        xlabel(#4, format(%tdMon_DD)) ///
        by(, title("Cases by scode", )) ///
        by(scode)
        Last edited by Rijo John; 30 Apr 2020, 21:32.

        Comment


        • #5
          Yes, install labmask from Stata Journal, by Nick Cox. Then have a numeric variable with the desired order and label it using your string variable identifying categories. For your example in #1

          Code:
          gen order= cond(scode=="DL", 1, 2)
          *findit labmask and click link to install
          labmask order, values(scode)
          twoway (line cum_a date) (line cum_r date) (line cum_d date), ytitle("") ///
          ylabel(, labels labsize(vsmall) angle(horizontal)) xtitle("") ///
          xlabel(#4, labels angle(vertical) format(%tdMon_DD)) ///
          by(, title("Cases by scode", size(medsmall) position(12))) ///
          by(, legend(on)) legend(order(1 "cum_a" 2 "cum_r" 3 "cum_d") size(medsmall)) ///
          scheme(_grstyle_) by(order, style(default) imargin(small) cols(3) yrescale)
          Click image for larger version

Name:	Graph.png
Views:	1
Size:	67.0 KB
ID:	1550394

          Comment

          Working...
          X