Skip to content

Instantly share code, notes, and snippets.

@tpoisot
Last active September 17, 2022 01:22
Show Gist options
  • Save tpoisot/3333254bab677d7b510e7cdb5474e9ac to your computer and use it in GitHub Desktop.
Save tpoisot/3333254bab677d7b510e7cdb5474e9ac to your computer and use it in GitHub Desktop.
COVID data Québec
using DataFrames
using DataFramesMeta
using CSV: CSV
using CairoMakie
using AlgebraOfGraphics
using Dates
using Downloads
CairoMakie.activate!(; px_per_unit=2)
# Get the csv file from histovigie
tmpfile = Downloads.download(
"https://msss.gouv.qc.ca/professionnels/statistiques/documents/covid19/COVID19_Qc_RapportINSPQ_HistoVigie.csv",
)
# Convert to a DataFrame
raw_data = DataFrame(CSV.File(tmpfile))
# Remove all rows where the date is unknown
@subset!(raw_data, :Date .!= "Date inconnue")
# Convert the date column to actual dates
@transform!(raw_data, :Date = Date.(:Date))
# Select the variables up to the day before
@subset!(raw_data, :Date .< (Dates.today() - Dates.Day(1)))
# Only keep the variables of interest
@select!(raw_data, :Date, :Nb_Nvx_Cas, :Nb_Cas_Cumulatif, :Nb_Nvx_Deces_Total, :Nb_Deces_Cumulatif_Total)
# Reshape the variables
long_data = stack(raw_data, Not(:Date))
long_data.Status = [ifelse(occursin("Nvx", row.variable), "New", "Cumulative") for row in eachrow(long_data)]
long_data.Measure = [ifelse(occursin("Cas", row.variable), "Cases", "Deaths") for row in eachrow(long_data)]
@select!(long_data, :Date, :Measure, :Status, :Number = :value)
# Make a new df
covid = copy(long_data)
# Extract the year
@transform!(covid, :Year = Dates.value.(Dates.Year.(:Date)))
#@subset!(covid, Dates.Month(7) .<= Dates.Month.(:Date) .<= Dates.Month(9) )
# Day in year
function day_in_year(date::Date)
jan1 = Dates.Date(Dates.year(date), 1, 1)
return Dates.value(date - jan1)
end
_dat = data(filter(r -> r.Status != "Cumulative", covid))
_mappings = [
mapping(color=:Year => nonnumeric, row=:Measure),
mapping(:Date => day_in_year => "Day in year", :Number => "")
]
_visuals = [
visual(Scatter, markersize=3, alpha=0.5),
smooth(span=0.15, degree=6, npoints=365)
]
plt = _dat * prod(_mappings) * sum(_visuals)
draw(plt; facet=(; linkxaxes=:minimal, linkyaxes=:none)) |>
save("covid_data_inspq.png")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment