title: How to tailor the x-axis of a ggplot to include dates
body: <p>How to insert dates in the x axis of a ggplot as this:</p>
  <p><a href="https://i.stack.imgur.com/uzJ68.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/uzJ68.png" alt="enter image description here" /></a></p>
  <p>Thanks to the help from SO, I managed to have a tailored x-axis in the basic plotting function in R:</p>
  <p><a href="https://i.stack.imgur.com/Aa83W.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Aa83W.png" alt="enter image description here" /></a></p>
  <p>However, the key two lines of code used to generate the second plot:</p>
  <pre><code>at1 &lt;- seq(min(ccw$date), max(ccw$date), by=1+.02*length(ccw$date))
  axis.Date(1, at=at1, format=&quot;%b %d&quot;, las=2, cex.axis=0.7)
  </code></pre>
  <p>don't seem easy to adapt to ggplot, where instead I am stuck with just the number of the day. By the way, if you are about to generate the plot with a fix to address my question and can figure out a way to make the confidence bands broader in general, I would appreciate it.</p>
  <p>Here is the entire code to generate both plots as are:</p>
  <pre><code>install.packages('RCurl')
  install.packages('zoo')
  suppressPackageStartupMessages({
      require(repr) # Enables resizing of the plots.
      require(RCurl)
      require(foreign)
      require(tidyverse) # To tip the df from long row of dates to cols (pivot_longer())
      require(zoo) # Rolling average
      require(RColorBrewer)
  })
  
  x = getURL(&quot;https://raw.githubusercontent.com/CSSEGISandData/COVID-19/master/csse_covid_19_data/csse_covid_19_time_series/time_series_covid19_confirmed_global.csv&quot;)
  corona &lt;- read.csv(textConnection(x))
  
  corona = (read_csv(x)
            %&gt;% pivot_longer(cols = -c(`Province/State`, `Country/Region`, Lat, Long),
                             names_to = &quot;date&quot;,
                             values_to = &quot;cases&quot;)
            %&gt;% select(`Province/State`,`Country/Region`, date, cases)
            %&gt;% mutate(date=as.Date(date,format=&quot;%m/%d/%y&quot;))
            %&gt;% drop_na(cases)
            %&gt;% rename(country=&quot;Country/Region&quot;, provinces=&quot;Province/State&quot;)
  )
   
  cc &lt;- (corona
         %&gt;% filter(country %in% c(&quot;Italy&quot;, &quot;Spain&quot;,&quot;US&quot;, &quot;Norway&quot;, &quot;Denmark&quot;, &quot;Sweden&quot;,&quot;Korea, South&quot;, &quot;Brazil&quot;,&quot;India&quot;, &quot;United Kingdom&quot;, &quot;Mexico&quot;))
  )
   
  ccw &lt;- (cc
          %&gt;% pivot_wider(names_from=&quot;country&quot;,values_from=&quot;cases&quot;)
          %&gt;% filter(Italy&gt;5)
  )
  
  x = getURL(&quot;https://raw.githubusercontent.com/CSSEGISandData/COVID-19/master/csse_covid_19_data/csse_covid_19_time_series/time_series_covid19_confirmed_global.csv&quot;)
  corona &lt;- read.csv(textConnection(x))
  
  corona = (read_csv(x)
            %&gt;% pivot_longer(cols = -c(`Province/State`, `Country/Region`, Lat, Long),
                             names_to = &quot;date&quot;,
                             values_to = &quot;cases&quot;)
            %&gt;% select(`Province/State`,`Country/Region`, date, cases)
            %&gt;% mutate(date=as.Date(date,format=&quot;%m/%d/%y&quot;))
            %&gt;% drop_na(cases)
            %&gt;% rename(country=&quot;Country/Region&quot;, provinces=&quot;Province/State&quot;)
  )
   
  cc &lt;- (corona
         %&gt;% filter(country %in% c(&quot;Italy&quot;, &quot;Spain&quot;,&quot;US&quot;, &quot;Norway&quot;, &quot;Denmark&quot;, &quot;Sweden&quot;,&quot;Korea, South&quot;, &quot;Brazil&quot;,&quot;India&quot;, &quot;United Kingdom&quot;, &quot;Mexico&quot;))
  )
   
  ccw &lt;- (cc
          %&gt;% pivot_wider(names_from=&quot;country&quot;,values_from=&quot;cases&quot;)
          %&gt;% filter(Italy&gt;5)
  )
  
  
  options(repr.plot.width=13, repr.plot.height=8)
  
  ccw$first.der &lt;- c(NA, diff(ccw$US))  ## better add an NA and integrate in data frame
  ccw$day &lt;- seq_along(ccw$date)
  
  first.der &lt;- diff(ccw$US, lag = 1, differences = 1)
  
  
  k=7
  MAV &lt;- rollmean(first.der,k)
  
  fit8 &lt;- lm(first.der ~ poly(day, 8, raw=TRUE), ccw[-1, ]) # OCTIC!
  
  par(mar=c(5,5,3,2))
  with(ccw, plot(day, first.der, 
        main=&quot;Daily COVID-19 Cases in the US&quot;, cex.main=2,
        axes=FALSE,
        xlab='', 
        ylab='',
        las = 2,
        col=rgb(0.4,0.4,0.8,0.6), pch = 16, cex = .6))
  
  abline(h=0)
  abline(h=ccw$first.der[length(ccw$day)], col='red', lty=2)
  
  tck &lt;- seq(min(ccw$day), max(ccw$day), by=10)
  axis(1, tck, labels=FALSE)
  
  at2 &lt;- seq(min(first.der),max(first.der)+150000, 0.10 * max(first.der))
  axis(side=2, at2, 
       las=2, cex.axis=1, labels = formatC(at2, big.mark = &quot;,&quot;, format = &quot;d&quot;))
  
  mtext(strftime(ccw$date[tck], &quot;%b %d&quot;), 1, 1, at=tck, las=2)
  
  lines(fit8$fitted.values, col=alpha('blue',.8), lwd=2)
  points(ccw$day, ccw$first.der, main=&quot;US covid-19&quot;, pch=16, col=rgb(0.8,0.2,0.1,0.6))
  points(tail(ccw$day,1), last(ccw$first.der), main=&quot;US covid-19&quot;, pch=19, cex=1.2, col='red')
  
  
  ggplot(ccw, aes(x = day, y = first.der)) + geom_point() +
    geom_vline(xintercept = 0, color = &quot;red&quot;)  + 
    geom_point(mapping = aes(x = ccw$day, y = ccw$first.der), color = &quot;blue&quot;) +
    geom_smooth(method = &quot;lm&quot;, formula = y ~ poly(x, 8))
  </code></pre>