Hi thank you, I tried to close the project and rerun everything, it worked(group_by and summarize), except now that I have a different problem with the ggplot.
The code runs but it is not “reading” correctly the x-axis dates (date_weeks) and y-axis tests per 1k pop’n (Tests1k).
Here is my script:
pacman:: p_load(
rio,
here,
janitor,
lubridate,
tidyverse,
ISOweek,
ggplot2,
data.table,
reprex
)
test <- import(here("data", "raw", "sample_testing.csv")) %>%
clean_names() %>%
mutate(report_date=mdy(report_date)) %>%
mutate(report_date = format(report_date, "%m-%d-%y")) %>%
rename(
n_samples = daily_output_samples_tested,
date_report = report_date) %>%
select(region,
n_samples,
date_report,) %>%
mutate(date_report =as.Date(date_report, "%m-%d-%y")) %>%
mutate(
date_week = floor_date(date_report, week_start = 1, unit = "week"),
week_report = week(date_week),
year_report = year(date_week))
#weekly tests
weekly_counts <- test %>%
group_by(date_week, week_report, year_report, region) %>%
summarize(Weeklytest= sum(n_samples))
#test per 1k popn
test1k <- weekly_counts %>%
mutate("Tests1k" = (Weeklytest/112892781)*1000) %>%
rename ("No.of RT-PCR samples tested" = Weeklytest)
#rounded off test per 1k popn
xa <- (test1k["Tests1k"])
x1 <- round(xa, digits = 2)
testround <- bind_cols(test1k, x1)
##problem with ggplot
ggplot(testround) +
geom_area(
aes(x = date_week, y = "Tests1k7"), fill="#3281B5") +
facet_wrap(~region, nrow = 6, scales = "free_y") +
xlab("") +
ylab("Number of Rt-PCR tests conducted per 1k pop'n") +
scale_x_date(date_breaks = "1 month",
labels = function (x){
month_labels <- format(x, "%b")
year_labels <- ifelse(format(x, "%m") == "06", format(x, "%Y"), "")
paste(month_labels, "\n", year_labels)
}) +
theme_minimal() +
theme(axis.text=element_text(size = 8),
axis.text.x = element_text(vjust = 0.5, hjust=0.2),
axis.title= element_text(size = 10,face= "bold"))
dput(head(testround))
#> Error in eval(expr, envir, enclos): object 'testround' not found
structure(list(date_week = structure(c(18351, 18351, 18351, 18351,
18351, 18358), class = "Date"), week_report = c(13L, 13L, 13L,
13L, 13L, 14L), year_report = c(2020L, 2020L, 2020L, 2020L, 2020L,
2020L), region = c("Cordillera Administrative Region (CAR)",
"National Capital Region (NCR)", "Region VI: Western Visayas",
"Region VII: Central Visayas", "Region XI: Davao Region", "Cordillera Administrative Region (CAR)"
), `No.of RT-PCR samples tested` = c(472L, 3692L, 385L, 348L,
388L, 1030L), Tests1k...6 = c(0.00418095821379403, 0.0327035968756939,
0.00341031549218369, 0.00308257088644136, 0.00343688937913577,
0.00912370118688103), Tests1k...7 = c(0, 0.03, 0, 0, 0, 0.01)), class = c("grouped_df",
"tbl_df", "tbl", "data.frame"), row.names = c(NA, -6L), groups = structure(list(
date_week = structure(c(18351, 18358), class = "Date"), week_report = 13:14,
year_report = c(2020L, 2020L), .rows = structure(list(1:5,
6L), ptype = integer(0), class = c("vctrs_list_of", "vctrs_vctr",
"list"))), class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA,
-2L), .drop = TRUE))
#> date_week week_report year_report region
#> 1 2020-03-30 13 2020 Cordillera Administrative Region (CAR)
#> 2 2020-03-30 13 2020 National Capital Region (NCR)
#> 3 2020-03-30 13 2020 Region VI: Western Visayas
#> 4 2020-03-30 13 2020 Region VII: Central Visayas
#> 5 2020-03-30 13 2020 Region XI: Davao Region
#> 6 2020-04-06 14 2020 Cordillera Administrative Region (CAR)
#> No.of RT-PCR samples tested Tests1k...6 Tests1k...7
#> 1 472 0.004180958 0.00
#> 2 3692 0.032703597 0.03
#> 3 385 0.003410315 0.00
#> 4 348 0.003082571 0.00
#> 5 388 0.003436889 0.00
#> 6 1030 0.009123701 0.01
Thank you very much.
Regards,
Echo