Search code examples
rdateplot

Add vertical line to plot in R


I would like to add vertical lines to the plot in the beginning of years 2020 and 2022. I think the problem might be related with the Date format

My data looks like this:

dput(plot_data2)
structure(list(Date = structure(c(1640995200, 1648771200, 1656633600, 
1664582400, 1672531200, 1680307200, 1688169600), tzone = "UTC", class = c("POSIXct", 
"POSIXt")), Index = c(1.159, 1.19, 1.2, 1.203, 1.203, 1.212, 
1.218), Compensation = c(0.667, 0.674, 0.682, 0.685, 0.694, 0.702, 
0.704), Depreciation = c(0.174, 0.178, 0.181, 0.184, 0.186, 0.189, 
0.189), Taxes = c(0.107, 0.11, 0.109, 0.109, 0.11, 0.109, 0.109
), Interest = c(0.027, 0.026, 0.025, 0.023, 0.022, 0.019, 0.015
), Corpotaxes = c(0.038, 0.038, 0.036, 0.037, 0.038, 0.038, 0.038
), Profits = c(0.147, 0.165, 0.167, 0.164, 0.153, 0.156, 0.162
), Profitshare = c(0.125970664365833, 0.13781512605042, 0.139166666666667, 
0.13715710723192, 0.127182044887781, 0.127887788778878, 0.133825944170772
), InterestTaxesCounter = c(0.13, 0.143, 0.142, 0.142, 0.143, 
0.142, 0.142), Counterfactual = c(0.129421915444349, 0.131932773109244, 
0.1325, 0.12884455527847, 0.118038237738986, 0.116336633663366, 
0.119047619047619)), row.names = c(NA, -7L), class = c("tbl_df", 
"tbl", "data.frame"))

This is what I have tried so far:

plot_data2 <- 
  subset(indexprofitshare, Date >= as.Date("2018-01-01"))

plot(plot_data2$Date, plot_data2$Profitshare,
     type = "l", yaxt = "n", col = "blue",
     xlab = "Date", ylab = "Value", ylim = c(0.05, 0.15)
  )

lines(
  indexprofitshare$Date[indexprofitshare$Date >= as.Date("2020-01-01")],
  indexprofitshare$Counterfactual[indexprofitshare$Date >= as.Date("2020-01-01")],
  type = "l", col = "red", lty = 2
  )

legend("bottomright",
       legend = c("Profitshare", "Counterfactual"),
       col = c("blue", "red"), lty = c(1, 2)
  )

axis(side = 2, at = pretty(c(0.06,0.14)), 
     labels = paste0(pretty(c(0.06,0.14) * 100), '%'),
     las = 1
  )

abline(v = c(as.Date("2022-01-01"), as.Date("2022-01-01")), 
       col = c("blue", "red"), lty = c(1,2), lwd = c(1, 3))

Solution

  • The issue is that you Date column is a date time not a date. Hence, convert it to a date or use date times as "x" values for the vertical lines, i.e. use as.POSIXct instead of as.Date:

    plot(plot_data2$Date, plot_data2$Profitshare,
      type = "l", yaxt = "n",
      col = "blue", xlab = "Date", 
      ylab = "Value", ylim = c(0.05, 0.15)
    )
    # lines(
    #   indexprofitshare$Date[indexprofitshare$Date >= as.Date("2020-01-01")],
    #   indexprofitshare$Counterfactual[indexprofitshare$Date >= as.Date("2020-01-01")],
    #   type = "l", col = "red", lty = 2
    # )
    legend("bottomright",
      legend = c("Profitshare", "Counterfactual"),
      col = c("blue", "red"), lty = c(1, 2)
    )
    axis(
      side = 2, at = pretty(c(0.06, 0.14)),
      labels = paste0(pretty(c(0.06, 0.14) * 100), "%"), las = 1
    )
    abline(
      v = c(as.POSIXct("2022-01-01"), as.POSIXct("2023-01-31")),
      col = c("blue", "red"), 
      lty = c(1, 2), 
      lwd = c(1, 3)
    )