Announcement

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

  • Scatter plot with time line

    Hi there,

    I would like to create a scatter plot between two variables, for example income vs consumption, but I want to adjust the result over a quarterly timeline. I could not find anywhere an answer for this specific problem.

    Regards, Adriano

  • #2
    I understand a scatter plot with points and a line plot with time on one axis. I don't follow quite what you're seeking. Can you give an example, a reference, or somehow more detail?

    Comment


    • #3
      A generated a scatter plot for 2 variables, consumption and income, and segmented it for before and after an event. The event occurs at the 3rd quarter - I have a 75% before and a 25% after in terms of time. However, the scatter plot locates the breakpoint on the middle of the x axis. I would like to adjust the spread to the real timeline.
      Attached Files

      Comment


      • #4
        I used the following command

        twoway (scatter housecons govtotaldebt ) (lfit housecons govtotaldebt if quarter <60, sort) (lfit housecons govtotaldebt if quarter >=60, sort), xline(60)

        Comment


        • #5
          xline(60) presumably makes no sense to Stata as govtotaldebt is plotted on the x axis in all your twoway calls. Perhaps you should use the value of that variable for quarter 60.

          Comment


          • #6
            More thoughts:

            0. It seems that you are not using Stata 19. Please tell us the version you are using. See FAQ Advice. https://www.statalist.org/forums/help#version

            1. Consider joining data points in time order within each group.
            2. Consider open marker symbols Oh which tolerate overlap better than default O.
            3. Consider adding time labels for first and last quarters and quarters at the end and beginning of each group.
            4. Divide consumption and debt by 1 million and state units on the graph.

            Sample code follows. Marker label positions may need tweaking.

            Code:
            * you will not need to be told how to divide by 1 million 
            
            su govtotaldebt if inlist(quarter, 59, 60), meanonly 
            local where = r(mean) 
            
            su quarter, meanonly 
            local first = r(min)
            local last = r(max) 
            
            * what follows is all one command 
            twoway 
            (scatter housecons govtotaldebt, ms(Oh) ytitle(Income (units here)) xtitle(Consumption (units here)))  
            (lfit housecons govtotaldebt if quarter <60, sort) 
            (lfit housecons govtotaldebt if quarter >=60, sort), xline(`where')
            (scatter housecons govtotaldebt if quarter == `first', ms(none) mla(quarter) mlabpos(9)) 
            (scatter housecons govtotaldebt if quarter == 59, ms(none) mla(quarter) mlabpos(3)) 
            (scatter housecons govtotaldebt if quarter == 60, ms(none) mla(quarter) mlabpos(9)) 
            (scatter housecons govtotaldebt if quarter == `last', ms(none) mla(quarter) mlabpos(9))

            Comment


            • #7
              Originally posted by Nick Cox View Post
              xline(60) presumably makes no sense to Stata as govtotaldebt is plotted on the x axis in all your twoway calls. Perhaps you should use the value of that variable for quarter 60.
              I tried that first, but didn't work. The result was the same.

              Comment


              • #8
                I have Stata 12. Thank you so much for taking the time to respond. I will explore the options suggested.

                Comment


                • #9
                  #7 is not a problem that I can decode ("didn't work").

                  I use the || notation for combining twoway calls. There are in my view quite enough parentheses to keep track of. There are typos in #6: this should be better as the final command. .


                  Code:
                  twoway
                  scatter housecons govtotaldebt, ms(Oh) ytitle(Income (units here)) xtitle(Consumption (units here))
                  || lfit housecons govtotaldebt if quarter &lt;60
                  || lfit housecons govtotaldebt if quarter &gt;=60, xline(`where')
                  || scatter housecons govtotaldebt if quarter == `first', ms(none) mla(quarter) mlabpos(9)
                  || scatter housecons govtotaldebt if quarter == 59, ms(none) mla(quarter) mlabpos(3)
                  || scatter housecons govtotaldebt if quarter == 60, ms(none) mla(quarter) mlabpos(9)
                  || scatter housecons govtotaldebt if quarter == `last', ms(none) mla(quarter) mlabpos(9)
                  Version 12 may not be a problem, but you need to try. I've lost access to Stata 12 long since.

                  By the way, I am guessing that your quarter variable is just 1 up for your data, but using Stata quarterly dates may help the reader.

                  Comment

                  Working...
                  X