Adding Vertical Lines to plot, "Error in int_abline(...) : plot.new has not been called yet"

Originally posted to Start graph curve at (0,0) to see overlapping trend - #3 - but slipt into separate discussion.


@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

Do you know what this means?

`

I'm exactly sure what you mean since ggplot() doesn't have a command called abline() -- what was the exact code your tried?

I see: You'd need geom_abline() instead if you're using ggplot().

Thanks @dromano. This makes lots of sense

abline() is a base plot command you can't mix it with ggplot2 objects, the equivalent would be geom_vline()

When I have an axis that uses a date column to represent in the form of "2020-03-13" for example. If I want to use

geom_vline(xintercept = 2020-03-13, linetype="dotted", color = "blue", size=1.5) +

What would be the correct syntax

I was thinking of something like
geom_vline(xintercept = as.numeric(as.Date("XXXX-XX-XX")), linetype=4)

You could try removing the as.numeric() -- does that help?

Tried both, with removing the brackets. I know my date column dateRep is in a date format because clearly the graph is able to read it right?

 geom_vline(xintercept = as.Date("2020-03-01"), linetype="dotted", 
             color = "blue", size=1.5)

&

 geom_vline(xintercept = (as.Date("2020-03-01")), linetype="dotted", 
             color = "blue", size=1.5)

Could you run str(data$dateRep) and post the output?

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?

> str(data$dateRep)
 POSIXct[1:7904], format: "2020-03-31" "2020-03-30" "2020-03-29" "2020-03-28" "2020-03-27" ...

Sorry, I misread your code: the argument xintercept refers to an aesthetic -- could you try placing it inside aes()?

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

geom_vline(aes(xintercept=as.Date("2020-03-01")), linetype="dotted", colour="red", size=1.5)

This is one possible solution. What do you think @dromano

  geom_vline(aes(xintercept = as.integer(as.POSIXct("2020-03-20"))), col = "black")

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.

  geom_text(aes(xintercept = as.integer(as.POSIXct("2020-03-13"))), label="Hello",color="black", angle=90)

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.

Yes, have you tried?

Yes, sorry but did you mean to put another annotate tag. I seem to be confused

  annotate(geom = "vline",
           label = "Testing Intervention",
           x = as.POSIXct("2020-03-16"),
           xintercept = as.POSIXct("2020-03-16"),
           linetype="dotted",
           color = "blue",
           size=1.5)