![](/img/trans.png)
[英]how to plot time series with x axis ticks in month-year format in ggplot
[英]time series plot with x axis ticks in month-year format in R
我無法獲得x軸以“ Jan-99”格式顯示刻度線......
Ny數據如下:
head(rates,10)
Month Repo_Rate
1 Apr-01 9.00
2 May-01 8.75
3 Jun-01 8.50
4 Jul-01 8.50
5 Aug-01 8.50
6 Sep-01 8.50
7 Oct-01 8.50
8 Nov-01 8.50
9 Dec-01 8.50
10 Jan-02 8.50
sapply(rates,class)
Month Repo_Rate
"character" "numeric"
如果我將R的“日期”格式轉換為“月”列,仍然無法以所需的格式“ Apr-01”獲得x軸刻度。
這里需要幫助...。
嘗試
library(xts)
xt1 <- xts(rates$Repo_Rate, order.by = as.yearmon(rates$Month, '%b-%y'))
plot(xt1)
或使用zoo
library(zoo)
z1 <- with(rates, zoo(Repo_Rate, order.by= as.yearmon(Month, '%b-%y')))
plot(z1, xaxt = 'n')
tt <- time(z1)[seq(1, length(z1), by = 2)]
axis(1, tt, format(tt, '%b-%y'), las = 1)
要么
library(zoo)
library(ggplot2)
fmt <- "%b-%y"
z <- read.zoo(rates, FUN = as.yearmon, format = fmt)
autoplot(z) + scale_x_yearmon(format = fmt)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.