0
p = ggplot(cases, aes(x="Specimen date", y="Daily lab-confirmed cases", group = 1)) + geom_point() + geom_line() + labs(title = "Daily COVID-19 Cases")
p.save(filename = date_today, height=5, width=15, units = 'in', dpi=1000)

This is my current code to plot a graph from a DataFrame containing COVID-19 cases in England, which is then saved. I'm trying to add a trend line that is similar to the Worldometer graphs (as shown below).

I cannot post images yet, so I will provide the example here.

This is what my graph currently looks like.

I am trying to achieve the '3-day moving average' and the '7-day moving average'.

1 Answers1

0

See stat_smooth, you can smooth using a moving average.

For example you may end up adding code like

+ stat_smooth(method='mavg', method_args={'window': 3}, color='cyan')
+ stat_smooth(method='mavg', method_args={'window': 7}, color='blue')

But this will not give you a legend, because the moving average is not a variable (with a corresponding value) in the dataframe, i.e. given what you want to plot the data is not tidy. So if you want a legend you will have to compute the moving average, create a tidy dataframe and plot the computed averages that are in tidy form.

How? Use pandas.melt e.g.

# Compute moving averages
cases['mavg_3'] = cases['Daily lab-confirmed cases'].rolling(window=3).mean()
cases['mavg_7'] = cases['Daily lab-confirmed cases'].rolling(window=7).mean()

# Convert data Long/tidy format
cases_long = cases.melt(
    id_vars=['Specimen date', 'Daily lab-confirmed cases'],
    value_vars=['mavg_3', 'mavg_7'],
    var_name='mavg_type',
    value_name='mavg_value'
)

# Plot tidy data
(ggplot(cases_long, aes(x="Specimen date", y="Daily lab-confirmed cases"))
 + geom_point()
 + geom_line(aes(y='mavg_value', color='mavg_type'), na_rm=True)
)
has2k1
  • 2,095
  • 18
  • 16
  • I'll have to do some research into this as I'm still fairly new to dataframes and such. However, you've pointed me in the right direction so thank you very much! – SpiritedByte Jul 23 '20 at 23:27