How to label some vertical lines in ggplot

Hi all,

simple question: I have two vertical lines in the following plot, a dashed line and a continuous line, which correspond to two specific dates, and I would like to label them with the corresponding dates:
image

Labels inside the plot area, next to the vertical lines, would probably look better, but it's also ok to just add the corresponding labels on the x-axis. The two dates are called day_2 and day_3 in the following reprex. Can you help me do that? Here is a reprex to reproduce the plot:

library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union
library(ggplot2)
library(lubridate)
#> 
#> Attaching package: 'lubridate'
#> The following object is masked from 'package:base':
#> 
#>     date

day_2 <- lubridate::ymd('2020-04-15')
day_3 <- lubridate::ymd('2020-05-15')
current_day <- lubridate::today()
#> Warning in system("timedatectl", intern = TRUE): running command 'timedatectl'
#> had status 1

cumulative <- structure(list(date = structure(c(18312, 18313, 18314, 18315, 
                                                18316, 18317, 18318, 18319, 18320, 18321, 18322, 18323, 18324, 
                                                18325, 18326, 18327, 18328, 18329, 18330, 18331, 18332, 18333, 
                                                18334, 18335, 18336, 18337, 18338, 18339, 18340, 18341, 18342, 
                                                18343, 18344, 18345, 18346, 18347, 18348, 18349, 18350, 18351, 
                                                18352, 18353, 18354, 18354, 18355, 18356, 18357, 18358, 18359, 
                                                18360, 18361, 18362, 18363, 18364, 18365, 18366, 18367, 18368, 
                                                18369, 18370, 18371, 18372, 18373, 18374, 18375, 18376, 18377, 
                                                18378, 18379, 18380, 18381, 18382, 18383, 18384, 18385, 18386, 
                                                18387, 18388, 18389, 18390, 18391, 18392, 18393, 18394, 18395, 
                                                18396, 18397, 18398, 18399, 18400, 18401), class = "Date"), cumulative_cases = c(3, 
                                                                                                                                 16, 79, 157, 229, 322, 400, 650, 888, 1128, 1694, 2036, 2502, 
                                                                                                                                 3089, 3858, 4636, 5883, 7375, 9172, 10149, 12462, 15113, 17660, 
                                                                                                                                 21157, 24747, 27980, 31506, 35713, 41035, 47021, 53578, 59138, 
                                                                                                                                 63927, 69176, 74386, 80539, 86498, 92472, 97689, 101739, 105792, 
                                                                                                                                 110574, 115242, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
                                                                                                                                 NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
                                                                                                                                 NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
                                                                                                                                 NA, NA, NA, NA), future = c(FALSE, FALSE, FALSE, FALSE, FALSE, 
                                                                                                                                                             FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, 
                                                                                                                                                             FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, 
                                                                                                                                                             FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, 
                                                                                                                                                             FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, 
                                                                                                                                                             FALSE, FALSE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, 
                                                                                                                                                             TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, 
                                                                                                                                                             TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, 
                                                                                                                                                             TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, 
                                                                                                                                                             TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE), sigmoid_fit = c(387.086529025157, 
                                                                                                                                                                                                                        462.924521776224, 553.559387398417, 661.851840769603, 791.204380067336, 
                                                                                                                                                                                                                        945.659058568235, 1130.01081924005, 1349.93795245062, 1612.15095207515, 
                                                                                                                                                                                                                        1924.56048633999, 2296.46424385281, 2738.75090107752, 3264.11718103818, 
                                                                                                                                                                                                                        3887.29068875663, 4625.24664188561, 5497.40049265995, 6525.75055365409, 
                                                                                                                                                                                                                        7734.93506764565, 9152.15702961967, 10806.9184130174, 12730.4951344904, 
                                                                                                                                                                                                                        14955.0782272288, 17512.5098614913, 20432.5609515071, 23740.7365066778, 
                                                                                                                                                                                                                        27455.6606148328, 31586.1854980028, 36128.4805968944, 41063.4685613782, 
                                                                                                                                                                                                                        46355.0537543839, 51949.5971809766, 57776.997188187, 63753.5281349245, 
                                                                                                                                                                                                                        69786.2966504892, 75778.8626128922, 81637.3263643896, 87276.0811006959, 
                                                                                                                                                                                                                        92622.5032741073, 97620.0800490956, 102229.780354693, 106429.777437928, 
                                                                                                                                                                                                                        110213.855251339, 113588.944580014, 113588.944580014, 116572.241841055, 
                                                                                                                                                                                                                        119188.293834093, 121466.323697047, 123437.960197737, 125135.435938444, 
                                                                                                                                                                                                                        126590.249696578, 127832.244579087, 128889.032441225, 129785.689794998, 
                                                                                                                                                                                                                        130544.65514608, 131185.767526527, 131726.397582552, 132181.633882443, 
                                                                                                                                                                                                                        132564.497061711, 132886.162610271, 133156.179511959, 133382.676755527, 
                                                                                                                                                                                                                        133572.5532153, 133731.648826496, 133864.896606426, 133976.456109635, 
                                                                                                                                                                                                                        134069.829520805, 134147.961912962, 134213.327326089, 134268.002322351, 
                                                                                                                                                                                                                        134313.728597716, 134351.966109986, 134383.93804273, 134410.668778288, 
                                                                                                                                                                                                                        134433.015910108, 134451.697190575, 134467.313188017, 134480.366316927, 
                                                                                                                                                                                                                        134491.276808533, 134500.396104323, 134508.018081827, 134514.388458963, 
                                                                                                                                                                                                                        134519.712669302, 134524.162454663, 134527.881382384, 134530.989461609, 
                                                                                                                                                                                                                        134533.587004949, 134535.757858363, 134537.572102241, 134539.088310019, 
                                                                                                                                                                                                                        134540.355436619, 134541.414397262), sigmoid_lwr = c(0, 0, 0, 
                                                                                                                                                                                                                                                                             0, 0, 0, 0, 0, 79.1465718551563, 390.005024003502, 759.992927324436, 
                                                                                                                                                                                                                                                                             1199.94186968418, 1722.49562663419, 2342.34043587197, 3076.43184442952, 
                                                                                                                                                                                                                                                                             3944.19999273448, 4967.70657040961, 6171.71585638505, 7583.62958558758, 
                                                                                                                                                                                                                                                                             9233.22200133159, 11152.0997187656, 13372.8049709707, 15927.4863769072, 
                                                                                                                                                                                                                                                                             18846.0860728639, 22154.0434625336, 25869.5982421976, 30000.8842679106, 
                                                                                                                                                                                                                                                                             34543.121937828, 39476.3041069973, 44763.7867469055, 50352.1167004911, 
                                                                                                                                                                                                                                                                             56172.2731123537, 62142.318243922, 68171.2892783979, 74164.0196616217, 
                                                                                                                                                                                                                                                                             80026.4521883945, 85670.9243055726, 91020.9127470969, 96014.8194372974, 
                                                                                                                                                                                                                                                                             100608.495912035, 104776.273547985, 108510.341328277, 111818.534312637, 
                                                                                                                                                                                                                                                                             111818.534312637, 114720.968676255, 117246.239059908, 119427.862827921, 
                                                                                                                                                                                                                                                                             121301.380818634, 122902.22740782, 124264.298651081, 125419.077978491, 
                                                                                                                                                                                                                                                                             126395.175356689, 127218.157517418, 127910.573443449, 128492.103378632, 
                                                                                                                                                                                                                                                                             128979.779362426, 129388.240750656, 129729.999951635, 130015.702337842, 
                                                                                                                                                                                                                                                                             130254.370605576, 130453.628291796, 130619.900177092, 130758.589279818, 
                                                                                                                                                                                                                                                                             130874.231373259, 130970.628660485, 131050.96458844, 131117.901895768, 
                                                                                                                                                                                                                                                                             131173.665954887, 131220.115347978, 131258.801449807, 131291.01860427, 
                                                                                                                                                                                                                                                                             131317.846293421, 131340.184517625, 131358.783439032, 131374.268190527, 
                                                                                                                                                                                                                                                                             131387.159619361, 131397.891618372, 131406.825597047, 131414.262558179, 
                                                                                                                                                                                                                                                                             131420.45317199, 131425.606176823, 131429.895382324, 131433.465506216, 
                                                                                                                                                                                                                                                                             131436.437037948, 131438.910290807, 131440.968777423, 131442.682021342, 
                                                                                                                                                                                                                                                                             131444.107898644, 131445.294588008, 131446.282194585, 131447.104102164
                                                                                                                                                                                                                        ), sigmoid_upr = c(1915.15175487228, 1991.20332779708, 2082.11754525938, 
                                                                                                                                                                                                                                           2190.77368763597, 2320.59728594165, 2475.65860304011, 2660.78660215467, 
                                                                                                                                                                                                                                           2881.69983479856, 3145.15533229514, 3459.11594867649, 3832.93556038118, 
                                                                                                                                                                                                                                           4277.55993247085, 4805.73873544216, 5432.2409416413, 6174.0614393417, 
                                                                                                                                                                                                                                           7050.60099258542, 8083.79453689856, 9298.15427890625, 10720.6844736518, 
                                                                                                                                                                                                                                           12380.6148247032, 14308.8905502151, 16537.3514834869, 19097.5333460755, 
                                                                                                                                                                                                                                           22019.0358301504, 25327.429550822, 29041.722987468, 33171.4867280951, 
                                                                                                                                                                                                                                           37713.8392559608, 42650.6330157591, 47946.3207618624, 53547.077661462, 
                                                                                                                                                                                                                                           59381.7212640204, 65364.738025927, 71401.3040225804, 77393.7055641626, 
                                                                                                                                                                                                                                           83248.2005403848, 88881.2378958192, 94224.0938011178, 99225.3406608938, 
                                                                                                                                                                                                                                           103851.064797351, 108083.28132787, 111917.369174401, 115359.354847391, 
                                                                                                                                                                                                                                           115359.354847391, 118423.515005855, 121130.348608277, 123504.784566172, 
                                                                                                                                                                                                                                           125574.53957684, 127368.644469067, 128916.200742075, 130245.411179683, 
                                                                                                                                                                                                                                           131382.889525761, 132353.222072578, 133178.736848711, 133879.431674423, 
                                                                                                                                                                                                                                           134473.015802678, 134975.027014229, 135398.994171787, 135756.622882699, 
                                                                                                                                                                                                                                           136057.988418342, 136311.725219257, 136525.206253507, 136704.708373173, 
                                                                                                                                                                                                                                           136855.561839593, 136982.283558784, 137088.69445317, 137178.021930155, 
                                                                                                                                                                                                                                           137252.988697292, 137315.889296725, 137368.655745625, 137412.913615702, 
                                                                                                                                                                                                                                           137450.029792039, 137481.15303895, 137507.248381185, 137529.126190622, 
                                                                                                                                                                                                                                           137547.466756673, 137562.841015481, 137575.728020019, 137586.529650467, 
                                                                                                                                                                                                                                           137595.582991664, 137603.170741102, 137609.529956281, 137614.85940311, 
                                                                                                                                                                                                                                           137619.325726819, 137623.06863241, 137626.205232475, 137628.833695384, 
                                                                                                                                                                                                                                           137631.036305837, 137632.882032029, 137634.428678653, 137635.724692361
                                                                                                                                                                                                                        )), class = "data.frame", row.names = c(NA, -91L))

p_sigmoid_pred <- ggplot(cumulative, aes(x = date, y = cumulative_cases, color = future)) +
  geom_point() +
  geom_line(aes(y = sigmoid_fit)) +
  geom_line(aes(y = sigmoid_lwr), linetype = "dashed") +
  geom_line(aes(y = sigmoid_upr), linetype = "dashed") +
  geom_vline(xintercept = day_2, linetype = "dashed") +
  geom_vline(xintercept = day_3) +
  labs(title=paste("predictions of sigmoid model updated to", current_day))

p_sigmoid_pred
#> Warning: Removed 48 rows containing missing values (geom_point).

I make this same mistake very frequently because is easy to forget that when you want to include annotation elements on your plot using a geom is not the best way to go since you are actually repeating the same element once for each row in your data, it is better to use the annotate() function, see this example.

library(dplyr)
library(ggplot2)
library(lubridate)

day_2 <- lubridate::ymd('2020-04-15')
day_3 <- lubridate::ymd('2020-05-15')
current_day <- lubridate::today()

cumulative <- data.frame(
              date = as.Date(c("2020-02-20","2020-02-21",
                       "2020-02-22","2020-02-23","2020-02-24","2020-02-25",
                       "2020-02-26","2020-02-27","2020-02-28","2020-02-29",
                       "2020-03-01","2020-03-02","2020-03-03","2020-03-04",
                       "2020-03-05","2020-03-06","2020-03-07","2020-03-08",
                       "2020-03-09","2020-03-10","2020-03-11","2020-03-12",
                       "2020-03-13","2020-03-14","2020-03-15","2020-03-16",
                       "2020-03-17","2020-03-18","2020-03-19","2020-03-20",
                       "2020-03-21","2020-03-22","2020-03-23","2020-03-24",
                       "2020-03-25","2020-03-26","2020-03-27","2020-03-28","2020-03-29",
                       "2020-03-30","2020-03-31","2020-04-01","2020-04-02",
                       "2020-04-02","2020-04-03","2020-04-04","2020-04-05",
                       "2020-04-06","2020-04-07","2020-04-08","2020-04-09",
                       "2020-04-10","2020-04-11","2020-04-12","2020-04-13",
                       "2020-04-14","2020-04-15","2020-04-16","2020-04-17",
                       "2020-04-18","2020-04-19","2020-04-20","2020-04-21",
                       "2020-04-22","2020-04-23","2020-04-24","2020-04-25",
                       "2020-04-26","2020-04-27","2020-04-28","2020-04-29",
                       "2020-04-30","2020-05-01","2020-05-02","2020-05-03",
                       "2020-05-04","2020-05-05","2020-05-06","2020-05-07",
                       "2020-05-08","2020-05-09","2020-05-10","2020-05-11",
                       "2020-05-12","2020-05-13","2020-05-14","2020-05-15",
                       "2020-05-16","2020-05-17","2020-05-18","2020-05-19")),
  cumulative_cases = c(3,16,79,157,229,322,400,
                       650,888,1128,1694,2036,2502,3089,3858,4636,5883,
                       7375,9172,10149,12462,15113,17660,21157,24747,
                       27980,31506,35713,41035,47021,53578,59138,63927,
                       69176,74386,80539,86498,92472,97689,101739,105792,
                       110574,115242,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,
                       NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,
                       NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,
                       NA,NA,NA,NA,NA,NA,NA,NA,NA),
            future = c(FALSE,FALSE,FALSE,FALSE,
                       FALSE,FALSE,FALSE,FALSE,FALSE,FALSE,FALSE,FALSE,
                       FALSE,FALSE,FALSE,FALSE,FALSE,FALSE,FALSE,FALSE,FALSE,
                       FALSE,FALSE,FALSE,FALSE,FALSE,FALSE,FALSE,FALSE,
                       FALSE,FALSE,FALSE,FALSE,FALSE,FALSE,FALSE,FALSE,
                       FALSE,FALSE,FALSE,FALSE,FALSE,FALSE,TRUE,TRUE,
                       TRUE,TRUE,TRUE,TRUE,TRUE,TRUE,TRUE,TRUE,TRUE,TRUE,
                       TRUE,TRUE,TRUE,TRUE,TRUE,TRUE,TRUE,TRUE,TRUE,
                       TRUE,TRUE,TRUE,TRUE,TRUE,TRUE,TRUE,TRUE,TRUE,TRUE,
                       TRUE,TRUE,TRUE,TRUE,TRUE,TRUE,TRUE,TRUE,TRUE,
                       TRUE,TRUE,TRUE,TRUE,TRUE,TRUE,TRUE,TRUE),
       sigmoid_fit = c(387.086529025157,
                       462.924521776224,553.559387398417,661.851840769603,791.204380067336,
                       945.659058568235,1130.01081924005,1349.93795245062,
                       1612.15095207515,1924.56048633999,2296.46424385281,
                       2738.75090107752,3264.11718103818,3887.29068875663,
                       4625.24664188561,5497.40049265995,6525.75055365409,
                       7734.93506764565,9152.15702961967,10806.9184130174,
                       12730.4951344904,14955.0782272288,17512.5098614913,20432.5609515071,
                       23740.7365066778,27455.6606148328,31586.1854980028,
                       36128.4805968944,41063.4685613782,46355.0537543839,
                       51949.5971809766,57776.997188187,63753.5281349245,
                       69786.2966504892,75778.8626128922,81637.3263643896,
                       87276.0811006959,92622.5032741073,97620.0800490956,
                       102229.780354693,106429.777437928,110213.855251339,113588.944580014,
                       113588.944580014,116572.241841055,119188.293834093,
                       121466.323697047,123437.960197737,125135.435938444,
                       126590.249696578,127832.244579087,128889.032441225,
                       129785.689794998,130544.65514608,131185.767526527,
                       131726.397582552,132181.633882443,132564.497061711,
                       132886.162610271,133156.179511959,133382.676755527,133572.5532153,
                       133731.648826496,133864.896606426,133976.456109635,
                       134069.829520805,134147.961912962,134213.327326089,
                       134268.002322351,134313.728597716,134351.966109986,
                       134383.93804273,134410.668778288,134433.015910108,
                       134451.697190575,134467.313188017,134480.366316927,134491.276808533,
                       134500.396104323,134508.018081827,134514.388458963,
                       134519.712669302,134524.162454663,134527.881382384,
                       134530.989461609,134533.587004949,134535.757858363,
                       134537.572102241,134539.088310019,134540.355436619,
                       134541.414397262),
       sigmoid_lwr = c(0,0,0,0,0,0,0,0,
                       79.1465718551563,390.005024003502,759.992927324436,
                       1199.94186968418,1722.49562663419,2342.34043587197,
                       3076.43184442952,3944.19999273448,4967.70657040961,6171.71585638505,
                       7583.62958558758,9233.22200133159,11152.0997187656,
                       13372.8049709707,15927.4863769072,18846.0860728639,
                       22154.0434625336,25869.5982421976,30000.8842679106,
                       34543.121937828,39476.3041069973,44763.7867469055,
                       50352.1167004911,56172.2731123537,62142.318243922,
                       68171.2892783979,74164.0196616217,80026.4521883945,85670.9243055726,
                       91020.9127470969,96014.8194372974,100608.495912035,
                       104776.273547985,108510.341328277,111818.534312637,
                       111818.534312637,114720.968676255,117246.239059908,
                       119427.862827921,121301.380818634,122902.22740782,
                       124264.298651081,125419.077978491,126395.175356689,127218.157517418,
                       127910.573443449,128492.103378632,128979.779362426,
                       129388.240750656,129729.999951635,130015.702337842,
                       130254.370605576,130453.628291796,130619.900177092,
                       130758.589279818,130874.231373259,130970.628660485,
                       131050.96458844,131117.901895768,131173.665954887,
                       131220.115347978,131258.801449807,131291.01860427,131317.846293421,
                       131340.184517625,131358.783439032,131374.268190527,
                       131387.159619361,131397.891618372,131406.825597047,
                       131414.262558179,131420.45317199,131425.606176823,
                       131429.895382324,131433.465506216,131436.437037948,
                       131438.910290807,131440.968777423,131442.682021342,131444.107898644,
                       131445.294588008,131446.282194585,131447.104102164),
       sigmoid_upr = c(1915.15175487228,
                       1991.20332779708,2082.11754525938,2190.77368763597,2320.59728594165,
                       2475.65860304011,2660.78660215467,2881.69983479856,
                       3145.15533229514,3459.11594867649,3832.93556038118,
                       4277.55993247085,4805.73873544216,5432.2409416413,
                       6174.0614393417,7050.60099258542,8083.79453689856,
                       9298.15427890625,10720.6844736518,12380.6148247032,
                       14308.8905502151,16537.3514834869,19097.5333460755,22019.0358301504,
                       25327.429550822,29041.722987468,33171.4867280951,
                       37713.8392559608,42650.6330157591,47946.3207618624,
                       53547.077661462,59381.7212640204,65364.738025927,
                       71401.3040225804,77393.7055641626,83248.2005403848,88881.2378958192,
                       94224.0938011178,99225.3406608938,103851.064797351,
                       108083.28132787,111917.369174401,115359.354847391,
                       115359.354847391,118423.515005855,121130.348608277,
                       123504.784566172,125574.53957684,127368.644469067,
                       128916.200742075,130245.411179683,131382.889525761,
                       132353.222072578,133178.736848711,133879.431674423,134473.015802678,
                       134975.027014229,135398.994171787,135756.622882699,
                       136057.988418342,136311.725219257,136525.206253507,
                       136704.708373173,136855.561839593,136982.283558784,
                       137088.69445317,137178.021930155,137252.988697292,
                       137315.889296725,137368.655745625,137412.913615702,137450.029792039,
                       137481.15303895,137507.248381185,137529.126190622,
                       137547.466756673,137562.841015481,137575.728020019,
                       137586.529650467,137595.582991664,137603.170741102,
                       137609.529956281,137614.85940311,137619.325726819,
                       137623.06863241,137626.205232475,137628.833695384,137631.036305837,
                       137632.882032029,137634.428678653,137635.724692361)
)

ggplot(cumulative, aes(x = date, y = cumulative_cases, color = future)) +
    geom_point() +
    geom_line(aes(y = sigmoid_fit)) +
    geom_line(aes(y = sigmoid_lwr), linetype = "dashed") +
    geom_line(aes(y = sigmoid_upr), linetype = "dashed") +
    annotate(geom = "vline",
             x = c(day_2, day_3),
             xintercept = c(day_2, day_3),
             linetype = c("dashed", "solid")) +
    annotate(geom = "text",
             label = c(as.character(day_2), as.character(day_2)),
             x = c(day_2, day_3),
             y = c(6e4, 6e4),
             angle = 90, 
             vjust = 1) +
    labs(title=paste("predictions of sigmoid model updated to", current_day))

Created on 2020-04-02 by the reprex package (v0.3.0.9001)

2 Likes

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.