Announcement

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

  • Using Area Plots - Specify area to shade and calculate area under curve

    Hello:
    I am trying to create twoway Area Plots and I would like to:

    1. Specify that only the area below a certain threshold (the set base in the command) is shaded, rather than what seems to be the Stata default to shade areas above and below. Say base is 81. I would only want to shade the areas of dt where level<81
    This is the command I used:

    Code:
    twoway (area level dt, base(81))
    2. I would then like to calculate an area under the curve for this shaded region. I was considering using the pharmacokinetics -pk- commands, as those seem to be in a similar process. But I didn't know if that is the best method or if there are more appropriate methods.

    My data is below (please note just to explain that I have this set as time series and I used tsfill to fill in with missings, which is why there are so many - however I don't think that should affect our methods).

    Thank you for any insight!

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input double dt int level
    1.9742361e+12 75
    1974236160000  .
    1974236220000  .
    1974236280000  .
    1974236340000 72
    1.9742364e+12  .
    1974236460000  .
    1974236520000  .
    1974236580000  .
    1974236640000 68
    1.9742367e+12  .
    1974236760000  .
    1974236820000  .
    1974236880000  .
    1974236940000 74
     1.974237e+12  .
    1974237060000  .
    1974237120000  .
    1974237180000  .
    1974237240000 74
    1.9742373e+12  .
    1974237360000  .
    1974237420000  .
    1974237480000  .
    1974237540000 70
    1.9742376e+12  .
    1974237660000  .
    1974237720000  .
    1974237780000  .
    1974237840000 78
    1.9742379e+12  .
    1974237960000  .
    1974238020000  .
    1974238080000  .
    1974238140000 82
    1.9742382e+12  .
    1974238260000  .
    1974238320000  .
    1974238380000  .
    1974238440000 87
    1.9742385e+12  .
    1974238560000  .
    1974238620000  .
    1974238680000  .
    1974238740000 90
    1.9742388e+12  .
    1974238860000  .
    1974238920000  .
    1974238980000  .
    1974239040000 96
    1.9742391e+12  .
    1974239160000  .
    1974239220000  .
    1974239280000  .
    1974239340000 96
    1.9742394e+12  .
    1974239460000  .
    1974239520000  .
    1974239580000  .
    1974239640000 95
    1.9742397e+12  .
    1974239760000  .
    1974239820000  .
    1974239880000  .
    1974239940000 93
      1.97424e+12  .
    1974240060000  .
    1974240120000  .
    1974240180000  .
    1974240240000 94
    1.9742403e+12  .
    1974240360000  .
    1974240420000  .
    1974240480000  .
    1974240540000 96
    1.9742406e+12  .
    1974240660000  .
    1974240720000  .
    1974240780000  .
    1974240840000 90
    1.9742409e+12  .
    1974240960000  .
    1974241020000  .
    1974241080000  .
    1974241140000 90
    1.9742412e+12  .
    1974241260000  .
    1974241320000  .
    1974241380000  .
    1974241440000 86
    1.9742415e+12  .
    1974241560000  .
    1974241620000  .
    1974241680000  .
    1974241740000 85
    1.9742418e+12  .
    1974241860000  .
    1974241920000  .
    1974241980000  .
    1974242040000  .
    end
    format %tc dt
    Last edited by Leonard Scott; 18 Mar 2023, 03:54.

  • #2
    Interpolate (extrapolate) the points in between (at the end) and then have two twoway calls.

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input double dt int level
    1.9742361e+12 75
    1974236160000  .
    1974236220000  .
    1974236280000  .
    1974236340000 72
    1.9742364e+12  .
    1974236460000  .
    1974236520000  .
    1974236580000  .
    1974236640000 68
    1.9742367e+12  .
    1974236760000  .
    1974236820000  .
    1974236880000  .
    1974236940000 74
     1.974237e+12  .
    1974237060000  .
    1974237120000  .
    1974237180000  .
    1974237240000 74
    1.9742373e+12  .
    1974237360000  .
    1974237420000  .
    1974237480000  .
    1974237540000 70
    1.9742376e+12  .
    1974237660000  .
    1974237720000  .
    1974237780000  .
    1974237840000 78
    1.9742379e+12  .
    1974237960000  .
    1974238020000  .
    1974238080000  .
    1974238140000 82
    1.9742382e+12  .
    1974238260000  .
    1974238320000  .
    1974238380000  .
    1974238440000 87
    1.9742385e+12  .
    1974238560000  .
    1974238620000  .
    1974238680000  .
    1974238740000 90
    1.9742388e+12  .
    1974238860000  .
    1974238920000  .
    1974238980000  .
    1974239040000 96
    1.9742391e+12  .
    1974239160000  .
    1974239220000  .
    1974239280000  .
    1974239340000 96
    1.9742394e+12  .
    1974239460000  .
    1974239520000  .
    1974239580000  .
    1974239640000 95
    1.9742397e+12  .
    1974239760000  .
    1974239820000  .
    1974239880000  .
    1974239940000 93
      1.97424e+12  .
    1974240060000  .
    1974240120000  .
    1974240180000  .
    1974240240000 94
    1.9742403e+12  .
    1974240360000  .
    1974240420000  .
    1974240480000  .
    1974240540000 96
    1.9742406e+12  .
    1974240660000  .
    1974240720000  .
    1974240780000  .
    1974240840000 90
    1.9742409e+12  .
    1974240960000  .
    1974241020000  .
    1974241080000  .
    1974241140000 90
    1.9742412e+12  .
    1974241260000  .
    1974241320000  .
    1974241380000  .
    1974241440000 86
    1.9742415e+12  .
    1974241560000  .
    1974241620000  .
    1974241680000  .
    1974241740000 85
    1.9742418e+12  .
    1974241860000  .
    1974241920000  .
    1974241980000  .
    1974242040000  .
    end
    format %tc dt
    
    ipolate level dt, g(level2) epolate
    set scheme s1mono
    twoway (area level2 dt, base(81) lc(black) fc(gray)) ///
    (area level2 dt if level2>81, base(81) lc(black) fc(white)), ///
    xtitle("") xlab(, labsize(small)) ytitle("Some description") ///
    xsc(range(. `=tc(24jul2022 00:40)')) leg(off)
    Res.:

    Click image for larger version

Name:	Graph.png
Views:	1
Size:	36.0 KB
ID:	1706122

    Comment


    • #3
      #2: For calculating area, see

      Code:
      help integ
      Time is measured in milliseconds and this is multiplied by whatever units you have for your y variable. So you need to think about any rescaling that you need to do. If you want to report the area underneath the threshold as a proportion of the entire area, then that is fine as it is unitless.
      Last edited by Andrew Musau; 18 Mar 2023, 07:58.

      Comment


      • #4
        Thank you Andrew Musau this was very helpful.

        Comment

        Working...
        X