I have some time series data and I would like to customize the x-axis (dates) to show the date labels where I obtain measurements, as opposed to having regular breaks per week/month/year.
Sample data:
dates <- as.Date("2011/01/01") + sample(0:365, 5, replace=F)
number <- sample(1:100, 5)
df <- data.frame(
dates = dates,
number = number
)
This way I can plot my df with regular breaks every month...
ggplot(df, aes(as.Date(dates), number)) +
geom_point(size=6) +
geom_segment(aes(x = dates, y = 0, xend = dates, yend = number),
size=0.5, linetype=2) +
scale_x_date(breaks = date_breaks("1 month"), labels = date_format("%d-%b-%Y")) +
theme(axis.text.x = element_text(angle=90, hjust=1, vjust=0.5))
... but I would like to set the major breaks to the actual 5 dates in df$dates
. It works with a normal continuous scale (scale_x_continuous(breaks = c(1, 3, 7, 9))
) but I can't figure out how to do it for a continuous date scale.
I am looking to do something like...
scale_dates_continuous(breaks = df$dates)
...but that doesn't exist unfortunately. Thanks lot for your help!
Please read ?scale_x_date
, about the breaks
argument: you can use a "vector of breaks". Thus, try
scale_x_date(breaks = df$dates, labels = date_format("%d-%b-%Y"))