简体   繁体   English

用ggplot2绘制日期时遇到问题

[英]Trouble plotting dates with ggplot2

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. 我正在尝试针对R中的日期进行绘图。尝试针对已存在的绘图创建垂直线时遇到了麻烦。 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). 我尝试的所有不同格式都会导致绘图上未显示任何内容,或者在1970年(默认日期)显示一行。 The year-data is in the form yyyy-mm-dd . 年数据的格式为yyyy-mm-dd For example, "1914-07-01" . 例如, "1914-07-01"

I've also tried inputting these dates in a data.frame , but got the same problem. 我也尝试过在data.frame输入这些日期,但是遇到了同样的问题。

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)) : 编辑:这是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)))

You were very close, the problem is your data is in POSIXct, and you were trying to convert to Date. 您非常接近,问题在于您的数据位于POSIXct中,并且您试图将其转换为Date。 To fix it, change to POSIXct: 要解决此问题,请更改为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) 解释为什么intecept如此接近1970(两者均为0)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM