[英]Plot year below month on x axis
1)一種簡單的方法是將年份放在每年的1月份(或者使用6月,例如,如果你想讓年份居中 - 即在下面的代碼中將1替換為6)。 另一種方法是通過使用%Y
代替%b\\n%Y
來替換1月份。
首先介紹一些測試數據 - 這通常應該在問題中完成,但這次我提供了一些。 測試數據幀d
的x
列是"yearmon"
類, y
是數字。 我假設問題中X軸上的數字用於表示月份,但如果它們打算用季度,則從"yearqtr"
類開始,然后使用scale_x_yearqtr
相應調整。
library(ggplot2)
library(zoo)
d <- data.frame(x = as.yearmon("2000-1") + 0:23/12, y = 1:24) # test data
x <- d$x
autoplot(d)
ggplot(d, aes(x, y)) +
geom_line() +
geom_point() +
scale_x_yearmon(breaks = x, lab = format(x, ifelse(cycle(x) == 1, "%b\n%Y", "%b")))
2)或者如果你開始使用動物園對象z
:
z <- read.zoo(d)
x <- time(z)
autoplot(z, geom = c("line", "point")) +
scale_x_yearmon(breaks = x, lab = format(x, ifelse(cycle(x) == 1, "%b\n%Y", "%b"))) +
xlab("")
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.