-1

I want to produce a plot (Base R) that shows dates in the x-axis.

This is my code so far:

plot(dr[[ncol(dr)]][-(length(dr[[ncol(dr)]]))], type = 'l', main = 
paste("Title - ", colnames(dr[ncol(dr)])), xlab = "", ylab = "",
xaxt = 'n', col = "red", lwd = 2, cex.main = 2, ylim = c(0,0.5))
title(xlab = "Period", line=0.5, cex.lab = 1.5)

How do I put dates on the x axis?

Here is the plot:

enter image description here

This is my variable date:

 [1] "2008-11-28" "2008-12-31" "2009-01-30" "2009-02-27" "2009-03-31" "2009-04-30" "2009-05-29" "2009-06-30" "2009-07-31"
[10] "2009-08-31" "2009-09-30" "2009-10-30" "2009-11-30" "2009-12-31" "2010-01-29" "2010-02-26" "2010-03-31" "2010-04-30"
[19] "2010-05-31" "2010-06-30" "2010-07-30" "2010-08-31" "2010-09-30" "2010-10-29" "2010-11-30" "2010-12-31" "2011-01-31"
[28] "2011-02-28" "2011-03-31" "2011-04-29" "2011-05-31" "2011-06-30" "2011-07-29" "2011-08-31" "2011-09-30" "2011-10-31"
[37] "2011-11-30" "2011-12-30" "2012-01-31" "2012-02-29" "2012-03-30" "2012-04-30" "2012-05-31" "2012-06-29" "2012-07-31"
[46] "2012-08-31" "2012-09-28" "2012-10-31" "2012-11-30" "2012-12-31" "2013-01-31" "2013-02-28" "2013-03-29" "2013-04-30"
[55] "2013-05-31" "2013-06-28" "2013-07-31" "2013-08-30" "2013-09-30" "2013-10-31" "2013-11-29" "2013-12-31" "2014-01-31"
[64] "2014-02-28" "2014-03-31" "2014-04-30" "2014-05-30" "2014-06-30" "2014-07-31" "2014-08-29" "2014-09-30" "2014-10-31"
[73] "2014-11-28" "2014-12-31" "2015-01-30" "2015-02-27" "2015-03-31" "2015-04-30" "2015-05-29" "2015-06-30" "2015-07-31"
[82] "2015-08-31" "2015-09-30" "2015-10-30" "2015-11-30" "2015-12-31"
Jacob
  • 77,566
  • 24
  • 149
  • 228
Luka
  • 113
  • 1
  • 6

1 Answers1

0

Since you missed your y variable I will put an example using your variable date.

data <- as.Date(data, format= "%Y-%m-%d", tz = "UTC", origin="1970-01-01")
ats = seq(1, length(runif(length(data))),2) # this is the position
labs = seq.Date(min(data), max(data), length.out = length(ats)) # these are the labels to plot in the x axis
plot(runif(length(ats)) ~ ats , type = 'l', main =  "Title ", xlab = "", ylab = "",
     xaxt = 'n', col = "red", lwd = 2, cex.main = 2, ylim = c(0,1)) # note here that the values are randomly generated using `runif`. 
axis(1, at = ats, labels = labs, las= 2)

Output: enter image description here

Data:

data <- c("2008-11-28", "2008-12-31",   "2009-01-30",   "2009-02-27",   "2009-03-31",   "2009-04-30",   "2009-05-29",   "2009-06-30",   "2009-07-31",
"2009-08-31",   "2009-09-30",   "2009-10-30",   "2009-11-30",   "2009-12-31",   "2010-01-29",   "2010-02-26",   "2010-03-31",   "2010-04-30",
"2010-05-31",   "2010-06-30",   "2010-07-30",   "2010-08-31",   "2010-09-30",   "2010-10-29",   "2010-11-30",   "2010-12-31",   "2011-01-31",
"2011-02-28",   "2011-03-31",   "2011-04-29",   "2011-05-31",   "2011-06-30",   "2011-07-29",   "2011-08-31",   "2011-09-30",   "2011-10-31",
"2011-11-30",   "2011-12-30",   "2012-01-31",   "2012-02-29",   "2012-03-30",   "2012-04-30",   "2012-05-31",   "2012-06-29",   "2012-07-31",
"2012-08-31",   "2012-09-28",   "2012-10-31",   "2012-11-30",   "2012-12-31",   "2013-01-31",   "2013-02-28",   "2013-03-29",   "2013-04-30",
"2013-05-31",   "2013-06-28",   "2013-07-31",   "2013-08-30",   "2013-09-30",   "2013-10-31",   "2013-11-29",   "2013-12-31",   "2014-01-31",
"2014-02-28",   "2014-03-31",   "2014-04-30",   "2014-05-30",   "2014-06-30",   "2014-07-31",   "2014-08-29",   "2014-09-30",   "2014-10-31",
"2014-11-28",   "2014-12-31",   "2015-01-30",   "2015-02-27",   "2015-03-31",   "2015-04-30",   "2015-05-29",   "2015-06-30",   "2015-07-31",
"2015-08-31",   "2015-09-30",   "2015-10-30",   "2015-11-30",   "2015-12-31")
Edgar Santos
  • 3,426
  • 2
  • 17
  • 29