2017-08-30 118 views
0

我正在嘗試使用「行」類型繪製時間序列數據。但它沒有正確出現。我有十三個變量繪製在具有不同y軸範圍的單個圖表中,並在x軸中繪製爲datetimeR圖中的多個Y軸

> head(eg) 
       datetime  x1  x2  x3  x4  x5  x6 
24 2017-06-14 12:00:00 8446.755 7648.031 1111.6707 0.9045327 414.2691 11.778449 
25 2017-06-14 13:00:00 6295.053 5660.273 882.6834 0.7028812 415.9553 8.754347 
26 2017-06-14 14:00:00 6086.147 5498.443 833.2998 0.6438458 416.5267 8.459638 
27 2017-06-14 15:00:00 1850.083 1644.708 254.0441 0.2337391 416.7503 2.569269 
28 2017-06-14 16:00:00 5267.201 4773.258 768.3997 0.5249667 416.4483 7.320368 
29 2017-06-14 17:00:00 5907.030 5302.488 752.4784 0.6931478 409.5094 8.348385 
     x7  x8  x9  x10  x11  x12  x13 
24 50.00656 57990800 51.76610 1169.014 0.4784138 0.5478276 1.193828 
25 50.01681 57994800 51.92203 1199.004 0.4809830 0.6833898 1.174119 
26 49.99918 57999183 53.23167 1194.763 0.4223333 0.6339667 1.166983 
27 50.01026 58003118 53.97018 1068.251 0.3476316 0.2754211 1.260561 
28 50.00910 58007020 49.94800 1188.888 0.4477200 0.5084000 1.171740 
29 50.15805 58011111 46.86375 1144.759 0.3844375 0.5530125 1.131637 

當我暗算x軸單變量,它是未來像這樣

plot(eg$x1, x = eg$datetime, type = "l") 

pic1

可我知道爲什麼它沒有顯示線曲線?

謝謝。

回答

0

什麼是列的類型datetime?我會認爲這是一個因素或字符串。 R不會繪製具有分類x軸的線條。您可以使用lubridatedatetime值轉換成能繪出更漂亮:

df <- data.frame(datetime= c("2017-06-14 12:00:00", "2017-06-14 13:00:00", 
"2017-06-14 14:00:00", "2017-06-14 15:00:00", "2017-06-14 16:00:00", 
"2017-06-14 17:00:00"), x1 = c(8446.755, 6295.053, 6086.147, 1850.083, 
5267.201, 5907.030)) 

library(lubridate) 

plot (df$datetime, df$x1, type="l") # does not work 
df$date <- ymd_hms(df$datetime) 
plot (df$date, df$x1, type="l") # works 
+0

謝謝你。我沒有檢查類類型的變量。 –

+0

如果您只想使用'base-R',您可以使用'as.POSIXct'將'datetime'格式化爲時間格式,然後'plot'圖形 – smoff