Home > Net >  R: How to add vertical line (geom_vline) on plotly (ts_plot)?
R: How to add vertical line (geom_vline) on plotly (ts_plot)?

Time:10-13

I have weekly time series for 2 variables. Now I need to add 2 vertical lines (geom_vline) at "2019-11-10" and "2020-04-26 date points.

structure(list(week = structure(c(18119, 18126, 18133, 18140, 
18147, 18154, 18161, 18168, 18175, 18182, 18189, 18196, 18203, 
18210, 18217, 18224, 18231, 18238, 18245, 18252, 18259, 18266, 
18273, 18280, 18287, 18294, 18301, 18308, 18315, 18322, 18329, 
18336, 18343, 18350, 18357, 18364, 18371, 18378, 18385, 18392, 
18399, 18406, 18413, 18420, 18427, 18434, 18441, 18448, 18455, 
18462, 18469, 18476, 18483, 18490, 18497, 18504, 18511, 18518, 
18525, 18532, 18539, 18546, 18553, 18560, 18567, 18574, 18581, 
18588, 18595, 18602, 18609, 18616, 18623, 18630, 18637, 18644, 
18651, 18658, 18665, 18672, 18679, 18686, 18693, 18700, 18707, 
18714, 18721, 18728, 18735, 18742, 18749, 18756, 18763, 18770, 
18777, 18784, 18791, 18798, 18805, 18812, 18819, 18826, 18833, 
18840, 18847, 18854, 18861, 18868, 18875, 18882, 18889, 18896, 
18903, 18910, 18917, 18924, 18931, 18938, 18945, 18952, 18959, 
18966, 18973, 18980, 18987, 18994, 19001, 19008, 19015, 19022, 
19029, 19036, 19043, 19050, 19057, 19064, 19071, 19078, 19085, 
19092, 19099, 19106, 19113, 19120, 19127, 19134, 19141, 19148, 
19155, 19162, 19169, 19176, 19183, 19190, 19197, 19204, 19211, 
19218, 19225, 19232, 19239, 19246, 19253, 19260, 19267), class = "Date"), 
    X = c(6L, 104L, 123L, 82L, 67L, 108L, 89L, 1153L, 311L, 346L, 
    220L, 219L, 184L, 257L, 585L, 342L, 197L, 184L, 351L, 278L, 
    120L, 204L, 206L, 146L, 216L, 186L, 171L, 200L, 198L, 170L, 
    192L, 139L, 136L, 414L, 256L, 306L, 281L, 221L, 271L, 311L, 
    315L, 277L, 342L, 394L, 493L, 855L, 1617L, 954L, 1143L, 1031L, 
    692L, 380L, 378L, 493L, 381L, 706L, 546L, 653L, 447L, 1004L, 
    499L, 442L, 926L, 564L, 568L, 755L, 581L, 572L, 780L, 601L, 
    739L, 563L, 300L, 454L, 727L, 733L, 673L, 648L, 614L, 754L, 
    827L, 719L, 874L, 692L, 752L, 536L, 658L, 817L, 913L, 813L, 
    844L, 811L, 977L, 877L, 818L, 673L, 1419L, 809L, 818L, 709L, 
    577L, 802L, 508L, 536L, 663L, 782L, 634L, 665L, 583L, 685L, 
    908L, 1013L, 903L, 965L, 981L, 1030L, 1205L, 1197L, 956L, 
    936L, 901L, 707L, 565L, 384L, 341L, 529L, 510L, 597L, 610L, 
    587L, 715L, 777L, 638L, 619L, 617L, 677L, 1118L, 1173L, 1025L, 
    655L, 1006L, 1129L, 811L, 773L, 796L, 993L, 891L, 900L, 1072L, 
    1182L, 947L, 743L, 759L, 616L, 584L, 613L, 597L, 734L, 671L, 
    1119L, 906L, 825L, 1109L, 1085L, 913L), Y = c(0L, 23L, 50L, 
    35L, 38L, 38L, 43L, 173L, 128L, 134L, 115L, 103L, 104L, 122L, 
    168L, 186L, 158L, 136L, 122L, 91L, 87L, 73L, 105L, 97L, 96L, 
    84L, 121L, 108L, 83L, 106L, 108L, 107L, 96L, 151L, 128L, 
    135L, 148L, 133L, 114L, 150L, 175L, 172L, 182L, 185L, 181L, 
    224L, 451L, 369L, 399L, 377L, 325L, 259L, 245L, 268L, 284L, 
    338L, 375L, 383L, 307L, 318L, 341L, 290L, 401L, 333L, 336L, 
    404L, 406L, 333L, 372L, 372L, 388L, 236L, 162L, 264L, 378L, 
    390L, 350L, 327L, 341L, 359L, 462L, 413L, 421L, 400L, 424L, 
    283L, 377L, 459L, 478L, 482L, 411L, 416L, 522L, 557L, 502L, 
    479L, 631L, 533L, 514L, 442L, 477L, 432L, 378L, 365L, 443L, 
    407L, 384L, 451L, 381L, 435L, 603L, 612L, 685L, 567L, 603L, 
    576L, 564L, 643L, 671L, 722L, 695L, 463L, 328L, 228L, 160L, 
    235L, 282L, 346L, 332L, 340L, 396L, 433L, 354L, 356L, 355L, 
    367L, 462L, 445L, 454L, 355L, 566L, 628L, 491L, 517L, 429L, 
    569L, 595L, 520L, 601L, 646L, 630L, 590L, 513L, 465L, 390L, 
    356L, 356L, 417L, 420L, 443L, 450L, 482L, 540L, 611L, 572L
    )), row.names = c(NA, -165L), class = c("tbl_df", "tbl", 
"data.frame"))

Firstly I convert it into xts object and next use ts_plot() for visualization

#Create xts object
df.xts <- xts(df[, 2:3], order.by = df.w$week)

#plot
library(TSstudio)
ts_plot(df.xts,
        slider = TRUE)

Plot

I tried the following codes, but it give me the "NULL" in the output

  geom_vline(xintercept = as.Date("2020-03-01"))

or

  geom_vline(xintercept = as.POSIXct("2020-03-01"))

CodePudding user response:

Edit

You could use the add_segments function from plotly. You could also add multiple lines by specifying vectors like this:

library(xts)
library(TSstudio)
library(dplyr)
library(plotly)
#Create xts object
df.xts <- xts(df[, 2:3], order.by = df$week)
# plot
ts_plot(df.xts) %>%
  add_segments(y = c(0, 0, 0), 
               x = as.Date(c("2020-03-01", "2020-06-01", "2020-08-01")), 
               yend = c(1500, 1500, 1500), 
               xend = as.Date(c("2020-03-01", "2020-06-01", "2020-08-01")), 
               color = c('red', 'green', 'purple'),
               showlegend = FALSE)

Created on 2022-10-12 with enter image description here

Created on 2022-10-11 with reprex v2.0.2

  • Related