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!

4
  • 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. Commented Apr 9, 2013 at 22:32
  • I agree with @sebastian-c, if you make the scale datetime, ggplot will find nice breaks for you. Commented Apr 9, 2013 at 22:36
  • This, this or even this post should give you an idea.. Commented Apr 9, 2013 at 22:42
  • Implicit in the suggestion to use a datetime scale is to convert sp$date into a Date object. Commented Apr 9, 2013 at 22:48

1 Answer 1

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)

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the reply, it works. But i already found it out :) Thanks anyway

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.