3

This might be fairly simple but yet i cant seem to find out how to do it. I got a nice plot with a group of lines of values in it. The y represents an amount, the x represents dates.

The problem is simple, there so many dates that they are printed on top of each other.

The problem

The code :

sp = rbind(sp1,sp2,sp3,sp4)
pm = ggplot(data = sp, aes(x = date, 
                                 y = amount, 
                                 colour=sm, 
                                 group=sm)) + 
        geom_line()

How can I make the x axis only print for example every 5 dates instead of all of them? Thanks in advance!

lmo
  • 37,904
  • 9
  • 56
  • 69
Patrick Aleman
  • 612
  • 6
  • 19
  • 1
    Have you considered using `theme` to make them diagonal? In any case, you're probably looking for the `breaks` argument in `scale_x_continuous` or `scale_x_datetime`. – sebastian-c Apr 09 '13 at 22:32
  • I agree with @sebastian-c, if you make the scale `datetime`, ggplot will find nice breaks for you. – Roman Luštrik Apr 09 '13 at 22:36
  • [**This**](http://stackoverflow.com/questions/15838533/rotating-axis-labels-in-date-format/15838671#15838671), [**this**](http://stackoverflow.com/questions/15417420/pick-the-latest-data-set-when-doing-geom-hline-in-ggplot2/15418202#15418202) or even [**this**](http://stackoverflow.com/questions/13837011/error-in-ggplot-in-r/13837298#13837298) post should give you an idea.. – Arun Apr 09 '13 at 22:42
  • Implicit in the suggestion to use a datetime scale is to convert `sp$date` into a Date object. – Brian Diggs Apr 09 '13 at 22:48

1 Answers1

3
library(scales)

sp = rbind(sp1,sp2,sp3,sp4)
pm = ggplot(data = sp, aes(x = date, y = amount, colour=sm, group=sm)) + 
    geom_line() +
    scale_x_date("x axis title", breaks = "5 years")

scale_x_date will sort out the x axis labels for you. To specify the label intervals use the scales packages as above. (p.s your dates need to be of class Date, POSIXct or POSIXlt)

OliE
  • 666
  • 6
  • 5