简体   繁体   English

如何在ggplot中以月-年格式绘制带有x轴刻度的时间序列

[英]how to plot time series with x axis ticks in month-year format in ggplot

How to plot the data below in ggplot with x axis to show ticks in format as 'Jan-99'. 如何使用x轴在ggplot绘制以下数据,以将刻度显示为“ Jan-99”格式。

My data is as below: 我的数据如下:

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" 

I have done the plotting using xts / zoo / ts packages but would like to do using ggplot as this gives my publication quality figures. 我已经使用xts / zoo / ts软件包进行了绘图,但是想使用ggplot进行ggplot因为这给出了我的出版质量数据。

you can try the following: 您可以尝试以下操作:

rates$date <- as.character(rates$month, stringAsFactors = FALSE)
rates$date <- as.Date(rates$date, "%B-%d") 

# Now plot the graph #######

ggplot(rates)+
  geom_line(aes(x=date, y= Repo_Rate))+
  scale_x_date(labels = date_format("%B-%d))

In any case if you need to change the date format please refer to : " http://docs.ggplot2.org/current/scale_date.html " 无论如何,如果您需要更改日期格式,请参考:“ http://docs.ggplot2.org/current/scale_date.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM