Jared Pace
Jared Pace

Reputation: 41

geom_vline to indicate today's date in ggplot2

Would love to display a vline that indicates today's date in ggplot2.

Here's what i'm trying. It doesn't break the visual, but it also doesn't work.

geom_vline(aes(xintercept = as.integer(dataset$Today)), col = "black") +

in my dataset (an .xls) I have a column named "Today" where I used the function =today() (which works btw) so I'm just trying to grab that & display it as a vline.

As a side note this works perfectly well:

geom_vline(aes(xintercept = as.integer(as.POSIXct("2017-07-18"))), col = "black") +

Here's the whole shebang:

library(scales) #date time scales  
library(ggplot2) # Visualization  


#Convert timestamp to POSIXct.

dataset$From<-as.POSIXct(dataset$From,format="%Y-%m-%dT%H:%M")
dataset$To<-as.POSIXct(dataset$To,format="%Y-%m-%dT%H:%M")

#ggplot stuff

ggplot(dataset,aes(x=datetime_start, y=dataset$Product, color=Stage, order = - as.numeric(Stage))) +
scale_x_datetime(breaks = date_breaks("1 month"), labels=date_format("%b%y")) +
geom_segment(aes(x=From,xend=To,yend=dataset$Product),size=15) +

#Custom V Line

geom_vline(aes(xintercept = as.integer(as.POSIXct("2017-07-18"))), col = "black") +

#Custom Legend Ordering

scale_color_brewer(palette = "Set3", breaks=c("CPR","PPR","DPR", "VPR/IPR", "GA")) +

#Title

ggtitle("Product Phase Review Schedule") + xlab("") + ylab("") + theme_bw()

Upvotes: 1

Views: 1155

Answers (1)

Jared Pace
Jared Pace

Reputation: 41

This worked for me:

geom_vline(aes(xintercept = as.numeric(as.POSIXct(Sys.Date()))), col = "black") +

as.integer worked too

Upvotes: 1

Related Questions