Search code examples
plotlinestatamoving-average

Bar and moving average plot


I´m trying to make this kind of chart of Covid cases and moving averages using Stata. I tried using graph bar but it plots densities. I want to make a plot against time with this kind of vertical lines. This is the plot I´m trying to make:

enter image description here

Data for any country:

https://ourworldindata.org/covid-cases?country=IND~USA~GBR~CAN~DEU~FRA


Solution

  • To create this kind of plot you can easily use the twoway command. This command allows you to combine an arbitrary number of different graphs. You are looking to combine a bar graph with a line graph. For example, the new cases plot for Great Britain can be created as follows:

    import owid-covid-data.csv
    
    keep if iso_code=="GBR"
    keep date new_cases
    gen date2 = date(date, "YMD")
    format date2 %td
    
    tsset date2 // Set data to time series format 
    tssmooth ma ma7=new_cases, window(6 1 0) // create the 7 day moving-average
    
    twoway (bar  new_cases  date2) (line  ma7 date2) 
    

    enter image description here