Search code examples
rplotx-axis

Changing extent of x axis (remove a year) in R


I am trying to remove a year from a time series graph I have. Despite having no data for that year, when I plot it I have a gap for the year from which I don't have any data.

A representation of the data I want to plot is in the attached code. Basically I want the plot NOT to display that gap for the year 2006. I just want the scale to be all equal and from the year 2005 we just go straight to 2007.

Does anyone have any piece of code so I can overcome this?

# Data.frame containing data to plot
model_bind_mean
   year    Predicted           SE         lower upper year
1  2000 2.489231e-08 5.396462e-06 8.109053e-139     1 2000
2  2001 9.163199e-09 1.989060e-06 1.901918e-129     1 2001
3  2002 3.714457e-08 6.725223e-06 2.632171e-133     1 2002
4  2003 3.815457e-08 7.461874e-06 5.009737e-132     1 2003
5  2004 1.976398e-08 5.591814e-06 1.652577e-133     1 2004
6  2005 6.348324e-08 1.260939e-05 1.076752e-130     1 2005
7  2007 6.567202e-09 1.439420e-06 1.947562e-129     1 2007
8  2008 8.087554e-09 2.021963e-06 6.891736e-134     1 2008
9  2009 8.474279e-09 2.267154e-06 3.261077e-142     1 2009
10 2010 1.487757e-08 2.722827e-06 7.227378e-129     1 2010
11 2011 1.031034e-08 1.941836e-06 4.652030e-133     1 2011
12 2012 9.974196e-09 2.073215e-06 1.279174e-131     1 2012
13 2013 2.616392e-07 5.847537e-05 1.255000e-128     1 2013

legend_year <- c(2000,2001,2002,2003,2004,2005,2007,2008,2009,2010,2011,2012,2013)

# Plot
plot(legend_year,model_bind_mean $Predicted, ylim = c(0,1),
     las = 1,pch = 15,font.lab = 2, col='gold4',
     xlab = 'Year', ylab = 'Y axis',
     bty = 'l',xaxt = 'n',yaxt = 'n',
     main="Title")

lines(legend_year,model_bind_mean $Predicted,lwd = 2, col = 'gold4')

arrows(x0=legend_year, y0=model_bind_mean $Predicted-model_bind_mean $SE,
       y1=model_bind_mean $Predicted+model_bind_mean $SE, code = 3, angle = 90, lty = 1,
       length = 0.03,col = rgb(0,0,0,0.3))

axis(1, at = legend_year ,
     labels = T,tck = 0.02,
     cex.axis = 0.75)

axis(2, at = seq(0,1,0.1),
     labels = seq(0,1,0.1),las = 1,tck = 0.02,
     cex.axis = 0.75)

picture


Solution

  • When using actual year values on the plot they are placed numerically and so the year 2005 is placed on the x-axis position of 2005 while year 2007 is placed on the x-axis position of 2007. To not have a gap you should place the values on your own defined positions, not the positions defined by year. The code below places years at positions 1, 2 ,3 etc., and then adds ths the labels for the years on the axis.

    # Plot
    plot(1:nrow(model_bind_mean), model_bind_mean$Predicted, ylim = c(0,1),
         las = 1,pch = 15,font.lab = 2, col='gold4',
         xlab = 'Year', ylab = 'Y axis',
         bty = 'l',xaxt = 'n',yaxt = 'n',
         main="Title")
    
    lines(1:nrow(model_bind_mean), model_bind_mean$Predicted,lwd = 2, col = 'gold4')
    
    arrows(x0=1:nrow(model_bind_mean), y0=model_bind_mean$Predicted-model_bind_mean$SE,
           y1=model_bind_mean$Predicted+model_bind_mean$SE, code = 3, angle = 90, lty = 1,
           length = 0.03,col = rgb(0,0,0,0.3))
    
    axis(1, at = 1:nrow(model_bind_mean), labels = legend_year,tck = 0.02, cex.axis = 0.75)
    axis(2, at = seq(0,1,0.1), labels = seq(0,1,0.1),las = 1,tck = 0.02, cex.axis = 0.75)
    

    picture