Nishant
Nishant

Reputation: 1121

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'.

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.

Upvotes: 0

Views: 1617

Answers (1)

Shiva Prakash
Shiva Prakash

Reputation: 1919

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"

Upvotes: 1

Related Questions