R 为什么 plot.xts 在调用行后创建额外的图形?
R Why plot.xts creates extra graph after calling lines?
考虑以下两个图,第一个使用通用绘图函数,第二个使用 plot.xts:
一般情节
par(mfrow = c(2,1))
plot(1:5, type="l", main = "generic plot")
lines(5:1)
正如预期的那样,lines 函数添加到现有图形中,因此它生成单个图形
我已经设置 mfrow = c(2,1) 来向您展示,只有一张图。
现在使用 xts 数据:
par(mfrow = c(2,1))
plot(xts(x = 1:5, order.by = 1:5+as.Date("2017-01-01")), type="l", main = "plot.xts")
lines(xts(x = 5:1, order.by = 1:5+as.Date("2017-01-01")), main = "plot.xts")
没想到结果是两个图。为什么?
我的具体情况有点复杂,但我发现这段代码是重现我的问题的最简单方法。基本上我想在一个图上继续添加 xts 数据。我能够使用通用的 plot 和 lines 函数来实现这一点。
平台信息:
R 版本 3.4.3 (2017-11-30)
平台:x86_64-apple-darwin15.6.0(64 位)
运行下:macOS High Sierra 10.13.2
quantmod_0.4-12
xts_0.10-1
xts
绘图函数实际上并不像基本绘图函数那样工作,尽管它们的调用看起来是一样的。
plot.xts
函数returns一个对象。默认情况下,如果您不在任何地方分配对象,R 将 "print" 导致绘制绘图的对象。 lines.xts
函数更改最近的绘图对象并添加一个新系列。由于未保存绘图,因此也会打印该新对象。这个新对象会记住第一个系列以及新添加的系列。
最好的办法是保存这些对象,只在添加完图层后打印它们。例如
par(mfrow = c(2,1))
pp <- plot(xts(x = 1:5, order.by = 1:5+as.Date("2017-01-01")), type="l", main = "plot.xts")
pp <- lines(xts(x = 5:1, order.by = 1:5+as.Date("2017-01-01")), main = "plot.xts")
pp #plot will be drawn here
考虑以下两个图,第一个使用通用绘图函数,第二个使用 plot.xts:
一般情节
par(mfrow = c(2,1))
plot(1:5, type="l", main = "generic plot")
lines(5:1)
正如预期的那样,lines 函数添加到现有图形中,因此它生成单个图形
我已经设置 mfrow = c(2,1) 来向您展示,只有一张图。 现在使用 xts 数据:
par(mfrow = c(2,1))
plot(xts(x = 1:5, order.by = 1:5+as.Date("2017-01-01")), type="l", main = "plot.xts")
lines(xts(x = 5:1, order.by = 1:5+as.Date("2017-01-01")), main = "plot.xts")
没想到结果是两个图。为什么?
我的具体情况有点复杂,但我发现这段代码是重现我的问题的最简单方法。基本上我想在一个图上继续添加 xts 数据。我能够使用通用的 plot 和 lines 函数来实现这一点。
平台信息: R 版本 3.4.3 (2017-11-30) 平台:x86_64-apple-darwin15.6.0(64 位) 运行下:macOS High Sierra 10.13.2 quantmod_0.4-12 xts_0.10-1
xts
绘图函数实际上并不像基本绘图函数那样工作,尽管它们的调用看起来是一样的。
plot.xts
函数returns一个对象。默认情况下,如果您不在任何地方分配对象,R 将 "print" 导致绘制绘图的对象。 lines.xts
函数更改最近的绘图对象并添加一个新系列。由于未保存绘图,因此也会打印该新对象。这个新对象会记住第一个系列以及新添加的系列。
最好的办法是保存这些对象,只在添加完图层后打印它们。例如
par(mfrow = c(2,1))
pp <- plot(xts(x = 1:5, order.by = 1:5+as.Date("2017-01-01")), type="l", main = "plot.xts")
pp <- lines(xts(x = 5:1, order.by = 1:5+as.Date("2017-01-01")), main = "plot.xts")
pp #plot will be drawn here