title: R barplots: specify intervals of date-based x-axis
body: <p>I've been producing different sets of charts, all in R base. I have a problem though with barplots. I've formatted the x-axis to show the dates by year, however, many years show up several times. I would like each year to only show up once.</p>
  
  <p>Here's my example code:</p>
  
  <pre><code>library(quantmod)
  start &lt;- as.Date("01/01/2010", "%d/%m/%Y")
  
  #Download FRED data
  tickers &lt;- c("WTISPLC", "DCOILBRENTEU")
  fred &lt;- lapply(tickers, function(sym) {na.omit(getSymbols(sym, src="FRED", auto.assign=FALSE, return.class = "zoo"))})
  df &lt;- do.call(merge, fred)
  
  #Subset for start date
  df &lt;- subset(df, index(df)&gt;=start)
  
  #Create bar plot
  par(mar = c(5,5,5,5))
  
  barplot(df[,2], names.arg=format(index(df), "%Y"), ann=FALSE, bty="n", tck=-0, col=1:1,  border=NA, space=0); title(main="Example chart", ylab="y-axis")
  </code></pre>
  
  <p>This example should be reproducible and show clearly what I mean. Now, I've been researching how to add a separate x-axis and how to define that axis. So, I've tried to add the following code:</p>
  
  <pre><code>#Plot bars but without x-axis
  barplot(df[,2], names.arg=format(index(df), "%Y"), ann=FALSE, bty="n", tck=-0, xaxt="n", col=1:1,  border=NA, space=0); title(main="Example chart", ylab="y-axis")
  
  # Set x-axis parameters
  x_min &lt;- min(index(df))
  x_max &lt;- max(index(df))
  xf="%Y"
  
  #Add x-axis
  axis.Date(1, at=seq(as.Date(x_min), x_max, "years"), format=xf, las=1, tck=-0)
  </code></pre>
  
  <p>This does not give me an error message, but it also does absolutely nothing in terms of drawing an x-axis.</p>
  
  <p>Please do not provide a solution for ggplot. Even though I like ggplot, these barplots are part of a bigger project for me, all using R base and I would not like to introduce ggplot into this project now. </p>
  
  <p><a href="https://i.stack.imgur.com/kpNR1.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/kpNR1.png" alt="enter image description here"></a></p>
  
  <p>Thanks!</p>