Announcement

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

  • Twoway bar and Twoway scatter in one graph

    Hi
    Is there a way to prepare a graph that shows a binary variable (recession having 0 and 1) as bars, and a continuous variable (GDP) as a time series in one graph>

    Something similar to :
    Code:
    twoway (scatter GDP quarter, connect(l) sort)(twoway bar US_recession quarter)
    The previous code shows an error message : twoway is not a twoway plot type

    Thanks

  • #2
    Omit the second twoway. Stata thinks you are trying to call up twoway twoway (as your parentheses do imply).

    I guess you could use twoway bar for the shading, but you would want to lay down the shading first, as otherwise it will just occlude the time series, unless in turn the bars were unfilled.

    See http://stackoverflow.com/questions/3...ours-histogram for one approach to background shading.
    Last edited by Nick Cox; 12 Jul 2016, 12:44.

    Comment


    • #3
      Thanks Nick. It worked fine.

      A related question:

      The binary variable has a 0/1 value. The continuous variable can take negative and positive values. The graph will then show a time series of the continuous variable (positive and negative) and bars for the binary variables, which will start from 0 to one.
      Is there a way to shade the entire column (bar) when the binary variable has a value of 1 ?
      In other words, I want to show shaded regions for the recession variable that take a columns length in the graph . Through which the times series of the continuous variable will be presented.

      Do you think I will need to recode the recession variable differently to get these shaded areas?

      Any help is appreciated.

      Comment


      • #4
        Hi
        I have attached a document here. The first figure is what I get with bars but not shaded regions. The second figure is what I want to get.
        Basically, I want my bars to represent shaded areas for the quarters rather than starting from 0 as they are now.

        hope this clarifies my request!
        Any advice?
        Attached Files

        Comment


        • #5
          As you want your bars to go from about -0.8 to 0.6 your code (not given!!!) needs to be revised. Just change the base to -0.8 and the top value to 0.6.

          Comment


          • #6
            Thanks Nick.
            Not sure if I get this.The US-recession variable is either 1 (a recessionary quarter) or 0 ( a non-recessionary quarter). How to recode the 1 value to be capture the -0.8 to 0.6 range?
            Also, is there any generic way to get shaded regions for these recessionary quarters as in the second figure ?

            Comment


            • #7
              I've already answered that. You want your shading to go from -0.8 to 0.6. The fact that you think of recession or not as a 0, 1 variable is material only in so far as it determines when such bars are or are not plotted.

              I made up some data:

              Code:
              clear
              set obs 100
              version 10: set seed 2803
              gen year = 1900 + _n
              gen war = 0.6 if inrange(year, 1914, 1918) | inrange(year, 1939, 1945)
              gen random = -0.2 + rnormal(0,0.25)
              twoway bar war year, base(-0.8) ysc(r(-0.8 0.6)) bcolor(gs14) scheme(s1color) || ///
              line random year, yla(-0.8(0.2)0.6, ang(h))
              Click image for larger version

Name:	kraft.png
Views:	1
Size:	32.6 KB
ID:	1349104




              The legend is optional, clearly, and more titles are needed, but between the top value and the base you have the bars you need.
              Last edited by Nick Cox; 12 Jul 2016, 17:41.

              Comment


              • #8
                Thanks.
                Is there any other way to get this done without having to look each time at the range of the continuous variable and then try to work this out? something like using min and max so we get shaded regions? or perhaps by assigning values to the year-quarters that are in recession instead of using a recession binary variable?

                I also tried to adopt Nick's code in my data (after adjusting the range) but I got shaded regions for all quarters ! !

                I will really appreciate if there is another way that one can generalize to all graphs of this kinds. Simply, how do we get shaded areas for the quarters when the binary variable is 1 (as in the second figure attached to my post #4) instead of having a bar that starts from zero to one consistent with the binary variable (as in the first figure attached to my post #4) ?

                I attach a sample of my data in this post. The US_recession is the binary variable and X is the continuous variable.

                Look forward to hearing from you all.

                Thanks
                Attached Files

                Comment


                • #9
                  http://www.statalist.org/forums/help#stata 12.5 does explain why .dta files are not as helpful as you might think.

                  Comment


                  • #10
                    Hi,
                    Not quite sure how this works, but I installed it and copied and pasted it as instructed.

                    Code:
                    * Example generated by -dataex-. To install: ssc install dataex
                    clear
                    input float fqdate byte US_recession float X
                     46 0            .
                     47 0            .
                     48 0            .
                     49 0            .
                     50 0    -.1800102
                     51 0   .012469874
                     52 0    .22131304
                     53 0     .4591231
                     54 1     .2324131
                     55 1     .6072696
                     56 1     .3283232
                     57 1     .4022807
                     58 1    1.0606457
                     59 1     .6966434
                     60 0     .2418955
                     61 0   -.24948415
                     62 0    -.5006028
                     63 0   -.59206825
                     64 0    -.3292846
                     65 0    -.1323471
                     66 0    -.3435568
                     67 0    -.3056247
                     68 0     .0233958
                     69 0    -.0688891
                     70 0  -.013837675
                     71 0    .05844776
                     72 0    .02865543
                     73 0  -.020653104
                     74 0    -.1778461
                     75 0    .08095168
                     76 0  -.027603416
                     77 0    .04116964
                     78 0    .06859166
                     79 1   -.08719537
                     80 1      .252221
                     81 1    .06622423
                     82 0 -.0039477334
                     83 0   -.03664225
                     84 0    -.3608751
                     85 1   -.21568656
                     86 1     .1499893
                     87 1    .06066972
                     88 1     .2596138
                     89 1     .3348131
                     90 1   .005083695
                     91 0   -.12943847
                     92 0    -.3470092
                     93 0    -.5295372
                     94 0    -.4206032
                     95 0   -.16423754
                     96 0   .016647344
                     97 0    .18129233
                     98 0     .1033366
                     99 0    .09992823
                    100 0   -.03821685
                    101 0   -.11781645
                    102 0   -.02020126
                    103 0   -.13797054
                    104 0   -.13891208
                    105 0   -.13323224
                    106 0   -.09576883
                    107 0 -.0011167721
                    108 0   -.03041052
                    109 0  -.007958412
                    110 0   -.10405996
                    111 0    .13696986
                    112 0    .14225286
                    113 0     .1165265
                    114 0    .15407714
                    115 0   -.06933147
                    116 0  -.012969925
                    117 0     -.024362
                    118 0   -.08059987
                    119 0   -.02335259
                    120 0    .04528837
                    121 1    .09234151
                    122 1     .3950249
                    123 1     .3248725
                    124 0     .0738061
                    125 0     .0865095
                    126 0    -.2085813
                    127 0   -.22222766
                    128 0    -.0952204
                    129 0   -.05046888
                    130 0   -.02778437
                    131 0    -.0993638
                    132 0   -.04690713
                    133 0   -.09430797
                    134 0   -.11994633
                    135 0  -.034005444
                    136 0   .008615996
                    137 0   .032201704
                    138 0    .04152886
                    139 0    .07542249
                    140 0    .03261491
                    141 0   -.02268741
                    142 0   -.04812483
                    143 0    -.0797161
                    144 0   -.06956914
                    145 0   -.04612602
                    end
                    format %tq fqdate

                    Comment


                    • #11
                      Thanks for the workable example. Completely general code is quite a challenge, but here is some technique. You're responsible for the step between y axis labels, but other than that the upper limit of the bars is necessarily the maximum y value rounded up some and the lower limit is the minimum y value rounded down some. This code begins where yours ends.

                      Code:
                      local magic = 0.2 
                      su X, meanonly 
                      local ymin = `magic' * floor(r(min)/`magic') 
                      local ymax = `magic' * ceil(r(max)/`magic') 
                      tempvar bar 
                      gen `bar' = `ymax' 
                      local ytitle : var label X 
                      if "`ytitle'" == "" local ytitle "X" 
                      twoway bar `bar' fqdate if US_recession, base(`ymin') bcolor(gs12) yla(`ymin'(`magic')`ymax') ///
                      || line X fqdate, legend(off) ytitle("`ytitle'")
                      Click image for larger version

Name:	recession.png
Views:	1
Size:	27.3 KB
ID:	1349233

                      Comment


                      • #12
                        Many thanks Nick. It worked well. But I had to replace the missing values of US_recession by zero as they erroneously led to shaded regions in post #11.
                        Thanks a lot.

                        Comment


                        • #13
                          Your example had values of 1 and 0 only for the recession variable. With missings as well, it would have sufficed to write

                          Code:
                          ,,, if US_recession == 1

                          Comment


                          • #14
                            Hi Mike and Nick,
                            Code:
                            ssc install bgshade
                            should take care of this problem pretty easily next time!

                            Comment


                            • #15
                              I wrote on this topic later in 2016 in https://www.stata-journal.com/articl...article=gr0067 so in turn there's a prior reference.

                              Comment

                              Working...
                              X