首页 文章

R中以月 - 月格式表示的x轴刻度的时间序列图

提问于
浏览
1

我无法让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的日期格式中转换'月'字体,仍然不会得到所需格式'4月01'的x轴刻度.....

需要帮助....

1 回答

  • 2

    尝试

    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)
    

相关问题