一个图中的多个时间序列 [英] Multiple time series in one plot

查看:18
本文介绍了一个图中的多个时间序列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有几年的时间序列需要在一张图中绘制.最大系列的平均值为 340,最小值为 245,最大值为 900.最小系列的平均值为 7,最小值为 -28,最大值为 31.其余系列的值范围为 6 到 700.多年来,该系列遵循规律的年度和季节性模式,直到突然出现一个月的温度升高,随后死亡人数比平时增加很多.

I have a time series of several years that I need to plot in one graph. The largest series has a mean of 340 and a minimum of 245 and maximum of 900. The smallest series has a mean of 7 with a minimum of -28 and maximum of 31. The remaining series has values in the range of 6 to 700. The series follows a regular annual and seasonal pattern over years until suddenly there was an upsurge of temperature for a month which was followed by much increased deaths than usual.

我无法提供任何真实数据,但我模拟了以下数据并尝试了以下代码,该代码基于此处找到的示例代码 http://www.r-bloggers.com/multiple-y-axis-in-ar-plot/.但情节并没有产生我想要的.我有以下问题

I cannot provide any real data, but I have simulated the following data and tried the code below which was based on an example code found here http://www.r-bloggers.com/multiple-y-axis-in-a-r-plot/. But the plot has not produced what I have desired. I have the following questions

  1. 在情节中,很难清楚地描绘出任何一个系列,重要的事实隐藏在细节中.我怎样才能更好地呈现这些数据?
  2. Y 轴有不同的长度.我怎么会有相同长度的轴?我感谢有关如何改进此代码并呈现更好情节的任何想法和建议.我模拟的数据不能反映我的数据,因为我无法模拟反映极端天气事件时期的极端值.

非常感谢

temp<- rnorm(365, 5, 10)
mort<- rnorm(365, 300, 45)
poll<- rpois(365,  lambda=76)
date<-seq(as.Date('2011-01-01'),as.Date('2011-12-31'),by = 1)
df<-data.frame(date,mort,poll,temp)

windows(600,600)
par(mar=c(5, 12, 4, 4) + 0.1)

with(df, {
  plot(date, mort, axes=F, ylim=c(170,max(mort)), xlab="", ylab="",type="l",col="black", main="")
  points(date,mort,pch=20,col="black")
  axis(2, ylim=c(170,max(mort)),col="black",lwd=2)
  mtext(2,text="Mortality",line=2)

})

par(new=T)
plot(date, poll, axes=F, ylim=c(45,max(poll)), xlab="", ylab="", 
     type="l",col="red",lty=2, main="",lwd=1)
axis(2,  ylim=c(45,max(poll)),lwd=1,line=3.5)
points(date, poll,pch=20)
mtext(2,text="PM10",line=5.5)

par(new=T)
plot(date,  temp, axes=F, ylim=c(-28,max(temp)), xlab="", ylab="", 
     type="l",lty=3,col="brown", main="",lwd=1)
axis(2, ylim=c(-28,max(temp)),lwd=1,line=7)

points(date,  temp,pch=20)
mtext(2,text="Temperature",line=9)

axis(1,pretty(range(date),10))
mtext("date",side=1,col="black",line=2)

推荐答案

我会为每个变量使用单独的图,使它们的 y 轴不同.与在一个图中引入多个 y 轴相比,我更喜欢这一点.我将使用 ggplot2 来做到这一点,更具体地说是切面的概念:

I'd use separate plots for each variable, making their y-axis different. I like this better than introducing multiple y-axes in one plot. I will use ggplot2 to do this, and more specifically the concept of facetting:

library(ggplot2)
library(reshape2)

df_melt = melt(df, id.vars = 'date')
ggplot(df_melt, aes(x = date, y = value)) + 
    geom_line() + 
    facet_wrap(~ variable, scales = 'free_y', ncol = 1)

请注意,我将刻面堆叠在一起.这将使您能够轻松比较每个系列中的事件时间.或者,您可以将它们并排放置(在 facet_wrap 中使用 nrow = 1),这将使您能够轻松比较 y 值.

Notice that I stack the facets on top of each other. This will enable you to easily compare the timing of events in each of the series. Alternatively, you could put them next to each other (using nrow = 1 in facet_wrap), this will enable you to easily compare the y-values.

我们还可以引入一些极端:

We can also introduce some extremes:

df = within(df, {
        temp[61:90] = temp[61:90] + runif(30, 30, 50)
        mort[61:90] = mort[61:90] + runif(30, 300, 500)
    })
df_melt = melt(df, id.vars = 'date')
ggplot(df_melt, aes(x = date, y = value)) + 
    geom_line() + 
    facet_wrap(~ variable, scales = 'free_y', ncol = 1)

在这里您可以很容易地看到,温度的升高与死亡率的升高相关.

Here you can see easily that the increase in temp is correlated with the increase in mortality.

这篇关于一个图中的多个时间序列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆