Search code examples
rplotaxis-labels

Missing x axis labels in R plot


I have three data frames, not very exciting data I know, but this is not the problem I am trying to address.

  > Ascidcv
             Date   Average       SE
    3  2014-09-01 37.250000 6.326673
    15 2014-10-02  6.285714 2.738613
    > Ascidc1
              Date Average SE
    3   2014-10-15       0  0
    34  2014-11-12       0  0
    62  2014-12-11       0  0
    88  2015-02-11       0  0
    119 2015-03-09       0  0
    > Ascidc2
              Date Average SE
    18  2014-10-15       0  0
    48  2014-11-12       0  0
    75  2014-12-11       0  0
    103 2015-02-11       0  0
    135 2015-03-09       0  0

I use these data frames to produce a plot:

plot(Ascidcv$Date, Ascidcv$Average, type='p', pch=4, col="red", xlab='Date', ylab='', main=expression(italic('Ascidiella sp.')), xlim=c(as.Date("2014-09-01"), as.Date("2015-03-09")), ylim=c(0,120))
points(Ascidc1$Date, Ascidc1$Average, type='p', pch=19, xlab='Date', main=expression(italic('Ascidiella sp.')), xlim=c(as.Date("2014-09-01"),as.Date("2014-12-11")), ylim=c(0,100))
points(Ascidc2$Date, Ascidc2$Average, type='p', pch=2, col="blue", xlab='Date', ylab='Average num ind.', main=expression(italic('Bugula sp.')), xlim=c(as.Date("2014-09-01"),as.Date("2014-12-11")), ylim=c(0,100))
mtext("Average % cover",side=2,line=3)

For some reason only the months September, October and March are being plotted on the x axis, with November- February failing to appear. I am sure this is quite a simple fix but I can't seem to figure it out. Any assistance would be greatly appreciated! Thanks!


Solution

  • You can do your plot in two steps:

    first, your plot without axis

    plot(Ascidcv$Date, Ascidcv$Average, type='p', pch=4, col="red", xlab='Date',
         ylab='', main=expression(italic('Ascidiella sp.')), xlim=c(as.Date("2014-09-01"), 
         as.Date("2015-03-09")), ylim=c(0,120),xaxt="n")
    

    then, add the axis with ticks at the first day of each month from September 2014 to march 2015 with axis.Date and a vector for the respective dates

    axis.Date(1, at=as.Date(c(paste0("2014-",c("09",10:12),"-01"),
              paste0("2015-0",1:3,"-01"))))
    

    finally, add the other points and text

    points(Ascidc1$Date, Ascidc1$Average, type='p', pch=19, xlab='Date', 
           main=expression(italic('Ascidiella sp.')), xlim=c(as.Date("2014-09-01"),
           as.Date("2014-12-11")), ylim=c(0,100))
    
    points(Ascidc2$Date, Ascidc2$Average, type='p', pch=2, col="blue", xlab='Date', 
           ylab='Average num ind.', main=expression(italic('Bugula sp.')), 
           xlim=c(as.Date("2014-09-01"),as.Date("2014-12-11")), ylim=c(0,100))
    
    mtext("Average % cover",side=2,line=3)
    

    enter image description here