使用ggplot2的多个时间序列图,每个时间序列图具有不同数量的观察值

编程入门 行业动态 更新时间:2024-10-19 16:27:32
本文介绍了使用ggplot2的多个时间序列图,每个时间序列图具有不同数量的观察值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在寻找一些有关多个时间序列图的帮助,按照以下描述。

我有一个具有以下结构的数据框。列isin正在重复,它有5个唯一值。对于每个isin,都有多行数据由t_week,MS和t_MS组成。每个isin具有不等数量的行。换句话说,数据帧有两个时间序列(t_week,MS)(t_week,t_MS),每个数据点的数据点数量不等。

我想使用ggplot2在一个图上绘制所有5个时间序列(t_week,MS)。我可以很容易地绘制多个相同长度的时间序列,但在这里寻找帮助正确的R方式。请帮助。

问候

K

str(df)'data.frame':95 obs。 4个变量: $ isin:chrIN0019960056IN0019960056IN0019960056IN0019960056... $ t_week:日期,格式:2006-01-092006-01- 16... $ MS:num 0 0 0.01 0.86 0.54 0.23 1.55 0.07 0.29 0.79 ... $ t_MS:num 0.14 0.14 0.14 0.75 0.35 0.31 0.63 0.28 0.54 0.52 ...

解决方案

canocial ggplot2 方式如下:

ggplot(df,aes(x = t_week,y = MS,color = isin)) + geom_line()

这将构造一个 t_week vs MS ,对于 isin 中的每个唯一元素使用不同的颜色线。时间序列不包含相同数量的行是没有问题的,它们甚至不必覆盖相同的时间范围。一个例子: $ $ p $ df_part1 = data.frame(t_week = seq(1,5,length = 100),MS = runif( 100),isin =A) df_part2 = data.frame(t_week = seq(2,6,length = 500),MS = runif(500)+ 1,isin =B) df = rbind(df_part1,df_part2) library(ggplot2) ggplot(df,aes(x = t_week,y = MS,color = isin))+ geom_line()

I am looking for some help with multiple time series plot as per the following description.

I have a data frame with the following structure. Column isin is repeating and it has 5 unique values. For each isin, there are multiple rows of data consisting of t_week, MS and t_MS. Each isin has unequal number of rows. In another words, the data frame has 2 time series (t_week, MS) (t_week, t_MS) for each isin with unequal number of data points.

I would like to plot all the 5 isin time series (t_week, MS) on a single plot using ggplot2. I can plot multiple time series of equal length easily, but looking for help here doing it right "The R" way. Please help.

regards

K

str(df) 'data.frame': 95 obs. of 4 variables: $ isin : chr "IN0019960056" "IN0019960056" "IN0019960056" "IN0019960056" ... $ t_week: Date, format: "2006-01-09" "2006-01-16" ... $ MS : num 0 0 0.01 0.86 0.54 0.23 1.55 0.07 0.29 0.79 ... $ t_MS : num 0.14 0.14 0.14 0.75 0.35 0.31 0.63 0.28 0.54 0.52 ...

解决方案

The canocial ggplot2 way is the following:

ggplot(df, aes(x = t_week, y = MS, color = isin)) + geom_line()

this wil construct a plot of t_week vs MS, with a differently colored line for each unique element in isin. It is no problem that the timeseries do not consist of the same number of rows, they don't even have to cover the same time range. An example:

df_part1 = data.frame(t_week = seq(1,5,length=100), MS = runif(100), isin = "A") df_part2 = data.frame(t_week = seq(2,6,length=500), MS = runif(500) + 1, isin = "B") df = rbind(df_part1, df_part2) library(ggplot2) ggplot(df, aes(x = t_week, y = MS, color = isin)) + geom_line()

更多推荐

使用ggplot2的多个时间序列图,每个时间序列图具有不同数量的观察值

本文发布于:2023-07-18 04:42:22,感谢您对本站的认可!
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:时间   多个   序列图   数量

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!