Announcement

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

  • Problem shading area between two vertical lines

    I'm using Stata/IC 14.2.

    I want to shade the area between two vertical lines, but have two problems:
    1. The shading does not cover the entire vertical range of the plot. I believe I could fix this if there were a way to identify the maximum and minimum y values that Stata includes in graph region, but I have been unable to figure out how to do this. Is there a way to know before plotting what these values are? I am making several figures in a loop so eyeballing it won't help.
    2. The vertical lines are covered by the shading. Is there a way to fix this?
    Below is an example which illustrates what I am trying to do:

    Code:
    clear
    sysuse sp500
    
    gen t = _n
    
    twoway (scatteri -50 70  -50 80, bcolor(gs12) recast(area) plotr(m(zero))) ///
           (scatteri 90 70  90 80, bcolor(gs12) recast(area) plotr(m(zero))) ///
           (scatter change t), ///
           xline(70 80, lcolor(black) lpattern(dash)) ///
           legend(off)
    This produces the following figure:
    Click image for larger version

Name:	Graph.png
Views:	2
Size:	30.8 KB
ID:	1438430

    If I could replace the values of -50 and 90 with the edges of the plot area I believe that would solve #1. I'm not quite sure about #2.

  • #2
    Hi David,
    a different strategy that would solve both your problems is the following:

    Code:
    twoway (scatter change t), ///
        xline(75, lwidth(4.5) lc(gs12)) ///
        xline(70 80, lcolor(black) lpattern(dash)) ///
        legend(off)

    However, you have to play a bit with the width of the vertical shaded line.

    Raffaele

    Comment


    • #3
      Thanks Raffaele, that worked great!

      Comment


      • #4
        Another (somewhat ad hoc) solution in case anyone else encounters this issue is to simply include several xlines in between the values you care about, i.e.:
        Code:
        clear
        sysuse sp500
        
        gen t = _n
        
        twoway (scatter change t), ///
               xline(71 72 73 74 75 76 77 78 79, lwidth(thick) lcolor(gs12)) ///
               xline(70 80, lcolor(black) lpattern(dash)) ///
               legend(off)
        This is obviously ugly, but there are two possible difficulties with the other answer that this method avoids:
        1. If you are using "combine" then it may resize the figures such that the shaded area no longer covers the intended area.
        2. If you are making several figures in a loop and always want to shade the same region, but the range of your x variable varies, you would need to change lwidth for each figure, which is difficult.

        Comment


        • #5
          Hi,
          I am trying to shade an area between two horizontal lines.

          Y lines are as follows
          yline(24.8 35, lpattern(dash) lcolor(gray))

          I tried to use
          graph twoway (scatteri 35 0 35 400, recast(area) bcolor(gs14) fcolor(gs14))

          However, it gives a shaded areas beneath 35.

          Is there a way to stop at 24.8?

          Thanks in advance

          Nicoletta

          Comment

          Working...
          X