0

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.

Nishant
  • 1,063
  • 13
  • 40
  • Well first thing you nedd to do is convert your character vector to date format... see `?as.Date`, and perhaps http://stackoverflow.com/questions/6242955/converting-year-and-month-to-a-date-in-r. Then plotting should be straight forward. http://stackoverflow.com/questions/26458822/create-specific-date-range-in-ggplot2-scale-x-date , http://stackoverflow.com/questions/10576095/formatting-dates-with-scale-x-date-in-ggplot2, and lots more examples on SO – user20650 Nov 14 '15 at 04:36
  • 2
    Can you check the update by @G.Grothendieck on your previous post with `ggplot`. – akrun Nov 14 '15 at 04:59

1 Answers1

1

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"

Shiva Prakash
  • 1,849
  • 4
  • 21
  • 25