2017-08-15 24 views
0

我想添加指定X限制值的垂直線。我的數據:Geom_vline時間值

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替代品?此代碼在Hour具有此格式2017-08-15 07:32:00 CESTPOSIXct("2017-08-15 00:53:00")時有效。但我只想要Hour作爲時間。

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

回答

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") 
+0

第一種解決方案很適合,非常感謝。 – ranell