Announcement

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

  • reference lines on top of -tw contour- plot

    Dear all,

    consider the following:

    Code:
    sysuse sandstone, clear
    
    sum northing
    local mean_north = r(mean)
    sum easting
    local mean_east = r(mean)
    
    twoway contour depth northing easting, yline(`mean_north') xline(`mean_east')
    The above code produces a graph where the reference lines xline and yline are hidden under the contour plot. I would like to make the two reference lines visible, that is, to put them on top of the contour plot.

    I have tried the following...

    Code:
    twoway contour depth northing easting || ///
                scatter northing easting, mcolor(none) yline(`mean_north') xline(`mean_east')
    ... hoping that the scatter with invisible markers will come on top of the contour plot, together with the reference lines so that they be visible, but this did not work either.

    Any suggestions?

    Thank you.

    Ivica

    // ivica_rubil //

  • #2
    Your intuition to add the reference lines to other layers was correct, but you just needed to take a slightly different approach:

    Code:
    sysuse auto, clear
    qui: su northing
    
    // Store the location for the y-axis reference line
    loc yline = r(mean)
    
    // Store the minimum value of y to reference later
    loc xliymin = r(min)
    
    // Store the maximum value of y to reference later
    loc xliymax = r(max)
    
    qui: su easting
    
    // Store the location for the reference line on the x-axis
    loc xline = r(mean)
    
    // Store the minimum value of x to reference later
    loc ylixmin = r(min)
    
    // Store the maximum value of x to reference later
    loc ylixmax = r(max)
    
    // Create the graph
    twoway contour depth northing easting || /// Contour plot for the base
    scatteri `yline' `ylixmin' `yline' `ylixmax', c(l) lc(black) lw(thick) lp(solid) m(i) || /// use the minimum/maximum of easting for x coordinates for the y-axis reference line
    scatteri `xliymin' `xline' `xliymax' `xline', c(l) lc(black) lw(thick) lp(dash) m(i) // use the minimum/maximum of northing for y coordinates for the x-axis reference line
    There may be more elegant solutions available, but this is at least a quick/dirty way to get the job done.

    Comment


    • #3
      Thank you, William - it works perfectly.

      ir

      // ivica_rubil //

      Comment


      • #4
        William's code is good but a minor tweak is possible. Something like

        Code:
        su easting, meanonly
        is sufficient when all you want are min, mean, max. (The name meanonly is a little misleading here.) That could be a noticeable timesaver for really big file or doing it repeatedly.

        Comment

        Working...
        X