Announcement

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

  • Draw xline on top of plot

    I would like to draw an vertical that appears on top of a plot from a "graph twoway" command. I have seen solutions on this forum that use the "scatteri" command to manually draw the vertical line.

    My problem is that I do not know beforehand what parameters to use for the "scatteri" command. That is, I don't know the rage of y-values to use for the vertical line. After the graph is produced, I can eyeball the graph and plug in numbers, but I would like to automate the process. Is there a way to figure out what y-values to use beforehand?

    I figure that if I can run the command once, without "scatteri" and then recover the y-coordinates of the plot region, I may be able to use those in a subsequent command with "scatteri." However, I do not know how to extract the y-range of a previous graph. Is there a way to find out the y-range of a graph in memory?

  • #2
    So I have an answer to the first part of your question using -scatteri-, where the line is drawn from point (x1, y1) to (x2, y2). Since you want a vertical line, set y1=y2.

    Code:
    twoway (scatteri x1 y1 x2 y2, recast(line) legend(off))
    
    Example:
    set obs 100
    gen x = rnormal(0,1)
    gen y = rnormal(0,2)
    graph twoway (sc x y) || (scatteri 2 1 -2 1, recast(line))

    Comment


    • #3
      If you just want a vertical line superimposed on the graph and you know the x-coordinate for the line, you don't need to know the range of y. Just specify the -xline()- option in your -graph twoway- command. Am I missing something here?

      Code:
      graph twoway scatter x y, xline(2)
      will give you a scatter plot of x and y with a vertical line drawn at x = 2.

      Comment


      • #4
        Clyde,

        I think the issue is that the line drawn by the xline option can fall behind or be occluded by the main graphic as in this example:
        Code:
        sysuse auto, clear
        summ mpg, meanonly
        hist mpg, xline(`r(mean)')
        Using graph twoway allows you to control the order of the graphic layers.

        Best,
        Alan

        Comment


        • #5
          Oh, I see. Thanks for clarifying that, Alan. I didn't get what the problem was.

          Comment


          • #6
            A loosely related problem is the subject of the next Speaking Stata column in Stata Journal 16(3).

            Partial automation is possible if we start with the rule that the axis range must include the minimum and maximum values in the data shown.

            Then consider that we usually want 'nice' rounded numbers to be shown. 'Nice' is easy to recognise---2 and 50 are nice but 1.9786 and 49.435 are not---but harder to define. Heckbert (1990) gave an excellent first stab at simple rules. Hardin (1995) used these ideas in a Stata context and they underlie Stata's default choices.

            We will usually round up a bit from the maximum and down a bit from the minimum. Here is some technique, illustrated on the auto data.

            Code:
            . sysuse auto, clear
            (1978 Automobile Data)
            
            . su weight, meanonly
            
            . local wmax = 500 * ceil(r(max)/500)
            
            . local wmin = 500 * floor(r(min)/500)
            
            . mac li
            * other stuff deleted
            _wmin:          1500
            _wmax:          5000
            
            . su weight
            
                Variable |        Obs        Mean    Std. Dev.       Min        Max
            -------------+---------------------------------------------------------
                  weight |         74    3019.459    777.1936       1760       4840
            The call to ceil() (think 'ceiling' if the function is new to you) rounds to the nearest multiple of 500, but always to the same value or upwards. Conversely, the call to floor() rounds to the nearest multiple of 500, but always to the same value or downwards.

            See Cox (2003) for more on building with floors and ceilings.

            There is a choice in there of 500 as a suitable step size; subject-matter knowledge will often make that choice easy, or ambitious readers may wish to think how to automate a choice.

            Cox, N. J. 2003. Stata tip 2: Building with floors and ceilings. Stata Journal 3: 446--447.

            Hardin, J. W. 1995. Calculate nice numbers for labeling or drawing grid lines.
            Stata Technical Bulletin 25: 2--3.

            Heckbert, P. S. 1990. Nice numbers for graph labels.
            In Glassner, A. S. (Ed.) Graphics Gems. San Diego, CA: Academic Press, 61--63.

            Comment

            Working...
            X