Geom_vline时间的值

时间:2017-08-15 11:16:26

标签: r

我尝试添加指定X限制值的垂直线。我的数据:

pair

meltk1=structure(list(variable = structure(c(1L, 1L, 1L, 1L, 1L, 1L), .Label = "number", class = "factor"), value = c(0L, 85L, 65L, 35L, 28L, 124L), Hour = c("00:23:00", "00:29:00", "00:53:00", "04:51:00", "05:08:00", "05:23:00" )), .Names = c("variable", "value", "Hour"), row.names = c(168L, 169L, 170L, 1L, 2L, 3L), class = "data.frame") 是指定测量变量的时间,没有日期。 Hour问题,正如我在几篇文章中看到的那样,它需要一个日期。因为,我没有必要提供日期信息。如何在没有日期的情况下使用geom_vline?

按照我尝试使用的代码,但不适用于简单日期。 geom_vline还有其他选择吗?当as.POSIXct具有此格式Hour2017-08-15 07:32:00 CEST时,此代码有效。但我只想POSIXct("2017-08-15 00:53:00")作为时间。

Hour

1 个答案:

答案 0 :(得分:2)

您的代码将“小时”视为离散变量,而不是时间不是您只选择一个任意日期并使用日期?:

library(ggplot2)
meltk1=structure(list(variable = structure(c(1L, 1L, 1L, 1L, 1L, 1L), 
.Label = "number", class = "factor"), 
        value = c(0L, 85L, 65L, 35L, 28L, 124L), Hour = c("00:23:00", 
        "00:29:00", "00:53:00", "04:51:00", "05:08:00", "05:23:00"
        )), .Names = c("variable", "value", "Hour"), row.names = c(168L, 
                                                               169L, 
170L, 1L, 2L, 3L), class = "data.frame")

meltk1$Hour <- as.POSIXct(paste("2017-01-01", meltk1$Hour, "CEST"))


ggplot(meltk1, aes(x=Hour, y = value, group = variable, colour = 
variable)) +
    geom_line(size=1) + 
    geom_vline(xintercept = as.numeric(as.POSIXct("2017-01-01 00:53:00 CEST")), color = "black", linetype = "dashed")

或者如果你必须有一个时间不按比例绘制的情节:

library(ggplot2)
meltk1=structure(list(variable = structure(c(1L, 1L, 1L, 1L, 1L, 1L), .Label = "number", class = "factor"), 
        value = c(0L, 85L, 65L, 35L, 28L, 124L), Hour = c("00:23:00", 
        "00:29:00", "00:53:00", "04:51:00", "05:08:00", "05:23:00"
        )), .Names = c("variable", "value", "Hour"), row.names = c(168L, 
                                                                   169L, 170L, 1L, 2L, 3L), class = "data.frame")


ggplot(meltk1, aes(x=Hour, y = value, group = variable, colour = variable)) +
    geom_line(size=1) + 
    geom_vline(xintercept = 3, color = "black", linetype = "dashed")