简体   繁体   中英

Time-series plot for different seasons in the same plot

I would like plot a time series data for different seasons of the year (grouped as cold or warm) in the same plot and identify the two periods. How can I do that?

Reproducible data follows:

library(gamair) 
data(chicago) 
chicago$date<-seq(from=as.Date("1987-01-01"), to=as.Date("2000-12-31"),length=5114)
data.cold <- subset(chicago, quarters(date) %in% c("Q1", "Q4"))
data.warm <- subset(chicago, quarters(date) %in% c("Q2", "Q3"))

These codes below will create two separate plots for the two periods:

with(data.cold,plot(date,death,pch=".", ylab= expression("Mortality Count"), main = "Daily Mortality in cold season"))
with(data.warm ,plot(date,death,pch=".", ylab= expression("Mortality Count"), main = "Daily Mortality in warm season"))

This following code creates one plot, but there is no clear demarcation of the two periods. My wish is to create identifiable plot for the two periods in a single plot.

with(chicago,plot(date,death,pch=".", ylab= expression("Mortality Count"), main = "Daily Mortality from  1987-2000"))

Have a look at ?points :

with(data.cold,plot(date,death,pch=".", ylab= expression("Mortality Count"), main = "Daily Mortality in cold season"))
with(data.warm ,points(date,death,pch=".", ylab= expression("Mortality Count"), main = "Daily Mortality in warm season", col="red"))
legend("topright", c("cold", "warm"), fill=c("black", "red"))

Does that work for you?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM