3

Below is the data.table I am working with. I want to plot verticle lines whenever longSignal column is 1.

data.frame(
        index = c("2011-09-09 17:00:00", 
                    "2011-09-12 17:00:00",
                    "2011-09-13 17:00:00", "2011-09-14 17:00:00",
                    "2011-09-15 17:00:00", "2011-09-16 17:00:00", "2011-09-19 17:00:00",
                    "2011-09-20 17:00:00", "2011-09-21 17:00:00",
                    "2011-09-22 17:00:00", "2011-09-23 17:00:00", "2011-09-26 17:00:00",
                    "2011-09-27 17:00:00", "2011-09-28 17:00:00", "2011-09-29 17:00:00",
                    "2011-09-30 17:00:00", "2011-10-03 17:00:00",
                    "2011-10-04 17:00:00", "2011-10-05 17:00:00", "2011-10-06 17:00:00",
                    "2011-10-07 17:00:00"),
   EURUSD.Close = c(1.36534, 1.367895, 1.36783, 1.37546, 1.38764, 1.38005,
                    1.36849, 1.37009, 1.35722, 1.346385, 1.35002, 1.353255,
                    1.35825, 1.35425, 1.359705, 1.33876, 1.31759, 1.33489, 1.33482,
                    1.34374, 1.33771),
     longSignal = c(0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0,
                    0)
)

Here is the code that I was trying to work with in ggplot

ggplot(RSI_data, aes(index, EURUSD.Close)) +
  geom_line() + 
  geom_vline(aes(xintercept = as.numeric(RSI_data$index[which(RSI_data$longSignal == 1)]), 
             size = 2, colour = "red"))

I have been getting error. Could anyone tell me how can I do this right? I Thanks in advance!

Z.Lin
  • 28,055
  • 6
  • 54
  • 94
boniface316
  • 489
  • 3
  • 17
  • 3
    Could you make your problem reproducible by sharing a sample of your data so others can help (please do not use `str()`, `head()` or screenshot)? You can use the [`reprex`](https://reprex.tidyverse.org/articles/articles/magic-reprex.html) and [`datapasta`](https://cran.r-project.org/web/packages/datapasta/vignettes/how-to-datapasta.html) packages to assist you with that. See also [Help me Help you](https://speakerdeck.com/jennybc/reprex-help-me-help-you?slide=5) & [How to make a great R reproducible example?](https://stackoverflow.com/q/5963269) – Tung Dec 04 '18 at 02:12
  • 2
    I'd say "Welcome to SO" but you've got 353 points so should kind of already know pictures are neither code nor data unless the topic is image processing. Time to re-visit the guidance provided at https://stackoverflow.com/questions/tagged/r – hrbrmstr Dec 04 '18 at 02:14
  • Sorry folks, this is my first time adding a data in the question. – boniface316 Dec 04 '18 at 02:26

2 Answers2

4

You can try this:

# convert index to date-time format; this makes x-axis continuous rather than
# categorical, so you don't have to specify the group for geom_line.
RSI_data$index <- as.POSIXct(as.character(RSI_data$index))

ggplot(RSI_data,
       aes(x = index, y = EURUSD.Close)) +
  geom_line() +
  geom_vline(data = subset(RSI_data, longSignal == 1), # filter data source
             aes(xintercept = index),
             size = 2, colour = "red")

plot

Z.Lin
  • 28,055
  • 6
  • 54
  • 94
0

If you put the xintercept term outside the aes() then it works. Also, you may get another error saying geom_path: Each group consists of only one observation. Do you need to adjust the group aesthetic? so the group=1 is there to stop that error

 ggplot(RSI_data, aes(index,EURUSD.Close)) +
      geom_line(group=1) +
      geom_vline(xintercept = as.numeric(RSI_data$index[which(RSI_data$longSignal == 1)]), 
                 size = 2, colour = "red")
morgan121
  • 2,213
  • 1
  • 15
  • 33