使用layout()进行绘图的R代码在逐行执行时有效,但在包装到函数中时则无效

时间:2018-08-14 15:15:26

标签: r plot layout time-series xts

我正在使用xts软件包。

library(xts)

以下完全可以正常工作:

mydata = xts(rnorm(200), order.by = Sys.Date() - 1:200)
layout(matrix(c(1,1,2,3), 2, 2, byrow = TRUE),
         widths=c(1,1), heights=c(1,2))
plot(mydata, main = 'mydata Time Series')
acf(mydata, main = 'mydata acf', lag = 10)
pacf(mydata, main = 'mydata pacf', lag = 10)

good

但是当我使用完全相同的东西作为函数时,图显示得不好

mydata = xts(rnorm(200), order.by = Sys.Date() - 1:200)

return_plot <- function(mydata, lag = 10){
  layout(matrix(c(1,1,2,3), 2, 2, byrow = TRUE),
         widths=c(1,1), heights=c(1,2))
  plot(mydata, main = 'mydata Time Series')
  acf(mydata, main = 'mydata acf', lag = lag )
  pacf(mydata, main = 'mydata pacf', lag = lag)
}

return_plot(mydata)

bad

知道为什么会这样吗?

1 个答案:

答案 0 :(得分:4)

在函数中使用print(plot(mydata, main = 'mydata Time Series'))