How to add a vertical line to a date range in a ggplot2 plot in the R programming language. More details: [ Ссылка ]
R code of this video:
set.seed(1510) # Create data frame
data <- data.frame(my_dates = seq(as.Date(0, origin="2015-01-01"),
length = 72,
by = "1 month"),
my_values = sample(1:100, 72, replace = TRUE))
install.packages("ggplot2") # Install ggplot2
library("ggplot2") # Load ggplot2
ggp <- ggplot(data, aes(my_dates, my_values)) + # Create basic ggplot
geom_line()
ggp # Draw basic ggplot
dates_vline <- as.Date(c("2015-11-01", "2018-03-01")) # Define positions of vline
dates_vline <- which(data$my_dates %in% dates_vline)
ggp + # Draw vlines to plot
geom_vline(xintercept = as.numeric(data$my_dates[dates_vline]),
col = "red", lwd = 2)
Ещё видео!