Announcement

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

  • Confidence area on a graph

    Dear All,

    I'd like to reflect the confidence area on the graph. This is not a big challenge, and can be done with the twoway area:

    Click image for larger version

Name:	conf_area_c.png
Views:	1
Size:	66.0 KB
ID:	1565024




    Yet one detail still makes me unhappy: by masking the gray area with a white overlap, I am losing the horizontal rules (corresponding to levels 10, 20, ...). I can not make this area transparent, because then it will not mask the gray background anymore. Yet I'd like the horizontal rule to shine through the two layers of the area-series in the chart.

    Below is a minimal program, which reproduces the problem essentially, without any actual data:
    Code:
    clear all
    
    set obs 1000
    gen x=_n
    gen y=normalden((x-500)/1000,20)
    gen y1=y-0.000003
    gen y2=y+0.000003
    
    twoway area y2 y y1 x, fc(gray gray white) lc(white%0 white%0 white%0) ///
    legend( order( 4 2) label(4 "Expected ...") label(2 "Confidence area")) ///
    || line y x, lc(red)
    And the corresponding graph:

    Click image for larger version

Name:	conf_area_cc.png
Views:	1
Size:	113.3 KB
ID:	1565025


    So far the only clear idea I have is to transform the series reflecting the confidence area into a closed polygon, and draw it similarly to how spmap renders polygons.
    Yet I was hoping for a somewhat simpler solution, such as adjusting the transparency, or some kind of options combination.


    Thank you, Sergiy Radyakin


  • #2
    You can try adding something like

    Code:
    scatteri 0.01994 -0.5 0.01994 1000, connect(l) lcolor(ebg%50) lstyle(grid) mcolor(none)

    Comment


    • #3
      Thank you, Andrew.

      I thought about adding the missing parts after the main part has been rendered.The simple line like this, drawn as a whole, will not solve it. Of the three shapes - gray, white, and line, I can't decide which one must be on top, and which one at the bottom:

      - The line must be above white;
      - The line must be under gray;
      - The white must be above gray.

      Unfortunately it is not one graph but many I need to draw, and the number of intersections may be quite large. Otherwise I could detect just the intersection points and connect them. But because the boundary of the confidence area is not smooth, but rather jagged - there could be thousands of those points, and it is a pain to detect them (If the problem was well-posed - I would rather enjoy it, as here: https://www.statalist.org/forums/for...he-second-time).

      Best, Sergiy

      Comment


      • #4
        Coming back to this, your conditions 1& 2 imply grey>line>white and 2& 3 imply grey>white>line. As far as I can see, there can be no consistent rule that satisfies this and your suggestion to detect just the intersection points and connect them may be the only viable solution. However, if your goal is to show grid lines, by approprately choosing color and opacity for the line, it is possible to create the appearance that the gridlines are in the background and the gray confidence interval is drawn with some level of transparency.

        Code:
        clear all
        
        set obs 1000
        gen x=_n
        gen y=normalden((x-500)/1000,20)
        gen y1=y-0.000003
        gen y2=y+0.000003
        
        twoway area y2 y y1 x, fc(gray gray white) lc(white%0 white%0 white%0) ///
        legend( order( 4 2) label(4 "Expected ...") label(2 "Confidence area")) ///
        || line y x, lc(red)|| scatteri 0.01994 -0.5 0.01994 1000, connect(l) lcolor(gray%30) ///
        lstyle(grid) mcolor(none)|| scatteri 0.019935 -0.5 0.019935 1000, connect(l) ///
        lcolor(gray%30) lstyle(grid) mcolor(none)|| scatteri 0.019945 -0.5 0.019945 1000, ///
        connect(l) lcolor(gray%30) lstyle(grid) mcolor(none)|| scatteri 0.01995 -0.5 0.01995 1000, ///
        connect(l) lcolor(gray%30) lstyle(grid) mcolor(none) scheme(s1color)

        Click image for larger version

Name:	Graph.png
Views:	1
Size:	36.5 KB
ID:	1565703




        Comment


        • #5
          Dear Andrew, thank you for the time and effort. I find that without manipulating the scheme, I still don't reach perfection.

          For the default color scheme (color2) I am getting the disconnects where the graph extends beyond the scale minimum (panel A).
          Yet if I take the color of the grid lines from the scheme, then I am getting panel B, where the color is obviously foreign.


          Click image for larger version

Name:	color1.png
Views:	1
Size:	17.1 KB
ID:	1565771


          My situation is a bit more complex still, since I will have multiple confidence areas for various levels of confidence, which further complicates the selection of the color strategically (detail below):
          Click image for larger version

Name:	boundary.png
Views:	1
Size:	16.4 KB
ID:	1565772



          In the end I feel it is best not to put the grid lines at all and let the user use a ruler on the screen.
          But it gives me an idea to create a totally new type of graph which would not be subject to these layer complications.

          Thank you again, Sergiy


          Comment


          • #6
            To solve problem A in #5 you might try adding the option
            Code:
            plotregion(margin(zero))

            Comment


            • #7
              Dear John,
              thank you very much for the hint!
              Very helpful indeed!
              Regards, Sergiy

              Comment

              Working...
              X