1

我有两组时间序列数据。我想根据时间和日期绘制它们。此外,它们是异常值标题下相应时间的属性数(-1 或 1),它们应在图中用异常值 =“-1”的符号表示。此外,我的代码中的另一个问题是在 Legend 中,图例的标题和图例的内容之一出现了类似的情况。

true   pred   outlier   dtt 
0.05       0    1   9/29/2007 0:00  
-0.33      0    1   9/29/2007 1:00  
-0.41      0    1   9/29/2007 2:00  
-0.69      0    1   9/29/2007 3:00  
-0.68      0    1   9/29/2007 4:00  
-0.43   -0.53   1   9/29/2007 5:00  
-0.12   -0.23   1   9/29/2007 6:00  
0.15    0.16    1   9/29/2007 7:00  
0.56    0.39    1   9/29/2007 8:00  
0.98    0.87    1   9/29/2007 9:00  
1.18    1.27    1   9/29/2007 10:00 
2.14    1.31    1   9/29/2007 11:00 
4.12    2.87    1   9/29/2007 12:00 
5.59    5.43    1   9/29/2007 13:00 
6.90    6.52    1   9/29/2007 14:00 
6.40    7.80    1   9/29/2007 15:00 
5.88    5.78    1   9/29/2007 16:00 
6.14    5.58    1   9/29/2007 17:00 
4.61    6.20    -1  9/29/2007 18:00 
5.15    6.20    1   9/29/2007 19:00 
2.74    4.54    -1  9/29/2007 20:00 
4.66    4.54    1   9/29/2007 21:00 
5.10    4.74    1   9/29/2007 22:00 
4.79    5.53    1   9/29/2007 23:00 

应用代码:

ggplot(second_data_results_node25, aes( second_data_results_node25$dtt)) +
    geom_line(aes(y = second_data_results_node25$true, colour = "TRUE")) + 
    geom_line(aes(y = second_data_results_node25$pred, colour = "Prediction"))
4

1 回答 1

1

为了使用 绘图ggplot2,您必须相应地调整数据。由于您有两个变量,您可以将melt它们(我在reshape2这里用来融化)并按如下方式使用:groupggplot2

require(ggplot2)
require(reshape2)
df <- structure(list(true = c(0.05, -0.33, -0.41, -0.69, -0.68, -0.43, 
-0.12, 0.15, 0.56, 0.98, 1.18, 2.14, 4.12, 5.59, 6.9, 6.4, 5.88, 
6.14, 4.61, 5.15, 2.74, 4.66, 5.1, 4.79), pred = c(0, 0, 0, 0, 
0, -0.53, -0.23, 0.16, 0.39, 0.87, 1.27, 1.31, 2.87, 5.43, 6.52, 
7.8, 5.78, 5.58, 6.2, 6.2, 4.54, 4.54, 4.74, 5.53), outlier = c(1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, -1L, 1L, -1L, 1L, 1L, 1L), dtt = c("9/29/2007 0:00", "9/29/2007 1:00", 
"9/29/2007 2:00", "9/29/2007 3:00", "9/29/2007 4:00", "9/29/2007 5:00", 
"9/29/2007 6:00", "9/29/2007 7:00", "9/29/2007 8:00", "9/29/2007 9:00", 
"9/29/2007 10:00", "9/29/2007 11:00", "9/29/2007 12:00", "9/29/2007 13:00", 
"9/29/2007 14:00", "9/29/2007 15:00", "9/29/2007 16:00", "9/29/2007 17:00", 
"9/29/2007 18:00", "9/29/2007 19:00", "9/29/2007 20:00", "9/29/2007 21:00", 
"9/29/2007 22:00", "9/29/2007 23:00")), .Names = c("true", "pred", 
"outlier", "dtt"), class = "data.frame", row.names = c(NA, -24L
))

df.m <- melt(df, names(df)[3:4], names(df)[1:2])
df.m$outlier <- factor(df.m$outlier)
df.m$dtt <- strptime(as.character(df.m$dtt), format = "%m/%d/%Y %H:%M")
p <- ggplot(df.m, aes(x = dtt, y = value, group = variable, color = variable))
p <- p + geom_point(aes(shape = outlier))
p <- p + geom_line()
p <- p + theme(axis.text.x = element_text(angle = 90, hjust = 1))
p

结果_ggplot2

于 2013-01-15T10:11:16.573 回答