One way to generate vertical red lines on a ggplot2 time series plot at each date indicated in the "date" column of another dataframe is by using the geom_vline
function.
Here is some sample code that demonstrates this approach:
main.r507 chars16 lines
In this example, we first create a sample time series dataset df
with two columns: "date" and "value". We then create a separate dataframe red_dates
with a single column "date" indicating the dates for which we want to add the red lines to the plot.
To add the red lines to the plot, we use geom_vline
and specify the data
argument as red_dates
to indicate that we want to use the dates in this dataframe for the vertical lines. We also use aes(xintercept = date)
to specify that the date
column in red_dates
should be used to determine the location of the vertical lines. Finally, we set the color
argument to "red" to indicate that we want the lines to be red.
This code will generate a plot of the time series with vertical red lines at each date indicated in the red_dates
dataframe.
gistlibby LogSnag