簡體   English   中英

如何按時間序列繪制數據

[英]how to plot data in time series

我有看起來像這樣的數據:

  time sucrose fructose glucose galactose molasses water
1    5     0.0     0.00     0.0       0.0      0.3     0
2   10     0.3     0.10     0.1       0.0      1.0     0
3   15     0.8     0.20     0.2       0.2      1.4     0
4   20     1.3     0.35     0.7       0.4      2.5     0
5   25     2.2     0.80     1.6       0.5      3.5     0
6   30     3.1     1.00     2.3       0.6      4.5     0
7   35     3.6     1.60     3.1       0.7      5.7     0
8   40     5.1     2.80     4.3       0.7      6.7     0

如何制作使用時間列的時間序列圖? 它們都在增加價值。

我看到了這篇文章在一個情節中的多個時間序列,它使用ts.plot來實現類似於我想要顯示的內容,這是:

時間序列圖

上表的輸入數據:

structure(list(time = c(5, 10, 15, 20, 25, 30, 35, 40), sucrose = c(0, 
0.3, 0.8, 1.3, 2.2, 3.1, 3.6, 5.1), fructose = c(0, 0.1, 0.2, 
0.35, 0.8, 1, 1.6, 2.8), glucose = c(0, 0.1, 0.2, 0.7, 1.6, 2.3, 
3.1, 4.3), galactose = c(0, 0, 0.2, 0.4, 0.5, 0.6, 0.7, 0.7), 
    molasses = c(0.3, 1, 1.4, 2.5, 3.5, 4.5, 5.7, 6.7), water = c(0, 
    0, 0, 0, 0, 0, 0, 0)), .Names = c("time", "sucrose", "fructose", 
"glucose", "galactose", "molasses", "water"), row.names = c(NA, 
-8L), class = "data.frame")

似乎不需要ts圖。 這是您可以在base-R中執行的方法:

with(df, plot(time, sucrose, type="n", ylab="contents"))
var <- names(df)[-1]
for(i in var) lines(df$time, df[,i])

在此處輸入圖片說明

但是,更優雅的解決方案是使用dplyr and ggplot2軟件包:

df <- df %>% 
      gather(content, val, -time)

ggplot(df, aes(time, val, col=content)) + geom_line()

在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM