简体   繁体   English

如何使用 plot.zoo 在每个多图中自定义 y 轴的颜色和比例?

[英]How to customize color and scale of y axis in each multiple plot using plot.zoo?

This a reproducible example of my data这是我的数据的可重现示例

dat<-data.frame(
prec<-rnorm(650,mean=300),
temp<-rnorm(650,mean = 22),
pet<-rnorm(650,mean = 79),
bal<-rnorm(650,mean = 225))
colnames(dat)<-c("prec","temp","pet","bal")
    
dat<-ts(dat,start = c(1965,1),frequency = 12)
#splines
fit1<-smooth.spline(time(dat),dat[,1],df=25)
fit2<-smooth.spline(time(dat),dat[,2],df=25)
fit3<-smooth.spline(time(dat),dat[,3],df=25)
fit4<-smooth.spline(time(dat),dat[,4],df=25)
    
dat2 <- cbind(dat, fitted(fit1), fitted(fit2), fitted(fit3), fitted(fit4))
plot.zoo(window(dat2, start = 1965), xlab = "", screen = 1:4, 
col = c(1:4, 1, 2, 3, 4),yax.flip = TRUE, bty="n")

How can I modify the color and the scale of the y axes in each plot to match the same color of the time series?如何修改每个图中 y 轴的颜色和比例以匹配时间序列的相同颜色?

Create dat2 which contains both the series and the smooth splines, use window to start it at 1965, specify in screen= that the the columns be in panels 1:4 (it will recycle for the last 4 columns) and specify that the last 4 columns be black, ie 1, or modify colors to suit.创建包含系列和平滑样条dat2 ,使用window在 1965 年开始,在screen=中指定列在面板中 1:4(它将回收最后 4 列)并指定最后 4列是黑色的,即 1,或修改颜色以适应。

dat2 <- cbind(dat, fitted(fit1), fitted(fit2), fitted(fit3), fitted(fit4))
plot.zoo(window(dat2, start = 1965), xlab = "", screen = 1:4, 
  col = c(1:4, 1, 1, 1, 1))

截屏

Regarding the comment, to me it seems easier to read if the ticks, labels and axes are black but if you want to do that anyways use the mfrow= graphical parameter with a for loop and specify col.axis and col.lab in the plot.zoo call:关于评论,对我来说,如果刻度、标签和轴是黑色的,则似乎更容易阅读,但如果您无论如何都想这样做,请使用带有for循环的mfrow=图形参数并在plot.zoo指定col.axiscol.lab plot.zoo电话:

nc <- ncol(dat)
cols <- 1:nc  # specify desired colors
opar <- par(mfrow = c(nc, 1), oma = c(6, 0, 5, 0), mar = c(0, 5.1, 0, 2.1))
for(i in 1:nc) {
  dat1965 <- window(dat[, i], start = 1965)
  plot(as.zoo(dat1965), col = cols[i], ylab = colnames(dat)[i], col.axis = cols[i],
    col.lab = cols[i])
  fit <- smooth.spline(time(dat1965), dat1965, df = 25)
  lines(cbind(dat1965, fitted(fit))[, 2])  # coerce fitted() to ts
}
par(opar)
mtext("4 plots", line = -2, font = 2, outer = TRUE)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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