【问题标题】:R - Plot interpolated values a different colour in time series plot?R - 在时间序列图中绘制不同颜色的插值?
【发布时间】:2014-07-21 16:02:49
【问题描述】:

谁能帮帮我。我有一个数据集,其中包含我用动物园插值的 NA 值。我添加了一个“颜色列”,希望可以创建一个线图(时间序列),其中插值以与线的其余部分不同的颜色绘制。也就是说,由插值点之前和之后的点定义的线段应该是红色的,而不是黑色的。

我在此处附上了我的表格示例(颜色为“红色”的地方定义了已插值的值)。到目前为止,我还在这里放了一张图表的图像和所需的输出:

https://drive.google.com/folderview?id=0B_eJi0urUAzFM0JBS1ZIbUdGck0&usp=drive_web

到目前为止,这是我的代码。代码的“行”部分是我希望将颜色定义为数据框中的列的地方:

par(mfrow=c(2,1), mar=c(4,4.5,2,2), mgp=c(2,0.6,0))
x.limit <- round(range(UN.GRACE.Int$DecimDate), 2)
plot(NULL, type="n", xlim=x.limit, ylim=c(-20, 25), xlab="Year", ylab="GRACE-TWS (cm)",     axes=F)
box(lwd=1.5)
abline(h=0, col="gray50", lty=1)
axis(1, seq(2003, 2012, 1), cex.axis=0.8)
axis(2, seq(-20, 25, 5), las=1, cex.axis=0.8)
minor.tick(nx=4, ny=0, tick.ratio=0.5) 
lines(UN.GRACE.Int[,2] ~ UN.GRACE.Int[,1], type="l", lwd=3, col=UN.GRACE.Int[,3])
tws.slope <- round(as.vector(coef(lm(UN.GRACE.Int[,2] ~ UN.GRACE.Int[,1]))[2]), 2)
tws.sdev <- round(as.vector(coef(summary(lm(UN.GRACE.Int[,2] ~ UN.GRACE.Int[,1])))[,  "Std. Error"][2]), 2)
abline(lm(UN.GRACE.Int[,2] ~ UN.GRACE.Int[,1]), lwd=2.5, lty=2, col=2)
mtext(paste("Trend (cm/year): ", tws.slope, "±", tws.sdev, sep=""), cex=0.8, side=1,  line=-1.1) 

任何帮助将不胜感激 - 谢谢

【问题讨论】:

  • 所以,你想得到单行输出,而不是两行,我理解正确吗?你能分享一张你在想什么的图片吗(即使是画图也可以)。
  • 是的,这就是我需要的:) 这是一张图片:drive.google.com/file/d/0B_eJi0urUAzFV09tN0hLWHlnbnc/…
  • 你如何决定要着色的线段的长度?这确实是一件棘手的事情,您可以在下面看到我的答案,但它们都为要点着色。编辑:在进一步考虑它时,最好的方法是在第一行顶部绘制另一行,如果这是你需要的,你需要将参数提供给line()
  • 感谢您的回答 - 我意识到片段的长度可能很难绘制。这似乎是最明智的答案,所以谢谢!
  • 长度总是到下一点的距离吗?如果是这样,我可能会在解决方案中进行编辑......我认为这是相对可行的。

标签: r plot


【解决方案1】:

如果我理解正确,您希望插值点以不同的颜色显示。您可以使用 R 中的 type="o" 选项来完成此操作,该选项会给出过度绘制的线条。这是一些调整后的代码,可生成以下图表。我取出了 minor.tick 命令,因为它一定来自我没有的包,但除此之外它工作正常(在我的本地机器上使用 R 2.15.3)。

您会注意到我只是直接绘制项目,而不是调用 plot 为 NULL 然后添加行。这大大简化了代码。您可以在绘图调用中使用pch 参数来更改使用的符号,还可以根据需要更改lwd 参数。事实上,您可以轻松地为 pch 提供不同的值作为插值,就像您所做的颜色一样 - 它接受向量作为参数。

par(mar=c(4,4.5,2,2), mgp=c(2,0.6,0))
x.limit <- round(range(UN.GRACE.Int$DecimDate), 2)
plot(UN.GRACE.Int[,2] ~ UN.GRACE.Int[,1],
     type="o",
     pch=18,
     col=UN.GRACE.Int[,3],
     xlim=x.limit,
     ylim=c(-20, 25),
     xlab="Year",
     ylab="GRACE-TWS (cm)",
     axes=F)
box(lwd=1.5)
abline(h=0, col="gray50", lty=1)
axis(1, seq(2003, 2012, 1), cex.axis=0.8)
axis(2, seq(-20, 25, 5), las=1, cex.axis=0.8)
tws.slope <- round(as.vector(coef(lm(UN.GRACE.Int[,2] ~ UN.GRACE.Int[,1]))[2]), 2)
tws.sdev <- round(as.vector(coef(summary(lm(UN.GRACE.Int[,2] ~ UN.GRACE.Int[,1])))[,  "Std. Error"][1]), 2)
abline(lm(UN.GRACE.Int[,2] ~ UN.GRACE.Int[,1]), lwd=2.5, lty=2, col=2)
mtext(paste("Trend (cm/year): ", tws.slope, "±", tws.sdev, sep=""), cex=0.8, side=1,  line=-1.1)

如果您只是想查看插值数据的点,也可以稍后添加这些点。这可以按如下方式完成:

par(mar=c(4,4.5,2,2), mgp=c(2,0.6,0))
x.limit <- round(range(UN.GRACE.Int$DecimDate), 2)
plot(UN.GRACE.Int[,2] ~ UN.GRACE.Int[,1],
     type="l",
     pch=18,
     col="black",
     xlim=x.limit,
     ylim=c(-20, 25),
     xlab="Year",
     ylab="GRACE-TWS (cm)",
     axes=F)
box(lwd=1.5)
abline(h=0, col="gray50", lty=1)
axis(1, seq(2003, 2012, 1), cex.axis=0.8)
axis(2, seq(-20, 25, 5), las=1, cex.axis=0.8)
tws.slope <- round(as.vector(coef(lm(UN.GRACE.Int[,2] ~ UN.GRACE.Int[,1]))[2]), 2)
tws.sdev <- round(as.vector(coef(summary(lm(UN.GRACE.Int[,2] ~ UN.GRACE.Int[,1])))[,  "Std. Error"][3]), 2)
abline(lm(UN.GRACE.Int[,2] ~ UN.GRACE.Int[,1]), lwd=2.5, lty=2, col=2)
mtext(paste("Trend (cm/year): ", tws.slope, "±", tws.sdev, sep=""), cex=0.8, side=1,  line=-1.1)
points(x=UN.GRACE.Int[UN.GRACE.Int$Col.CSR=="red",1],
       y=UN.GRACE.Int[UN.GRACE.Int$Col.CSR=="red",2],
       pch=16,
       col="red")

编辑添加:这是一种通过重叠原始图来为线段本身着色的方法,假设要着色的距离总是长度为一。它使用了一个 quick'n'dirty for() 循环,但如果你愿意,它也可以变成一个函数。

par(mar=c(4,4.5,2,2), mgp=c(2,0.6,0))
x.limit <- round(range(UN.GRACE.Int$DecimDate), 2)
plot(UN.GRACE.Int[,2] ~ UN.GRACE.Int[,1],
     type="l",
     pch=18,
     col="black",
     xlim=x.limit,
     ylim=c(-20, 25),
     xlab="Year",
     ylab="GRACE-TWS (cm)",
     axes=F)
box(lwd=1.5)
abline(h=0, col="gray50", lty=1)
axis(1, seq(2003, 2012, 1), cex.axis=0.8)
axis(2, seq(-20, 25, 5), las=1, cex.axis=0.8)
tws.slope <- round(as.vector(coef(lm(UN.GRACE.Int[,2] ~ UN.GRACE.Int[,1]))[2]), 2)
tws.sdev <- round(as.vector(coef(summary(lm(UN.GRACE.Int[,2] ~ UN.GRACE.Int[,1])))[,  "Std. Error"][5]), 2)
abline(lm(UN.GRACE.Int[,2] ~ UN.GRACE.Int[,1]), lwd=2.5, lty=2, col=2)
mtext(paste("Trend (cm/year): ", tws.slope, "±", tws.sdev, sep=""), cex=0.8, side=1,  line=-1.1)

line_segs <- cbind(lstart=UN.GRACE.Int[which(UN.GRACE.Int$Col.CSR=="red")-1,c("DecimDate","CSR")],
                   lend=UN.GRACE.Int[which(UN.GRACE.Int$Col.CSR=="red")+1,c("DecimDate","CSR")])

for(x in 1:nrow(line_segs)) {

    lines(x=c(line_segs[x,1],line_segs[x,3]),
          y=c(line_segs[x,2],line_segs[x,4]),
          lwd=3,
          col="red")
}

【讨论】:

    猜你喜欢
    • 2021-03-24
    • 1970-01-01
    • 2016-07-27
    • 2018-04-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-26
    • 1970-01-01
    相关资源
    最近更新 更多