在R中绘制多变量时间序列数据

时间:2022-03-06 16:58:51

My data looks like this:

我的数据如下所示:

> head(Full.df)
        Date      Month       Week       Year Count.S Count.G Count.W Count.F
1 2006-01-02 2006-01-01 2006-01-02 2006-01-01       0       7       9       6
2 2006-01-03 2006-01-01 2006-01-02 2006-01-01       0      13      12       4
3 2006-01-04 2006-01-01 2006-01-02 2006-01-01       0      13      15       4
4 2006-01-05 2006-01-01 2006-01-02 2006-01-01       0      20       6       3
5 2006-01-06 2006-01-01 2006-01-02 2006-01-01       0      19      19       4
6 2006-01-07 2006-01-01 2006-01-02 2006-01-01       0       4      16       5

For a single variable I used the next line of code:

对于单个变量,我使用了下一行代码:

ggplot(data = Full.df, aes(Month, Count.S)) + stat_summary(fun.y = sum, geom ="line") + scale_x_date(
labels = date_format("%m-%y"),
breaks = "3 months")

I would like to plot Count.S, Count.G, Count.W, Count.F as four lines on the same plane, but I can't figure out how to plot all four variables in ggplot(or any other package for that matter). Thanks.

我想将Count.S,Count.G,Count.W,Count.F绘制为同一平面上的四条线,但我无法弄清楚如何在ggplot(或任何其他包)中绘制所有四个变量物)。谢谢。

EDIT: While the link provided to a different question is incredibly useful, the answer there explains how to plot different graphs in one image. However, I was wondering how to plot lines corresponding to various variables in a single XY axis.

编辑:虽然提供给另一个问题的链接非常有用,但那里的答案解释了如何在一个图像中绘制不同的图形。但是,我想知道如何在单个XY轴上绘制与各种变量对应的线。

1 个解决方案

#1


1  

Two ways of doing this:

有两种方法:

If sample data created as follows:

如果样本数据创建如下:

Full.df <- data.frame(Date = as.Date("2006-01-01") + as.difftime(0:364, units = "days"))
Full.df$Month <- as.Date(format(Full.df$Date, "%Y-%m-01"))
Full.df[paste0("Count.", c("S", "G", "W", "F"))] <-
  matrix(sample(100, 365 * 4, replace = TRUE), ncol = 4)

Optimal way using reshape2 package:

使用reshape2包的最佳方式:

molten <- melt(Full.df, id.vars = c("Date", "Month"),
  variable.name = "Category", value.name = "Count")
ggplot(data = molten, aes(x = Month, y = Count, colour = Category)) +
  stat_summary(fun.y = sum, geom ="line") +
  scale_x_date(labels = date_format("%m-%y"), breaks = "3 months")

Alternative using multiple geoms but no legend:

替代使用多个geoms但没有图例:

ggplot(Full.df, aes(x = Month)) +
  stat_summary(aes(y = Count.S), colour = "blue", fun.y = sum, geom = "line") +
  stat_summary(aes(y = Count.G), colour = "red", fun.y = sum, geom = "line") +
  stat_summary(aes(y = Count.W), colour = "green", fun.y = sum, geom = "line") +
  stat_summary(aes(y = Count.F), colour = "orange", fun.y = sum, geom = "line") +
  scale_x_date(labels = date_format("%m-%y"), breaks = "3 months")

#1


1  

Two ways of doing this:

有两种方法:

If sample data created as follows:

如果样本数据创建如下:

Full.df <- data.frame(Date = as.Date("2006-01-01") + as.difftime(0:364, units = "days"))
Full.df$Month <- as.Date(format(Full.df$Date, "%Y-%m-01"))
Full.df[paste0("Count.", c("S", "G", "W", "F"))] <-
  matrix(sample(100, 365 * 4, replace = TRUE), ncol = 4)

Optimal way using reshape2 package:

使用reshape2包的最佳方式:

molten <- melt(Full.df, id.vars = c("Date", "Month"),
  variable.name = "Category", value.name = "Count")
ggplot(data = molten, aes(x = Month, y = Count, colour = Category)) +
  stat_summary(fun.y = sum, geom ="line") +
  scale_x_date(labels = date_format("%m-%y"), breaks = "3 months")

Alternative using multiple geoms but no legend:

替代使用多个geoms但没有图例:

ggplot(Full.df, aes(x = Month)) +
  stat_summary(aes(y = Count.S), colour = "blue", fun.y = sum, geom = "line") +
  stat_summary(aes(y = Count.G), colour = "red", fun.y = sum, geom = "line") +
  stat_summary(aes(y = Count.W), colour = "green", fun.y = sum, geom = "line") +
  stat_summary(aes(y = Count.F), colour = "orange", fun.y = sum, geom = "line") +
  scale_x_date(labels = date_format("%m-%y"), breaks = "3 months")