How to include ticks for every date using ggplot?

Viewed 32

Let's consider data:

x <- c(
  "1900-01-01", "1900-04-01", "1900-07-01", "1900-10-01", "1901-02-01",
  "1901-05-01", "1901-08-01",
  "1901-11-01", "1902-02-01", "1902-05-01", "1902-08-01", "1902-11-01", "1903-02-01"
)
x <- as.Date(x)
y <- 1:length(x)
df <- data.frame("Date" = x, "Preds" = y) 

I want to make a plot using ggplot but with every date marked (in format %M-%Y):

My work so far:

ggplot(df, aes(x = Date, y = Preds)) +
  geom_line() +
  scale_x_date(date_breaks = "3 month", date_labels = "%b-%Y")

However this code brings one problem: starting day is in February, whereas our dates starts in Jan.

enter image description here

Do you know how can I back my time by one month ? And Do I have to specify exactly that I want to make a breaks by three months ? Or is there any generic solution that would allow me to just have ticks for every date without this specification ?

1 Answers

We just need to include breaks = x:

ggplot(df, aes(x = Date, y = Preds)) +
  geom_line() +
  scale_x_date(breaks = x, date_labels = "%b-%Y")
Related