Announcement

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

  • Shading area under a line


    Dear all

    I am trying to shade the area under a line on a scatter plot but despite my best efforts i cannot seem able to make it work. I am using the following code to define the coordinates of the area i want shaded:

    Code:
     twoway scatter ...  || scatteri  ... , msymbol(none) bcolor(black) recast(connected) lpattern(solid)


    Click image for larger version

Name:	triangle.png
Views:	1
Size:	16.8 KB
ID:	1339604
    Click image for larger version

Name:	triangle2.png
Views:	1
Size:	9.4 KB
ID:	1339605



    The result is illustrated on Figure 1. But when i change the recast option to "area" the graph gets messed up as Figure 2 shows. Apparently, my guess is that i have to define coordinates for every single point in order for this to work and again i am not sure.

    I was wondering if there is a simpler way of doing this? Or does anyone have any ideas?

    Thank you.

    Cynthia

  • #2
    Do you mean something like this:

    Code:
    clear
    input x y
    0 0
    1 0
    1 1
    0 0 
    end
    scatter y x , recast(line) || scatteri 0 0 0 .75 .75 .75 0 0 , recast(area)

    Comment


    • #3
      Hi Scott, thanks for this. The scatter plot and the line/area are separate. I just need to overlay them in one graph. Apologies, I should have been more specific:

      Code:
       twoway scatter var1 var2  || line var3 var4 ///
       || scatteri  --> coordinates that bound the area (including the line) <--  , ///  
      msymbol(none) bcolor(black) recast(connected) lpattern(solid)
      But I want to shade all the area under the line kind off like you do. Do i have to specify all the coordinates? Isn't there a simpler way?

      Comment


      • #4
        I'm not entirely sure what you want. Is it something like this?

        Code:
        sysuse auto, clear
        collapse (mean) mpg price headroom trunk weight length turn, by(rep78)
        twoway area trunk rep78 || scatter headroom rep78
        Note that you want to draw the area graph in the first graph so that it is in the background and does not obscure the scatter (and/or line).
        Stata/MP 14.1 (64-bit x86-64)
        Revision 19 May 2016
        Win 8.1

        Comment


        • #5
          Hi Carole, thanks for your suggestion. After playing around a bit, I have actually managed to do what i wanted by combining your examples: save the coordinates in variables and then using these in the right order in a twoway graph.

          Comment

          Working...
          X