Cauder
Cauder

Reputation: 2597

How to add a vertical line in ggplot

I'd like to add a vertical line to ggplot. My x axis is a character value and I'm having trouble getting it to work.

Here is my data

my_data <- read.table(text="day value
11/15/19    0.23633
11/16/19    0.28485
11/17/19    0.63127
11/18/19    0.15434
11/19/19    0.47964
11/20/19    0.65967
11/21/19    0.48741
11/22/19    0.84541
11/23/19    0.10123
11/24/19    0.78169
11/25/19    0.23189
11/26/19    0.86665
11/27/19    0.55184
11/28/19    0.81410
11/29/19    0.25821
11/30/19    0.23576
12/1/19 0.46397
12/2/19 0.55764
12/3/19 0.95645
12/4/19 0.63954
12/5/19 0.76766
12/7/19 0.74505
12/8/19 0.65515
12/9/19 0.58222
12/10/19    0.17294", header=TRUE, stringsAsFactors=FALSE)

Here is my code

my_data %>% 
  ggplot(aes(day, value)) +
  geom_line() +
  geom_vline(xintercept=5)

My goal is to have a vertical line that intercepts x on the value 11/20/19. When I run this code, I don't see a horizontal line anywhere on my chart.

Upvotes: 1

Views: 914

Answers (1)

d.b
d.b

Reputation: 32548

library(ggplot2)
library(lubridate)

my_data$day = mdy(my_data$day)
ggplot(my_data, aes(day, value)) +
    geom_line() +
    geom_vline(xintercept = mdy("11/20/19"), col = "red")

Upvotes: 5

Related Questions