2017-05-04 53 views
0

我有数据,看起来像这样:如何绘制数据的时间序列

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 

我怎样才能使使用时间列时间序列图?他们都在不断增加价值。

我看到这个帖子multiple-time-series-in-one-plot它采用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") 
+1

你想将数据转换为时间序列对象,还是简单地绘制这些值与时间的关系? – neilfws

+0

我想所有我想绘制的价值与时间。 –

回答

1

:以上表格

time-series plot

输入数据它似乎并不像ts情节是必要的。这里是你如何能做到这一点的基础-R:

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

enter image description here

的更优雅的解决办法然而,应当使用“dplyr and ggplot2`包:

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

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

enter image description here

+0

谢谢,这是有帮助的 –

+0

即时通讯不好意思,但我不明白如何代码的第二部分工作 –

+0

你指的是哪一部分? –