簡體   English   中英

如何在ggplot中以月-年格式繪制帶有x軸刻度的時間序列

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

如何使用x軸在ggplot繪制以下數據,以將刻度顯示為“ Jan-99”格式。

我的數據如下:

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" 

我已經使用xts / zoo / ts軟件包進行了繪圖,但是想使用ggplot進行ggplot因為這給出了我的出版質量數據。

您可以嘗試以下操作:

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))

無論如何,如果您需要更改日期格式,請參考:“ http://docs.ggplot2.org/current/scale_date.html

暫無
暫無

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

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