library(tidyverse)
url <- "https://api.coronavirus.data.gov.uk/v2/data?areaType=overview&metric=covidOccupiedMVBeds&metric=newAdmissions&metric=newCasesBySpecimenDate&metric=newDeaths28DaysByDeathDate&metric=newPeopleReceivingFirstDose&format=csv"
data <- read_csv(url)
#> Rows: 782 Columns: 9
#> ── Column specification ────────────────────────────────────────────────────────
#> Delimiter: ","
#> chr (3): areaCode, areaName, areaType
#> dbl (4): covidOccupiedMVBeds, newAdmissions, newCasesBySpecimenDate, newDea...
#> lgl (1): newPeopleReceivingFirstDose
#> date (1): date
#>
#> ℹ Use `spec()` to retrieve the full column specification for this data.
#> ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
data
#> # A tibble: 782 × 9
#> areaCode areaName areaType date covidOccupiedMVBe… newAdmissions
#> <chr> <chr> <chr> <date> <dbl> <dbl>
#> 1 K02000001 United Kingdom overview 2022-03-21 299 NA
#> 2 K02000001 United Kingdom overview 2022-03-20 280 NA
#> 3 K02000001 United Kingdom overview 2022-03-19 277 NA
#> 4 K02000001 United Kingdom overview 2022-03-18 287 1879
#> 5 K02000001 United Kingdom overview 2022-03-17 292 2004
#> 6 K02000001 United Kingdom overview 2022-03-16 281 2067
#> 7 K02000001 United Kingdom overview 2022-03-15 281 2077
#> 8 K02000001 United Kingdom overview 2022-03-14 280 1946
#> 9 K02000001 United Kingdom overview 2022-03-13 257 1806
#> 10 K02000001 United Kingdom overview 2022-03-12 252 1600
#> # … with 772 more rows, and 3 more variables: newCasesBySpecimenDate <dbl>,
#> # newDeaths28DaysByDeathDate <dbl>, newPeopleReceivingFirstDose <lgl>
tidy <- data |>
select(-newPeopleReceivingFirstDose) |>
pivot_longer(
names_to = "Data",
cols = c(
newCasesBySpecimenDate,
covidOccupiedMVBeds,
newAdmissions,
newDeaths28DaysByDeathDate
)
) |>
mutate(
Data = fct_recode(Data,
"New Cases" = "newCasesBySpecimenDate",
"Admissions" = "newAdmissions",
"Deaths" = "newDeaths28DaysByDeathDate",
"Ventilated" = "covidOccupiedMVBeds"
)
)
tidy
#> # A tibble: 3,128 × 6
#> areaCode areaName areaType date Data value
#> <chr> <chr> <chr> <date> <fct> <dbl>
#> 1 K02000001 United Kingdom overview 2022-03-21 New Cases 56268
#> 2 K02000001 United Kingdom overview 2022-03-21 Ventilated 299
#> 3 K02000001 United Kingdom overview 2022-03-21 Admissions NA
#> 4 K02000001 United Kingdom overview 2022-03-21 Deaths 19
#> 5 K02000001 United Kingdom overview 2022-03-20 New Cases 77509
#> 6 K02000001 United Kingdom overview 2022-03-20 Ventilated 280
#> 7 K02000001 United Kingdom overview 2022-03-20 Admissions NA
#> 8 K02000001 United Kingdom overview 2022-03-20 Deaths 63
#> 9 K02000001 United Kingdom overview 2022-03-19 New Cases 66061
#> 10 K02000001 United Kingdom overview 2022-03-19 Ventilated 277
#> # … with 3,118 more rows
last2 <- function (x, y) {
miss <- is.na(x) | is.na(y)
x <- x[!miss]
y <- y[!miss]
y[order(x)][length(y)]
}
tidy |>
mutate(Data = fct_reorder2(Data, date, value, .fun = last2)) |>
ggplot(aes(date, value, colour = Data)) +
geom_point(colour = "gray80") +
geom_smooth(span = 0.1, se = FALSE) +
labs(
title = "UK COVID-19",
x = NULL,
y = "Daily rate",
colour = NULL
) +
scale_x_date(date_breaks = "2 months", labels = scales::label_date_short()) +
scale_y_log10(
labels = scales::label_comma(),
breaks = 10^(0:5)
)
#> Warning: Transformation introduced infinite values in continuous y-axis
#> Transformation introduced infinite values in continuous y-axis
#> `geom_smooth()` using method = 'loess' and formula 'y ~ x'
#> Warning: Removed 172 rows containing non-finite values (stat_smooth).
#> Warning: Removed 151 rows containing missing values (geom_point).
-
-
Save hadley/d54895557fbb0fe0402d2277b901120a to your computer and use it in GitHub Desktop.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment