2

I'm trying to plot against dates in R. I've run into trouble trying to create vertical lines against a plot that I already have. All of the different formats that I try either result in nothing showing up on the plot, or a line at 1970 (the default date). The year-data is in the form yyyy-mm-dd. For example, "1914-07-01".

I've also tried inputting these dates in a data.frame, but got the same problem.

I've been trying to make a reproducible example, but I haven't seen any example datasets to do so with, and got frustrated trying to create one... sorry about that. Here's the relevant code:

ggplot(M,aes(x=date,color=origin,y=value)) + 
  geom_point() +
  geom_line() +
  facet_grid(topic~origin) +
  geom_vline(xintercept=as.numeric(as.Date("1914-07-01")))

Everything plots correctly without the addition of the final line.

Edit: here's the result of dput(head(M)):

structure(list(topic = structure(c(1L, 1L, 1L, 1L, 1L, 1L), .Label = c("1", 
"2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", 
"14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", 
"25"), class = "factor"), date = structure(c(-1767196800, -1765987200, 
-1764518400, -1763308800, -1762099200, -1760889600), class = c("POSIXct", 
"POSIXt"), tzone = ""), origin = structure(c(2L, 2L, 2L, 2L, 
2L, 2L), .Label = c("Blast", "The_Egoist"), class = "factor"), 
    value = c(6.69960398194253e-07, 7.48757156068349e-07, 7.04834977806836e-07, 
    7.10226526475778e-07, 6.8295233938925e-07, 6.16466066169137e-07
    )), .Names = c("topic", "date", "origin", "value"), class = c("grouped_df", 
"tbl_df", "tbl", "data.frame"), row.names = c(NA, -6L), vars = list(
    topic, date), drop = TRUE, indices = list(0L, 1L, 2L, 3L, 
    4L, 5L), group_sizes = c(1L, 1L, 1L, 1L, 1L, 1L), biggest_group_size = 1L, labels = structure(list(
    topic = structure(c(1L, 1L, 1L, 1L, 1L, 1L), .Label = c("1", 
    "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", 
    "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", 
    "23", "24", "25"), class = "factor"), date = structure(c(-1767196800, 
    -1765987200, -1764518400, -1763308800, -1762099200, -1760889600
    ), class = c("POSIXct", "POSIXt"), tzone = "")), class = "data.frame", row.names = c(NA, 
-6L), .Names = c("topic", "date"), vars = list(topic, date)))
2
  • 1
    for reproducible data, why not give the first few lines of your data: dput(head(M)) Commented Aug 6, 2015 at 1:10
  • 2
    I didn't think of that! Thanks, I edited it in. Commented Aug 6, 2015 at 1:13

1 Answer 1

3

You were very close, the problem is your data is in POSIXct, and you were trying to convert to Date. To fix it, change to POSIXct:

ggplot(M,aes(x=date,color=origin,y=value)) + 
  geom_point() +
  geom_line() +
  facet_grid(topic~origin) +
  geom_vline(xintercept=as.numeric(as.POSIXct("1914-07-01")))

You can see the difference in the calls:

as.numeric(as.POSIXct("1914-07-01"))
[1] -1751569200

as.numeric(as.Date("1914-07-01"))
[1] -20273

Explaining why the intecept was so close to 1970 (the 0 for both)

Sign up to request clarification or add additional context in comments.

4 Comments

This worked! Thank you! If I may, what makes this data POSIXct rather than a date? I was under the impression that in order for it to be POSIXct, there had to be a time attached.
They are both ways of holding dates - POSIX time is seconds since 01-01-1970, date is in days since 01-01-1970. Your original data was in POSIX, so that's what ggplot used. When you gave it an as.numeric(date), it thought you meant seconds before 1970, not days, leading to the vline being near to 1970 instead of where you wanted it.
Ah, that makes sense. But to clarify - I was more wondering what made the date POSIX rather than a date. Was it the format, or is that just the default? Or something else?
impossible for me to tell, str(M$date) gives it as POSIXct - your source for the data must have determined it. I think R normally treats anything with seconds as POSIXct but I could be wrong.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.