【问题标题】:How to customize color and scale of y axis in each multiple plot using plot.zoo?如何使用 plot.zoo 在每个多重图中自定义 y 轴的颜色和比例?
【发布时间】:2021-01-25 14:53:57
【问题描述】:

这是我的数据的可重现示例

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")

如何修改每个图中 y 轴的颜色和比例以匹配时间序列的相同颜色?

【问题讨论】:

    标签: r time-series panel axis zoo


    【解决方案1】:

    创建包含系列和平滑样条线的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))
    

    关于评论,对我来说,如果刻度、标签和轴是黑色的,似乎更容易阅读,但如果你想这样做,无论如何都要使用带有 for 循环的 mfrow= 图形参数并指定 col.axiscol.labplot.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)
    

    【讨论】:

    • 非常感谢@G。格洛腾迪克。最后一个问题。您知道我如何更改每个面板的 ylab 轴颜色(标签、刻度和值),因此它们将具有与其相应时间序列相同的颜色。例如data.temp,其值 20、22、24 和 yticks 必须为红色以匹配其时间序列颜色。到目前为止,我已经尝试过 col.axis="red" 但它对所有面板都发生了变化
    • 见最后的补充。
    • 再次感谢您的回复。但我想使用 plot.zoo 因为它更容易操纵情节。所以请您看一下修改后的代码(我编辑了帖子)。在这种情况下,如何修改每个图中轴的颜色和 y 轴的比例?
    • 我已经稍微简化了代码,但条件 col.axis 和 col.lab 不受支持,在任何图形系统中也不是直截了当的。无论您使用哪种系统,直接执行此操作的方法是分别创建每个绘图,然后像我们所做的那样将它们放在一起。建议您使用提供的代码,或者如果您真的想要在 plot.zoo 中提交补丁以支持向量 col.axis 和 col.lab(支持标量 col.axis 和 col.lab,我们在上面的代码中使用它们)。
    • 我现在明白为什么我不能修改它了。我将接受您的建议并使用提供的代码。非常感谢您的时间和帮助,您为我节省了大量时间。亲切的问候
    猜你喜欢
    • 2015-12-21
    • 1970-01-01
    • 1970-01-01
    • 2014-01-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多