Announcement

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

  • kdens plot: highlight area between graphs

    Hi statalisters,

    is anyone aware of an option that highlights selected areas in a combined kdens plot without covering the whole graph?
    I tried it with _ range( ) recast(area) color( ) _ but this is not exactly what I want because it covers only the area of one graph.
    In my kdens plot, I combine actually two graphs and want to highlight the overlapping area. I use Stata14.

    The syntax I created so far is:

    foreach test in fluid2 {
    twoway (kdens `test' if abschluss_2==0 [pweight=w_t], lpattern(shortdash) bw(1) lcolor(0) ) ///
    (kdens `test' if abschluss_2==1 [pweight=w_t], lpattern(solid) bw(1) lcolor(0) ), ///
    legend(region(lwidth(none))) legend(lab(1 "max. Hauptschulabschluss") lab(2 "Mittlerer Schulabschluss")) ///
    xlab(-4(1)2, format(%9.0g)) ///
    scheme(s1mono)
    }

    and the graph looks as attached.

    So how can I add an option that allows for highlighting the overlapping area between graph 1 and 2?

    Thanks!
    J.
    Attached Files

  • #2
    kdens is a user-written (community-contributed) from SSC as you are asked to explain (FAQ Advice #12). Same place advises using .png not .tif for graph attachments as they will show up directly.

    The problem with shading in between is that the sets of observations defining the curves are disjoint, as abschluss_2 can't be 0 and 1 in the same observations. But you could try recasting either curve to an area shown transparently and then the overlap will show up distinctly.

    On a different note, several of the underlying kernels for not very many distinct values are discernible on the graph, so that raises suspicions that a discrete variable is being massaged towards continuity.

    Comment


    • #3
      Specifying the bandwidth, kernel (default), and ensuring that both KDEs are evaluated on a common set of grid points,then the overlap is the minimum of two densities:

      Code:
      clear*
      sysuse auto
      kdens mpg if fore == 1,  bw(10) scheme(538w) gen(D1 X1) range(0 60) nograph
      kdens mpg if fore == 0,  bw(10) scheme(538w) gen(D0 X0) range(0 60) nograph
      egen DensityIntersection= rowmin(D0 D1)
      
      twoway kdens mpg if fore == 0, bw(10) range(0 60)  /// 
          || kdens mpg if fore == 1,  bw(10)  range(0 60) /// 
          || area DensityIntersection X1
      Click image for larger version

Name:	Graph.png
Views:	1
Size:	35.7 KB
ID:	1443332

      Comment

      Working...
      X