Last active
February 19, 2026 19:24
-
-
Save USMortality/42044879ba891507593e9e1fc97bc525 to your computer and use it in GitHub Desktop.
7-Day Moving Average of Cases, Tests, and Positivity [Santa Clara County, CA]
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| library(ggplot2) | |
| library(slider) | |
| library(scales) | |
| library(dplyr) | |
| library(readr) | |
| library(tsibble) | |
| sf <- 2 | |
| width <- 600 * sf | |
| height <- 335 * sf | |
| options(vsc.dev.args = list(width = width, height = height, res = 72 * sf)) | |
| # Load and process the COVID-19 data | |
| df <- read_csv("https://data.sccgov.org/resource/t8ae-ku7k.csv", | |
| col_types = "ciicciiidd" | |
| ) |> | |
| select(end_date, case_count, test_count, positivity_rate) |> | |
| setNames(c("date", "cases", "tests", "positivity")) |> | |
| group_by(date) |> | |
| summarize( | |
| cases = sum(cases, na.rm = TRUE), | |
| tests = sum(tests, na.rm = TRUE), | |
| positivity = mean(positivity, na.rm = TRUE), | |
| ) |> | |
| mutate( | |
| date = as.Date(date), | |
| positivity = positivity / 100 # Convert to proportion for easier scaling | |
| ) | |
| # Calculate 7-day moving averages | |
| ts <- df |> as_tsibble(index = date) | |
| chart <- ggplot(ts, aes(x = date)) + | |
| geom_line(aes(y = cases, color = "Cases")) + | |
| geom_line(aes(y = tests, color = "Tests")) + | |
| geom_line( | |
| aes( | |
| y = positivity * max(tests, na.rm = TRUE), | |
| color = "Positivity" | |
| ), | |
| linetype = "dashed" | |
| ) + | |
| scale_y_continuous( | |
| name = "Cases / Tests", | |
| sec.axis = sec_axis( | |
| ~ . / max(ts$tests, na.rm = TRUE), | |
| name = "Positivity (%)", | |
| labels = percent_format(accuracy = 1) | |
| ), | |
| labels = scales::number_format(), # Fix for formatting | |
| ) + | |
| labs( | |
| title = paste0( | |
| "7-Day Moving Average of Cases, Tests, and Positivity ", | |
| "[Santa Clara County, CA]" | |
| ), | |
| x = "Date", | |
| color = "Metric" | |
| ) + | |
| geom_vline( | |
| xintercept = as.Date("2020-12-17"), linetype = "dashed", | |
| color = "black", size = 0.5 | |
| ) + | |
| annotate( | |
| "text", | |
| x = as.Date("2020-12-17"), y = max(ts$cases, na.rm = TRUE), | |
| label = "First Vaccine\nGiven", vjust = -8, hjust = 1.1, size = 3.5 | |
| ) + | |
| scale_color_manual( | |
| values = c( | |
| "Cases" = "green", | |
| "Tests" = "blue", | |
| "Positivity" = "red" | |
| ) | |
| ) + | |
| theme_minimal() + | |
| theme( | |
| axis.title.y.right = element_text(color = "black"), | |
| legend.position = "bottom", | |
| legend.title = element_blank(), | |
| legend.direction = "horizontal" | |
| ) | |
| ggplot2::ggsave( | |
| filename = "chart1.png", plot = chart, width = width, height = height, | |
| units = "px", dpi = 72 * sf, device = grDevices::png, type = c("cairo") | |
| ) |
Author
Author
Long Term Care Cases:
library(readr)
df <- read_csv(
paste0(
"/Users/ben/Downloads/",
"COVID-19_cases_at_Long_Term_Care_Facilities_by_date_20250228.csv"
),
col_types = "iiD"
)
df |> ggplot(aes(x = dtepisode, y = New_cases)) +
geom_line() +
labs(
title = "COVID-19 cases at Long Term Care Facilities by date",
subtitle = "Source: https://data.sccgov.org/COVID-19"
)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment

https://www.mortality.watch/charts/list.html#7-day-moving-average-of-cases-tests-and-positivity-santa-clara-county-ca