moodymudskipper
moodymudskipper

Reputation: 47310

R ggplot custom grid

I want to plot values versus dates and adjust the grids of a graph so that on the x axis the main grid falls on every Sunday, and keep the minor grid for everyday. On the y axis i would like to have the major grid at every unit, and no minor grid.

The following example, with default grid, places the major grid every 2 units on each axis, and the minor grid every unit (which is actually OK if we can change only the major grid). How can I solve this ?

require(ggplot2)
data <- data.frame(date = seq(as.Date("2016-05-02"),as.Date("2016-05-16"),2),
              age_in_days = seq(1,15,2)))

ggplot(data=data,aes(x=date,y=age_in_days)) + geom_line()

Upvotes: 1

Views: 567

Answers (1)

moodymudskipper
moodymudskipper

Reputation: 47310

scale_x_date needs to be used in x.

To work as follow library "scale" should be loaded. The default date format is then changed for some reason, keep it as before the last line is necessary.

library(scales)
ggplot(data=data,aes(x=date,y=age_in_days)) + geom_line() +
  scale_x_date(breaks = seq(Sys.Date(), as.Date("2016-08-01"), by="1 week"),
labels = date_format("%b %d"))

Upvotes: 2

Related Questions