R x axis date label only one value

I am creating a plot in R with dates like xaxis. My frame has dates, no problem. I use a custom date range - one that cuts off some of the earliest data using a fixed start and slightly expanding the latest data using the end defined by some other code. The range is ~ 47 days. It all works fine.

My problem is that the xaxis label only includes one “Feb” label, but I would like to include at least three labels, if not 5.

starttime <- strptime("20110110", "%Y%m%d")
endtime <- strptime("20110226 1202", "%Y%m%d %H%M") #This is actually determined programmatically, but that not important
xrange <- c(starttime, endtime)
yrange <- c(0, 100)
par(mar=par()$mar+c(0,0,0,7),bty="l")
plot(xrange, yrange, type="n", xlab="Submission Time", ylab="Best Score", main="Top Scores for each team over time")
#More code to loop and add a bunch of lines(), but it not really relevant

The resulting graph is as follows: enter image description here

I just need the best shortcuts. I am not too concerned about what they are, but something with a Month + Day and at least 3 of them.

+3
source share
3 answers

. () .

 starttime <- strptime("20110110", "%Y%m%d")
 endtime <- strptime("20110226 1202", "%Y%m%d %H%M")
 #This is actually determined programmatically, but that not important
 xrange <- c(starttime, endtime)
 yrange <- c(0, 100)
 par(mar=par()$mar+c(0,0,0,7),bty="l")

 #I added xaxt="n" to supress the plotting of the x-axis
 plot(xrange, yrange, type="n", xaxt="n", xlab="Submission Time", ylab="Best Score", main="Top Scores for each team over time")

 #I added the following two lines to plot the x-axis with a label every 7 days
 atx <- seq(starttime, endtime, by=7*24*60*60)
 axis(1, at=atx, labels=format(atx, "%b\n%d"), padj=0.5)

 #More code to loop and add a bunch of lines(), but it not really relevant

enter image description here

+3

, axis.Date(), S3, , . , R , . , ?axis.Date:

random.dates <- as.Date("2001/1/1") + 70*sort(stats::runif(100))
plot(random.dates, 1:100)
# or for a better axis labelling
plot(random.dates, 1:100, xaxt="n")
axis.Date(1, at=seq(as.Date("2001/1/1"), max(random.dates)+6, "weeks"))
axis.Date(1, at=seq(as.Date("2001/1/1"), max(random.dates)+6, "days"),
          labels = FALSE, tcl = -0.2)

: enter image description here

axis.Date(), S3, Axis(dates.vec, ....), dates.vec - x. at ..

+3

, x plot(), axis() :

axis(1,at=axis.pos[axis.ind],labels=axis.txt[axis.ind])

axis.ind, x . strftime() , '%d %b' ,

R> strftime(Sys.Date(), "%d %b")
[1] "26 Feb"
+2

Source: https://habr.com/ru/post/1794953/


All Articles