Announcement

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

  • Overlaying a line with a specific slope on a scatterplot

    Dear all

    I am trying to overlay on a scatterplot a straight line that passes through the origin with a specific slope. The code that i use is the following:

    twoway scatter var1 var2 || function y=20000*x , range(-0.50 0.50)

    var1 has a min of -500 and a max of 500
    var2 has a min of 0.50 and a max of 0.50

    The problem that i have is that the line is always a 45 degrees line and the scatterplot is distorted with the points concentrated. This is because the values of the y axis are -predictably- going through the roof. The x axis that shows var2 looks fine.

    I was wondering if there is a way to draw the line without the y axis taking the values of the function. Instead i would like only the slope of the line to change (beyond 45 degrees) and keep the original values of the variable (var1). This would not distort my graph.

    Any ideas?

    Thank you!!

    Cynthia

  • #2
    Well, when x is -0.50, y is -10000, and when x is +0.50, y is +10,000. So it is inevitable that the scale for y will overpower the scale for var1 and cause the (var2, var1) ponts to be crowded into the vertical middle of the graph when you plot them together. There are ways to change the aspect ratio of the graph so that the line will not appear to be at 45 degree angle, but the (var2, var1) points will still be concentrated in the same way, perhaps even more so.

    The question in my mind is why you want to do this. What are you trying to show by overlaying such a steep line on top of these data with much more limited vertical range?

    Comment


    • #3
      Clyde explained why the y-axis has such a large range. To limit the y-axis to the range of values of var2, specify a smaller range for the function.

      Below is a reproducible example. I assume that the minimum value of var2 is 0.5, not 0.5 as you wrote.
      Code:
      clear
      input float(var1 var2)
      -500 -.5
       500  .5
      end
      twoway scatter var1 var2 || function y=20000*x , range(-0.025 0.025)

      Comment


      • #4
        Originally posted by Friedrich Huebler View Post
        I assume that the minimum value of var2 is 0.5.
        Correction: I assume that the minimum value of var2 is -0.5.

        Comment


        • #5
          This worked like a charm! I guess what i was missing was that the range in this case refers to the drawing range. Thank you both very much!!

          Comment

          Working...
          X