Announcement

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

  • Graph mirrored on x axis.

    Dear Statalist,

    I want to graph my two variables Eff...DOWN and Eff...UP across time (variable FlexYear) and mirrored on the xaxis (Eff...UP above xaxis, Eff...DOWN below xaxis). The yaxis should be in % of total observations starting with 0% at y intercept and then going to x% (would take the automatic maximum) on each side of the y axis.

    My approaches have been:

    1st based on a textbook, but the problem is that here I cannot use a percent command.
    Code:
    twoway bar EffectiveSpread_DEAL_ESPR_UP FlexYear (bar EffectiveSpread_DEAL_ESPR_DOWN FlexYear)
    2nd try gives me the correct output I am looking for the first variable, but combining it with my second variable leads to the graphic overlapping.
    Code:
    graph bar (percent) EffectiveSpread_DEAL_ESPR_DOWN, over(FlexYear, gap(2))

    I have the following data (data export only contains year 2018 but data set ranges from 1999 to 2018):
    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input float(EffectiveSpread_DEAL_ESPR_DOWN EffectiveSpread_DEAL_ESPR_UP FlexYear)
    0 1 2018
    0 1 2018
    0 1 2018
    0 0 2018
    0 1 2018
    0 0 2018
    0 0 2018
    0 1 2018
    0 1 2018
    0 1 2018
    0 0 2018
    0 1 2018
    0 0 2018
    0 1 2018
    0 0 2018
    0 0 2018
    0 1 2018
    0 1 2018
    0 1 2018
    0 1 2018
    0 1 2018
    0 1 2018
    0 0 2018
    0 1 2018
    0 1 2018
    0 1 2018
    0 1 2018
    0 1 2018
    0 0 2018
    0 0 2018
    0 1 2018
    0 0 2018
    0 0 2018
    0 1 2018
    0 0 2018
    0 1 2018
    0 0 2018
    0 0 2018
    0 0 2018
    0 1 2018
    0 0 2018
    0 0 2018
    0 0 2018
    0 1 2018
    0 0 2018
    0 0 2018
    0 0 2018
    0 0 2018
    0 1 2018
    0 0 2018
    0 0 2018
    0 0 2018
    0 0 2018
    0 1 2015
    0 0 2018
    0 0 2018
    0 1 2018
    1 0 2018
    0 1 2018
    0 1 2018
    0 1 2018
    0 1 2018
    1 0 2018
    0 1 2018
    0 1 2018
    0 0 2018
    0 0 2018
    0 0 2018
    0 0 2018
    0 0 2018
    0 0 2018
    0 0 2018
    0 0 2018
    0 0 2018
    0 1 2018
    0 0 2018
    1 0 2018
    0 1 2018
    0 0 2018
    0 1 2018
    0 0 2018
    1 0 2018
    0 1 2018
    0 0 2018
    0 1 2018
    0 1 2018
    0 1 2018
    0 1 2018
    0 1 2018
    0 0 2018
    0 1 2018
    1 0 2018
    1 0 2018
    0 1 2018
    1 0 2018
    1 0 2018
    0 1 2018
    0 1 2018
    1 0 2018
    1 0 2018
    end

  • #2
    2nd try gives me the correct output I am looking for the first variable, but combining it with my second variable leads to the graphic overlapping.
    Code:
    graph bar (percent) EffectiveSpread_DEAL_ESPR_DOWN, over(FlexYear, gap(2))
    The graph simply gives you percentage frequencies across years which you can calculate yourself. The rest is a matter of changing the sign of one set of values and relabeling the axis, although I am not quite sure if this is all that you wanted. As always, it is best to have your data in long layout.

    Code:
    *CALCULATE PERCENTAGE FREQUENCIES
    bys FlexYear: egen f1= count(EffectiveSpread_DEAL_ESPR_UP)
    replace f1= ((f1/_N)*100)
    bys FlexYear: egen f2= count(EffectiveSpread_DEAL_ESPR_DOWN)
    replace f2= -((f2/_N)*100)
    
    *RESHAPE AND GRAPH
    gen id= _n
    reshape long f, i(id) j(which)
    lab def which 1 "up" 2 "down"
    lab values which which
    gr bar f,  over(which) over(FlexYear) ylab(-100 "100" -80 "80" -60 "60" ///
    -40 "40" -20 "20" 0(20)100) scheme(s1color)  yline(0, lcolor(black)) ///
    ytitle("Frequency (%)") bar(1, color(none))
    Res.:
    Click image for larger version

Name:	Graph.png
Views:	1
Size:	109.5 KB
ID:	1536956

    Last edited by Andrew Musau; 17 Feb 2020, 09:06.

    Comment


    • #3
      Thank you for your reply. Your graph goes into the right direction but I want to get: Screenshot 2020-02-18 at 09.54.53.png.
      Your graph gives the same % for up and down but that is not correct as the total cannot exceed 100%... form a number of deals in a given year say 10% are flexed up and 5% down IN A SPECIFIC YEAR (the rest is 0, 0 in my data set = not flexed). The % should be calculated for each year separately. Additionally, can I do it without reshape?

      Code:
       
       * Example generated by -dataex-. To install: ssc install dataex clear input float(EffectiveSpread_DEAL_ESPR_DOWN EffectiveSpread_DEAL_ESPR_UP FlexYear) 0 1 2018 0 1 2018 0 1 2018 0 0 2018 0 1 2018 0 0 2018 0 0 2018 0 1 2018 0 1 2018 0 1 2018 0 0 2018 0 1 2018 0 0 2018 0 1 2018 0 0 2018 0 0 2018 0 1 2018 0 1 2012 0 1 2012 0 1 2012 0 1 2012 0 1 2012 0 0 2012 0 1 2012 0 1 2012 0 1 2012 0 1 2012 0 1 2012 0 0 2012 0 0 2012 0 1 2012 0 0 2012 0 0 2012 0 1 2012 0 0 2012 0 1 2012 0 0 2012 0 0 2012 0 0 2012 0 1 2015 0 0 2015 0 0 2015 0 0 2015 0 1 2015 0 0 2015 0 0 2015 0 0 2015 0 0 2015 0 1 2015 0 0 2015 0 0 2015 0 0 2015 0 0 2015 0 1 2015 0 0 2015 0 0 2013 0 1 2013 1 0 2013 0 1 2013 0 1 2013 0 1 2013 0 1 2013 1 0 2013 0 1 2013 0 1 2013 0 0 2013 0 0 2013 0 0 2013 0 0 2013 0 0 2014 0 0 2014 0 0 2014 0 0 2014 0 0 2014 0 1 2014 0 0 2014 1 0 2014 0 1 2014 0 0 2014 0 1 2014 0 0 2016 1 0 2016 0 1 2016 0 0 2016 0 1 2016 0 1 2016 0 1 2016 0 1 2016 0 1 2016 0 0 2015 0 1 2015 1 0 2015 1 0 2015 0 1 2015 1 0 2015 1 0 2015 0 1 2015 0 1 2015 1 0 2015 1 0 2015 end

      Comment


      • #4
        They say a picture is worth 1000 words. Same logic as in #2, but switch to twoway bar. I like a plain look for this, but you can switch to the default or some other scheme if you wish.

        Code:
        * Example generated by -dataex-. To install: ssc install dataex
        clear
        input float(EffectiveSpread_DEAL_ESPR_DOWN EffectiveSpread_DEAL_ESPR_UP FlexYear)
        0 1 2018
        0 1 2018
        0 1 2018
        0 0 2018
        0 1 2018
        0 0 2018
        0 0 2018
        0 1 2018
        0 1 2018
        0 1 2018
        0 0 2018
        0 1 2018
        0 0 2018
        0 1 2018
        0 0 2018
        0 0 2018
        0 1 2018
        0 1 2012
        0 1 2012
        0 1 2012
        0 1 2012
        0 1 2012
        0 0 2012
        0 1 2012
        0 1 2012
        0 1 2012
        0 1 2012
        0 1 2012
        0 0 2012
        0 0 2012
        0 1 2012
        0 0 2012
        0 0 2012
        0 1 2012
        0 0 2012
        0 1 2012
        0 0 2012
        0 0 2012
        0 0 2012
        0 1 2015
        0 0 2015
        0 0 2015
        0 0 2015
        0 1 2015
        0 0 2015
        0 0 2015
        0 0 2015
        0 0 2015
        0 1 2015
        0 0 2015
        0 0 2015
        0 0 2015
        0 0 2015
        0 1 2015
        0 0 2015
        0 0 2013
        0 1 2013
        1 0 2013
        0 1 2013
        0 1 2013
        0 1 2013
        0 1 2013
        1 0 2013
        0 1 2013
        0 1 2013
        0 0 2013
        0 0 2013
        0 0 2013
        0 0 2013
        0 0 2014
        0 0 2014
        0 0 2014
        0 0 2014
        0 0 2014
        0 1 2014
        0 0 2014
        1 0 2014
        0 1 2014
        0 0 2014
        0 1 2014
        0 0 2016
        1 0 2016
        0 1 2016
        0 0 2016
        0 1 2016
        0 1 2016
        0 1 2016
        0 1 2016
        0 1 2016
        0 0 2015
        0 1 2015
        1 0 2015
        1 0 2015
        0 1 2015
        1 0 2015
        1 0 2015
        0 1 2015
        0 1 2015
        1 0 2015
        1 0 2015
        end
        
        bys FlexYear: egen up= total(EffectiveSpread_DEAL_ESPR_UP)
        bys FlexYear: egen down= total(EffectiveSpread_DEAL_ESPR_DOWN)
        bys FlexYear: replace up= (up/_N)*100
        bys FlexYear: replace down= -(down/_N)*100
        tw (bar up FlexYear, xlab(2012 (1) 2018)ysca(range(-33))) ///
        (bar down FlexYear, ylab(-30 "30" -20 "20" -10 "10" 0(10) 65) ///
        ytitle("% of deals in a year") xtitle("") scheme(s1mono))
        Click image for larger version

Name:	Graph.png
Views:	1
Size:	48.0 KB
ID:	1537133

        Last edited by Andrew Musau; 18 Feb 2020, 06:36.

        Comment


        • #5
          Perfect. Didnt know that the command bys can be so helpful. Thank you.

          Comment

          Working...
          X