Announcement

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

  • Formatting post-intervention shading and dotted-line on a scatterplot

    Hi. My data set spans July 2020- July 2024. There are two indicator variables for two treatments - one starting in July 2023 and the other in Jan 2024. Once both the treatments start, they run till the end of the study period (July 2024). The scatterplot below indicates the distribution for observations (variable in data is "one") during this period. I want to shade differently the different periods of time: non-intervention period (July 2020 - July 2023) - white, first intervention period (July 2023 - Jan 2024) - light grey, second intervention period (Jan 2024 - July 2024/end of study) - dark grey.

    In addition, I would also like the line of fit to be solid in the pre-intervention period and dashed after the start of the first intervention.

    Any help in doing this will be much appreciated.



    Code:
    *Code
    
    
    gen mdate = ym(year, month)
    
    set scheme s1color
    
    su year, meanonly 
    local first = r(min)
    local last = r(max)
    
    forval y = `first'/`last' { 
        su mdate if year == `y', meanonly 
        local labelcall `labelcall' `r(mean)' "`y'"
        local tickpos = r(max) + 0.5 
        if `y' < `last' local tickcall `tickcall' `tickpos'
    }
    
    mac li 
    
    scatter one mdate,  ///
    ytitle("Number of teleconsults") yscale(range(0 .)) ylabel(#6, labsize(small) ///
    angle(horizontal) nogrid) ms(Oh) mc(black) ///
    xtick(`tickcall',  tlength(*2)) ///
    xla(`labelcall', noticks labsize(small)) ///
     xtitle("Year" "", height(6)) graphregion(fcolor(white)) || lfit one mdate, legend(off) xli(`tickcall', lc(gs12))
    
    
    
    
    
    *Data Sample
    
    input float(year month) long one byte(treatment1 treatment2)
    2020  7 1585 0 0
    2020  8 2460 0 0
    2020  9 2778 0 0
    2020 10 3284 0 0
    2020 11 3280 0 0
    2020 12 3262 0 0
    2021  1 2688 0 0
    2021  2 2849 0 0
    2021  3 2806 0 0
    2021  4 2517 0 0
    2021  5 4012 0 0
    2021  6 2665 0 0
    2021  7 2556 0 0
    2021  8 2618 0 0
    2021  9 2693 0 0
    2021 10 2247 0 0
    2021 11 2028 0 0
    2021 12 2217 0 0
    2022  1 2233 0 0
    2022  2 2224 0 0
    2022  3 2326 0 0
    2022  4 2052 0 0
    2022  5 2059 0 0
    2022  6 1656 0 0
    2022  7 1574 0 0
    2022  8 1386 0 0
    2022  9 1361 0 0
    2022 10 1071 0 0
    2022 11 1154 0 0
    2022 12 1109 0 0
    2023  1 1076 0 0
    2023  2 1264 0 0
    2023  3 1174 0 0
    2023  4  906 0 0
    2023  5  968 0 0
    2023  6 1012 1 0
    2023  7 1097 1 0
    2023  8 1517 1 0
    2023  9 1248 1 0
    2023 10 1098 1 0
    2023 11  975 1 0
    2023 12 1000 1 0
    2024  1 1112 1 1
    2024  2 1060 1 1
    2024  3  945 1 1
    2024  4 1017 1 1
    2024  5  916 1 1
    2024  6 1120 1 1
    2024  7 1174 1 1
    Click image for larger version

Name:	trendline.png
Views:	1
Size:	78.0 KB
ID:	1765510

  • #2
    This needs a cross-reference to https://www.statalist.org/forums/for...on-scatterplot

    Comment


    • #3
      Hi Nick Cox. I made a new post for this since, while it is the same graph as the one in the previous post, there are now more additions to the graph and the data it draws on as compared to the previous post.

      Could you please guide me on this request? I'd greatly appreciate it

      Comment


      • #4
        Making a new post doesn't rule out help helpful cross-references so that people can see -- in this case -- where the code came from and some previous discussion of your data and the problem.

        In this case my reaction was and is that the straight line fit is poor quantitatively and qualitatively.

        Otherwise shading scatter plots is a much discussed topic. The trick is to search with a good keyword and here

        Code:
        search shading
        reveals several items, including

        Blo
        Code:
        g    . . . . . . . . . . . . Adding recession shading to time-series graphs
                . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . D. Schenck
                2/20    http://blog.stata.com/2020/02/13/adding-recession-shading-
                        to-time-series-graphs/
        
        SJ-16-3 gr0067  . Speaking Stata: Shading zones on time series and other plots
                . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  N. J. Cox
                Q3/16   SJ 16(3):805--812                                (no commands)
                discusses background shading of time series and other plots

        Comment

        Working...
        X