在ggplot中绘制多个时间序列 [英] Plotting multiple time-series in ggplot
问题描述
我有一个由10个变量组成的时间序列数据集。
我想创建一个时间序列图,其中每个变量都以不同颜色绘制,随着时间的推移,在同一张图上。值应该在Y轴上,在X轴上应该是日期。
点击此处对于数据集csv
这是我一直在使用的(可能是错误的)代码:
c.o< -read.csv(文件= co.csv,头= TRUE)
ggplot(共,AES(年,A,b,C,d,E,F) )+ geom_line()
以下是代码的输出结果:
任何人都可以指向正确的方向吗?我无法在以前的线程中找到任何东西。
问题已解决,请参阅下面的内容。
我想知道的事情:
是否可以在图表中添加一条额外的线条,该线条表示所有变量的平均值,并且有一些平滑的低于和高于如果你的数据被称为 df
>是这样的:
库(GGPLOT2)
库(reshape2)
meltdf< - 熔体(DF,ID = 年)
ggplot(meltdf,AES(X =年,Y =值,颜色=变量,基团=可变))+ geom_line()
因此,基本上在我的代码中,当我使用 aes()
im时,告诉它x轴是年
,y轴是值
,然后是颜色
/ grouping
是b 变量
。
ggplot2
的格式获取你的数据。用于 year
等的一个大列,然后当您通过单独的行为变量绘制
。
I have a time-series dataset consisting of 10 variables.
I would like to create a time-series plot, where each 10 variable is plotted in different colors, over time, on the same graph. The values should be on the Y axis and the dates on the X axis.
Click Here for dataset csv
This is the (probably wrong) code I have been using:
c.o<-read.csv(file="co.csv",head=TRUE)
ggplot(c.o, aes(Year, a, b, c, d, e,f))+geom_line()
and here's what the output from the code looks like:
Can anyone point me in the right direction? I wasn't able to find anything in previous threads.
PROBLEM SOLVED, SEE BELOW.
One additional thing I would like to know:
Is it possible to add an extra line to the plot which represents the average of all variables across time, and have some smoothing below and above that line to represent individual variations?
If your data is called df
something like this:
library(ggplot2)
library(reshape2)
meltdf <- melt(df,id="Year")
ggplot(meltdf,aes(x=Year,y=value,colour=variable,group=variable)) + geom_line()
So basically in my code when I use aes()
im telling it the x-axis is Year
, the y-axis is value
and then the colour
/grouping
is by the variable
.
The melt()
function was to get your data in the format ggplot2
would like. One big column for year
, etc.. which you then effectively split when you tell it to plot by separate lines for your variable
.
这篇关于在ggplot中绘制多个时间序列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!