如何使用 R 绘制带有自定义标题的 two-columned 时间序列网格?
How to plot a two-columned grid of time series with custom titles using R?
我有以下代码(虽然没有数据,很遗憾):
detrend_plot <- cbind(l_p_lng,l_vol_lng,l_p_oil,l_rgdpe, ldiff_p_lng,ldiff_vol_lng,ldiff_p_oil,ldiff_rgdpe)
plot.ts(detrend_plot, main="",)
给出了以下情节:
我想要做的是添加自定义标题、个人 y-axis 标签和 x-axis 标签。我知道这可以使用 GGPLOT
,尽管我对此知之甚少。有没有人遇到过类似的问题?我认为使用常规 plot.ts( )
函数是不可能的。
我认为您不能将多个标题和标签直接传递给 plot.ts
,但您可以使用每个标签向量遍历列:
set.seed(1)
z <- ts(matrix(rt(200 * 8, df = 3), 200, 8), start = c(1961, 1), frequency = 12)
## vectors of x, y, and main labels
xl <- sprintf('x label %s', 1:8)
yl <- sprintf('y label %s', 1:8)
ml <- sprintf('main label %s', 1:8)
par(mfrow = c(4, 2), mar = c(5, 5, 1, 1), oma = c(0, 0, 1, 2))
lapply(1:8, function(ii) {
x <- z[, ii, drop = FALSE]
plot(x, xlab = xl[ii], ylab = yl[ii], main = ml[ii])
})
您还可以使用列表传递参数向量(例如,对于 x 或 y-axis 限制):
ylim <- list(c(-10, 10))
ylim <- rep(ylim, 8)
par(mfrow = c(4, 2), mar = c(5, 5, 1, 1), oma = c(0, 0, 1, 2))
lapply(1:8, function(ii) {
x <- z[, ii, drop = FALSE]
plot(x, xlab = xl[ii], ylab = yl[ii], main = ml[ii], col = ii, ylim = ylim[[ii]])
})
为了让图形更接近默认的 plot.ts
外观,您只需将顶部和底部边距设置为 0 并调整轴(这就是 plot.ts
在幕后所做的)。此方法比 plot.ts
更冗长,但可以进行更多自定义:
par(mfrow = c(4, 2), mar = c(0, 5, 0, 1), oma = c(5, 0, 3, 2))
lapply(1:8, function(ii) {
x <- z[, ii, drop = FALSE]
plot(x, xlab = xl[ii], ylab = yl[ii], col = ii, axes = FALSE)
axis(2, las = 1)
box()
if (ii %in% 7:8) {
axis(1)
title(xlab = 'Year', xpd = NA)
}
if (ii %in% 1:2)
title(main = c('Group 1', 'Group 2')[ii], xpd = NA, line = 1)
})
我有以下代码(虽然没有数据,很遗憾):
detrend_plot <- cbind(l_p_lng,l_vol_lng,l_p_oil,l_rgdpe, ldiff_p_lng,ldiff_vol_lng,ldiff_p_oil,ldiff_rgdpe)
plot.ts(detrend_plot, main="",)
给出了以下情节:
我想要做的是添加自定义标题、个人 y-axis 标签和 x-axis 标签。我知道这可以使用 GGPLOT
,尽管我对此知之甚少。有没有人遇到过类似的问题?我认为使用常规 plot.ts( )
函数是不可能的。
我认为您不能将多个标题和标签直接传递给 plot.ts
,但您可以使用每个标签向量遍历列:
set.seed(1)
z <- ts(matrix(rt(200 * 8, df = 3), 200, 8), start = c(1961, 1), frequency = 12)
## vectors of x, y, and main labels
xl <- sprintf('x label %s', 1:8)
yl <- sprintf('y label %s', 1:8)
ml <- sprintf('main label %s', 1:8)
par(mfrow = c(4, 2), mar = c(5, 5, 1, 1), oma = c(0, 0, 1, 2))
lapply(1:8, function(ii) {
x <- z[, ii, drop = FALSE]
plot(x, xlab = xl[ii], ylab = yl[ii], main = ml[ii])
})
您还可以使用列表传递参数向量(例如,对于 x 或 y-axis 限制):
ylim <- list(c(-10, 10))
ylim <- rep(ylim, 8)
par(mfrow = c(4, 2), mar = c(5, 5, 1, 1), oma = c(0, 0, 1, 2))
lapply(1:8, function(ii) {
x <- z[, ii, drop = FALSE]
plot(x, xlab = xl[ii], ylab = yl[ii], main = ml[ii], col = ii, ylim = ylim[[ii]])
})
为了让图形更接近默认的 plot.ts
外观,您只需将顶部和底部边距设置为 0 并调整轴(这就是 plot.ts
在幕后所做的)。此方法比 plot.ts
更冗长,但可以进行更多自定义:
par(mfrow = c(4, 2), mar = c(0, 5, 0, 1), oma = c(5, 0, 3, 2))
lapply(1:8, function(ii) {
x <- z[, ii, drop = FALSE]
plot(x, xlab = xl[ii], ylab = yl[ii], col = ii, axes = FALSE)
axis(2, las = 1)
box()
if (ii %in% 7:8) {
axis(1)
title(xlab = 'Year', xpd = NA)
}
if (ii %in% 1:2)
title(main = c('Group 1', 'Group 2')[ii], xpd = NA, line = 1)
})