Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
210 views
in Technique[技术] by (71.8m points)

r - Multiple time series in one plot

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.

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. In the plot it is difficult to clearly depict any of the series and important facts are hidden in the detail. How can I better present this data?
  2. The Y axes have different lengths. How could I have axes with the same length? I appreciate any idea and suggestion on how to improve this code and present a better plot. The data I have simulated does not reflect my data as I am unable to simulate the extreme values that mirror the period of extreme weather episode.

Many thanks

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)
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Here are 6 approaches:

library(zoo)
z <- read.zoo(df)

# classic graphics in separate and single plots
plot(z)
plot(z, screen = 1)

# lattice graphics in separate and single plots
library(lattice)
xyplot(z)
xyplot(z, screen = 1)

# ggplot2 graphics in separate and single plots
library(ggplot2)
autoplot(z) + facet_free()
autoplot(z, facet = NULL)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...