1

Here is the code, datereal has 517 date values from 2020-02-01 to 2020-06-30. Plot is produced as per image. Ideally I would like a tick mark for each month and alternating months i.e. Feb 20, Apr20 ....May 21, Jul 21.

dev.new(width=12, height=7.5) 
# Adjust the margin of plot (bottom,left,top,right)
par(mar=c(5, 5, 3, 7))
plot(datereal, base9, type = "l", lwd=2, col = "blue", xaxt = "n", xlab="Month",ylab="Daily new cases ")
#plot(datereal, base9, type = "l", lwd=2, col = "blue", xaxt = "n", xlab="              2020                Month                     2021",
#ylab="Daily new cases ")
axis.Date(1, datereal, 
         at = seq(as.Date("2020-02-01"), as.Date("2021-06-01"), by = "1 month"))

Rplot showing x axis with years not months

jay.sf
  • 60,139
  • 8
  • 53
  • 110
  • 1
    Try inserting `, format=strftime(dte, "%b")` after the `at=` argument. See `?strftime` for more options. You need to specify what to print on the axis in addition to where to print. – dcarlson Jan 05 '21 at 04:06

1 Answers1

0

To get the label positions on the first of each month, we use substr.

One way is to use two-month intervals as you have indicated. For this we may generate sequences 1, 2, 3, ... along the dates, using seq_along, and look where they are zero in modulo 2 to get the subset IDs ids. To get the month-year format we use format= (see ?strftime for options).

op <- par(mar=c(5, 5, 3, 7))  ## set par
plot(datereal, base9, type="l", lwd=2, col="blue", xaxt="n", xlab="Month",
     ylab="Daily new cases")
which(substr(datereal, 9, 10) == "01")
labs <- datereal[substr(datereal, 9, 10) == "01"]
ids <- seq_along(labs)[(seq_along(labs) + 1) %% 2 == 0]
axis.Date(1, at=labs[ids], format="%b %y", cex.axis=.9)
par(op)  ## reset par

enter image description here

Alternatively, you can rotate the text ninety degrees (but the first variant is nicer). This is possible using mtext. We still need axis to get the tick marks.

op <- par(mar=c(5, 5, 3, 7))
plot(datereal, base9, type="l", lwd=2, col="blue", xaxt="n", xlab="Month",
     ylab="Daily new cases")
axis(1, at=labs, labels=FALSE)
mtext(strftime(labs, format="%b %y"), side=1, line=.75, at=labs, las=2, cex=.8)
par(op)

enter image description here

Note, you appear to be working with the Plot window and saving images by mouse click. If you rescale the window, the labels may disappear. A better choice is to use, for example, the png device as explained in this answer


Data:

base9 <- 5e5*dnorm(seq(-2, 14, length=516), 10, 4)
datereal <- seq(as.Date("2020-02-01"), as.Date("2021-06-30"), "day")
jay.sf
  • 60,139
  • 8
  • 53
  • 110