Announcement

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

  • Contour plot legend

    Dear Statalist community,

    I have two questions related to the legend of contour plots:

    1) Legend width
    help clegend_option lists both height() and width() as suboptions. However, only width() seems to affect the rendition of the legend in the example below ("default" and "short" are identical).
    Code:
    * Setup
    clear all
    macro drop _all
    version 17
    sysuse sandstone.dta, clear
    
    * Contour plots
    twoway contour depth northing easting, name(default)
    twoway contour depth northing easting, name(narrow) clegend(width(*0.5))
    twoway contour depth northing easting, name(short) clegend(height(*0.5))
    twoway contour depth northing easting, name(quickfix) clegend(region(margin(t=10 b=10)))
    What am I missing? Compressing the legend by adding margins does the job, but it seems there should be a native solution.

    2) Legend positioning
    Am I right to assume that there is no way of flipping the clegend (similar to the ramp(bottom) option in Ben Jann's heatplot ado)? My goal ist to add a horizontal color ramp at the bottom of a twoway graph. The closest I got is using contourline as a workaround.
    Code:
    sysuse sandstone.dta, clear
    #delimit ;
    twoway contourline depth northing easting,
        colorlines crule(intensity) ecolor(dknavy) reversekey
        ccuts(7500(100)8000) format(%0.0f) graphregion(color(white))
        clwidth(vvvthick vvvthick vvvthick vvvthick vvvthick vvvthick)
        plegend(cols(6) pos(6) stack colgap(0) region(lcolor(none)))
    ;
    #delimit cr
    (dont't mind the ugly graph; it's all about the legend)

    I noticed that Stata 18 offers the colorvar() option but I haven't seen any horizontal color ramps in the official documentation there either. Any ideas and comments are appreciated!

    I am using Stata 17 on Windows 10.

  • #2
    Yet another curiosity I just discovered when working with twoway contourline: The help file (help twoway_contourline) states that option clwidths() takes a linewidthstylelist as input. However, it does not seem to tolerate shorthands:
    Code:
    clear all
    macro drop _all
    version 17
    sysuse sandstone.dta, clear
    
    * Generate help variables
    range x 1 10 10
    forvalues i = 1/4 {
        gen y`i' = `i'
    }
    
    * Shorthand notation: Example 1 (works as expected)
    twoway (line y1 y2 y3 y4 x, lwidth(vthick ...) name(g1))
    
    * Shorthand notation: Example 2 (does not work)
    twoway contourline depth northing easting, colorlines clwidth(vthick ...) name(g2)
    
    * Stylelist notation without shorthand: Example 3 (works, but is tedious)
    twoway contourline depth northing easting, colorlines clwidth(vthick vthick vthick vthick) name(g3)
    Sure, I can count the number of lines in my plot and write a loop to repeat the desired lwidth but this seems to be at odds with the idea of linewidthstylelist (and the documentation of twoway contourline).

    Comment


    • #3
      * Stylelist notation without shorthand: Example 3 (works, but is tedious)
      twoway contourline depth northing easting, colorlines clwidth(vthick vthick vthick vthick) name(g3)
      Just use -lwidth()- instead. Not sure that the documentation states that what you want is possible. If that is the case, contact Technical Services.

      Code:
      twoway contourline depth northing easting, colorlines lwidth(vthick) name(g3)
      On the contour legend, tw countour only supports a vertical legend.
      Last edited by Andrew Musau; 07 Mar 2024, 06:20.

      Comment

      Working...
      X