Announcement

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

  • Add vertical line to a twoway graph

    Hey,

    I have a panel dataset and I want to create a graph that shows the development of a certain variable over time (for a certain country), plus the growth rate of this variable. Furthermore I have an indicator variable that indicates if there was a "fast growth episode" around a certain date. For each of these dates I want to plot a vertical line. Is there a way to do this?

    Thanks in advance!

    Best,
    Thomas

  • #2
    You have not provided any data example or the codes you have used for your graphs which is contrary to the forum posting guidelines (please read the FAQ section for making a meaningful post). However, if you have used any graph commands, use the -xline- option after the command for a vertical line on xaxis:

    Code:
    your graph command goes here, xline(date1 date2 date3)
    Roman

    Comment


    • #3
      Sorry about that, I wanted to add it later!

      Oh, thanks, that even works in combination with ifs.

      edit:
      Additional question. I'd like to add two sorts of vertical lines (in two different colors). Using xline two times doesn't do the job. Is there an alternative?
      Last edited by Thomas Mitterling; 16 Aug 2017, 11:58.

      Comment


      • #4
        you don't say what you tried (please read and follow the FAQ advice), but the following works for me:
        Code:
        sysuse auto
        scatter price mpg, xline(20, lcolor(red)) xline(30, lcolor(green))

        Comment


        • #5
          Sorry, I didn't have the time to write the code for an whole example. I found a dataset that suits my questions and created a (working) example, including your solutions (thanks!).

          sysuse gnp96, clear

          * Calculate growth of gnp
          gen growth = log(gnp96) - log(L.gnp96)

          * Create a dummy variable that is 1 in about 10% of the cases
          gen dummy = runiform(0, 55)
          replace dummy = dummy/100
          replace dummy = round(dummy)

          * Plot gnp, its growth rate and a vertical line where the dummy is 1
          levelsof date if dummy == 1, local(levels)
          twoway (line gnp96 date, c(l) yaxis(2)) (line growth date, c(l) yaxis(1)), xline(`levels', lcolor(green))

          Comment


          • #6
            So basically, you need a loop that would draw xline for different levels in the option. I don't know how to achieve this in a loop but if you have small number of levels, I would go for the naive approach that Rich suggested at #4 by looking at the values of the levels. I don't rule out the possibility of doing it in a more intelligent manner as I believe people in this form would do it in a more efficient way but I can't get my head around this. See if you get different suggestions from others.

            Code:
             sysuse gnp96, clear
            
            * Calculate growth of gnp
            gen growth = log(gnp96) - log(L.gnp96)
            
            * Create a dummy variable that is 1 in about 10% of the cases
            gen dummy = runiform(0, 55)
            replace dummy = dummy/100
            replace dummy = round(dummy)
            
            * Plot gnp, its growth rate and a vertical line where the dummy is 1
            levelsof date if dummy == 1, local(levels)
            
            twoway (line gnp96 date, c(l) yaxis(2)) (line growth date, c(l) yaxis(1)), ///
                xline(48, lcol(green)) /// //
                xline(52, lcol(red)) ///
                xline(66, lcol(black)) ///
                xline(73, lcol(teal)) ///
                xline(89, lcol(blue)) ///
                xline(117, lcol(grey)) ///
                xline(119, lcol(orange)) ///
                xline(121, lcol(maroon)) ///
                xline(129, lcol(navy)) ///
                xline(140, lcol(green))
            Roman

            Comment


            • #7
              If all the vertical lines are supposed to have the same color the easiest way is to use a local. If the colors are different I think there is no other way than adding them one-by-one (like at the end of your example). In my case they only have two colors, hence two locals are enough.

              Comment

              Working...
              X