@dromano This worked, previously I had thought of adding intervention lines (Vertical Lines on the X Axis of the graph) using abline(v=10, col="blue") however I keep getting an error
Error in int_abline(a = a, b = b, h = h, v = v, untf = untf, ...) : plot.new has not been called yet
I did see that there was a way that you could do this by column number however since by dataset it always updating every day I think it would be better to use the date right?
Oh I see how other examples online use aes(). I believe my brackets are incorrect here, should there be one after .asDate instead of extra at the end? What I tried did not work
Sorry again -- I chased another red herring (aes()): Could you load the lubridate package and change as.Date() to as_datetime()? The dateRep column is a date-time object, not a date, so that may be the issue.
No problem @dromano I appreciate all your help. I have one last question before I sign off for the day, can you tell me where am I am going wrong with this syntax, I want to add lables to our lines however this seems to put the word "Hello" on all points not my line.
dataRep is a POSIXct object so you should use as.POSIXct(), also, since this line is for annotation purposes instead of mapping a variable to an aesthetic, it is better to use annotate() function, otherwise, you would be drawing the same line 7904 times (one for each row).
#these libraries are necessary
library(readxl)
library(httr)
library(tidyverse)
#create the URL where the dataset is stored with automatic updates every day
url <- paste("https://www.ecdc.europa.eu/sites/default/files/documents/COVID-19-geographic-disbtribution-worldwide-",format(Sys.time(), "%Y-%m-%d"), ".xlsx", sep = "")
#download the dataset from the website to a local temporary file
GET(url, authenticate(":", ":", type="ntlm"), write_disk(tf <- tempfile(fileext = ".xlsx")))
#read the Dataset sheet into “R”
data <- read_excel(tf)
data %>%
filter(countriesAndTerritories %in% c("United_States_of_America", "Iran", "Italy")) %>%
arrange(countriesAndTerritories, dateRep) %>%
group_by(countriesAndTerritories) %>%
mutate(cum_cases = cumsum(cases)) %>%
filter(cum_cases >= 100) %>%
ggplot(aes(dateRep, cases, colour = countriesAndTerritories)) +
geom_point() +
scale_y_log10() +
geom_smooth() +
annotate(geom = "vline",
x = as.POSIXct("2020-03-13"),
xintercept = as.POSIXct("2020-03-13"),
linetype="dotted",
color = "blue",
size=1.5)
The same goes for geom_text() if you want to annotate your plot use annotate(geom = "text", ...)
Interesting perspective, @andresrcs does the Annotate() have a variable I can use label="" to create a label for this line? I see your comment about geom_text(), does that mean I can add in a label here.