Benjamin Telkamp
Benjamin Telkamp

Reputation: 1561

Adding specific ticks with date labels from a date variable on x-axis

I have a df with two variables:

Observations: 342
Variables: 2
$ date   <date> 2016-04-02, 2016-04-03, 2016-04-04, 2016-04-05, 2016-04-06, 2016-04-07, 2016-04-08, 2016...
$ visits <int> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,...

I want to plot visits against date, but on my x-axis i wish to see some specific ticks for dates when some special events happened (to show more visits were present immediately after). The code now shows me some some dates of course, but i want some dates added, let's say "2016-08-10" and "2017-01-25".

How can i extend my code to show additional ticks with labels?

p = ggplot(df, aes(x = date, y = visits)) + 
    geom_line(colour = "purple")

p 

Upvotes: 0

Views: 851

Answers (1)

bVa
bVa

Reputation: 3948

You can use scale_x_date:

set.seed(666)
df = data.frame(date = seq(as.Date("2016/4/1"), as.Date("2016/4/30"), by = "day"), visits = round(runif(30, 0, 30)))

ggplot(df, aes(x = date, y = visits)) + 
geom_line(colour = "purple") + 
scale_x_date(breaks = c(as.Date("2016-04-10"), as.Date("2016-04-20")))

plot

Upvotes: 2

Related Questions