-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcovidTimeSeries.R
45 lines (44 loc) · 1.67 KB
/
covidTimeSeries.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#' @title Plot covid-19 time series
#'
#' @description Takes the data from updateCovid function and passes it on to plot a time series plot for
#' every type of data available.
#'
#' @param country name of the country
#' @param min.date a value from 2020-1-22 to today's date
#' @param max.date a value from 2020-1-22 to today's date
#' @author Ali Malek
#' @return a time series plot
#' @export
#' @import tidyverse
#'
#' @examples
#' covidTimeSeries(country = "egypt",min.date = "2020-02-22",max.date = as.Date(today()))
covidTimeSeries <- function(country = "egypt",min.date = "2020-01-22",max.date = as.Date(today())){
require(tidyverse)
min.date <- as.Date(min.date)
max.date <- as.Date(max.date)
country <- tolower(country)
if (min.date < "2020-01-22" | max.date > as.Date(today())) {
return(warning("Not a valid date"))
}
sap <- updateCovid()
sap$`Country/Region` <- tolower(sap$`Country/Region`)
if (country %in% sap$`Country/Region` != TRUE) {
return(warning("Not a valid country"))
}
sap %>% filter(`Country/Region` == country & date >= min.date & date <= max.date) -> sap
sap1 <- sap %>% filter(type == "Total Cases")
sap2 <- sap %>% filter(type == "Total Deaths")
sap3 <- sap %>% filter(type == "Total Recovered")
sap1$new <- c(sap1$count[1],diff(sap1$count))
sap2$new <- c(sap2$count[1],diff(sap2$count))
sap3$new <- c(sap3$count[1],diff(sap3$count))
sap <- rbind(sap1,sap2,sap3)
sap$new[sap$new <= 0] <- 0
title <- paste("Time Series Of",str_to_title(country),"From",min.date,"To",max.date)
sap %>% ggplot(aes(x=date,y=new)) +
geom_line(aes(group = type,color = type)) +
ggtitle(title) +
ylab("New Daily Cases") +
xlab("Time")
}