簡體   English   中英

如何在R中的x軸上自定義日期(年)

[英]How to customise dates (years) on the x-axis in R

我正在努力自定義R中x軸上的跳轉大小。

當前代碼:

par(mfrow = c(2,2))

r.star.ts.sp <- ts(r.star.sp, frequency = 4, start = c(1978,1), end = c(2018, 1)) 
# Big drop in r* around 123th quarter equivalent to 2008:Q4 / 2009:Q1
trendgrowth.ts.sp <- ts(trendgrowth.sp, frequency = 4, start = c(1978,1), end = c(2018, 1))

plot.ts(r.star.ts.sp,
        ylim = c(-3, 4), xlab = " ", ylab = " ", axes = F, col = "blue")
lines(trendgrowth.ts.sp, lty = 2, col = "red")
abline(h = 0, lty = 2)

title(main ="r* and Trend Growth", line = 0.5, font.main = 3)
box()
axis(4)
axis(1)

legend("bottomleft", legend = c("r*", "Trend Growth (g)"), 
       bty = "n", lty = c(1,2), col = c("blue", "red"), horiz = F, text.col = "black", 
       cex = 1, pt.cex = .5, inset = c(0.02, 0.02))

# -------------------------------------- #
# Plot output gap and real rate gap
# -------------------------------------- #
outputgap.ts.sp <- ts(outputgap.sp, frequency = 4, start = c(1978,1), end = c(2018, 1))
realrategap.ts.sp <- ts(realrategap.sp, frequency = 4, start = c(1978,1), end = c(2018, 1))

plot.ts(outputgap.ts.sp, ylim = c(-20, 15), xlab=" ", ylab=" ", axes = F, col="blue")
lines(realrategap.ts.sp, lty = 2, col = "red")
abline(h = 0, lty = 2)

legend("topright", legend = c("Output Gap", "Real Rate Gap"), 
       bty = "n", lty = c(1,2), col = c("blue", "red"), horiz = F, text.col = "black", 
       cex = 1, pt.cex = .5, inset = c(0.02, 0.02))

title(main = "Output Gap and Real Rate Gap", line = 0.5, font.main = 3)
box()
axis(side = 4)
axis(side = 1)

如何將1975年到2020年的x軸年份指定為跳躍5年?

此外,(離題)我需要兩個相鄰的圖,但是我覺得par(mfrow = c(2,2))是不正確的陳述。 但是,將其更改為par(mfrow = c(1,2))會生成異常大數字。

謝謝!

OP要求在1975年到2020年之間的x軸上指定年份,跳距為5年

這可以通過

axis(1, at = seq(1975L, 2020L, by = 5L))

但是,結果可能取決於mfrow參數。 這是一個使用par(mfrow = c(2, 2))虛擬示例:

在此處輸入圖片說明

請注意,左圖的x軸由axis(1)創建,而右圖的x軸由axis(1, at = seq(1975L, 2020L, by = 5L)) 還要注意兩個圖表下方的大空白。

使用par(mfrow = c(1, 2)) ,結果變為

在此處輸入圖片說明

在此,右圖顯示了未標記的(“次要”)刻度線。 ?parmfrow部分中對此進行了解釋: 在具有正好兩行和兩列的布局中,“ cex”的基本值減少了0.83倍 因此,字體大小減少了17%,可以標記所有刻度線而不會過度繪制。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM