Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Using a reproducible example</p> <pre><code>set.seed(1) x &lt;- as.POSIXct(sort(sample(100000, 100)), origin="1960-01-01") y &lt;- rpois(100, 5) plot(x, y, type = "l", xlab = "Time") </code></pre> <p>we can make use of the <code>axis.POSIXct()</code> function (one could also use the <code>Axis()</code> S3 generic as well) to add a custom axis to the plot. The main point here is that you, the user, are in full control of where ticks are draw and how they are labelled, you just need to work a little harder if the defaults don;t work for you.</p> <p>First we plot the data but suppress drawing of the x-axis:</p> <pre><code>plot(x, y, type = "l", xlab = "Time", xaxt = "n") </code></pre> <p>Next I will add a major tick mark at the start of each hour. For this I create a sequence of datetimes that goes</p> <ol> <li><strong>from</strong> the rounded hour of the first observation in the series,</li> <li><strong>to</strong> the end of the last hour in which an observation was made (using <code>ceiling()</code> to move us on to the next hour),</li> <li><strong>incrementing</strong> the sequence in 1 hour (<code>by = "1 hour"</code>) units.</li> </ol> <p>This sequence is supplied to the the <code>at</code> argument of <code>axis.POSIXct()</code>. The rest should be easy to follow if not read <code>?axis.POSIXct</code> and <code>?par</code></p> <pre><code>## add axis tick at each hour: axis.POSIXct(side = 1, x = x, at = seq(from = round(x[1], "hours"), to = x[1] + ceiling(difftime(tail(x, 1), head(x, 1), units = "hours")), by = "1 hour"), las = 2) </code></pre> <p>The resulting figure looks like this:</p> <p><img src="https://i.stack.imgur.com/eunaZ.png" alt="enter image description here"></p> <p>To show finer control, I now add minor tick marks at each half-hour location, but suppress the annotation of those ticks (via the <code>labels</code> argument) and also make the minor ticks shorter (via graphics parameter <code>tcl</code>). Notice how the <code>seq()</code> method's <code>by</code> argument can take a numeric amount of the stated interval</p> <pre><code>## add minor ticks at 30 min intervals to above plot axis.POSIXct(side = 1, x = x, at = seq(from = round(x[1], "hours"), to = x[1] + ceiling(difftime(tail(x, 1), head(x, 1), units = "hours")), by = "30 mins"), las = 2, tcl = -0.2, labels = FALSE) </code></pre> <p>The plot now looks like this:</p> <p><img src="https://i.stack.imgur.com/8Twh2.png" alt="enter image description here"></p> <p>You can add your own labels rather than the ones that the <code>axis.POSIXct</code> function comes up with. If you want to do this, then we should assign the output from <code>seq()</code> to an object that we can then use the <code>format()</code> function on. For example:</p> <pre><code>plot(x, y, type = "l", xlab = "Time", xaxt = "n") tseq &lt;- seq(from = round(x[1], "hours"), to = x[1] + ceiling(difftime(tail(x, 1), head(x, 1), units = "hours")), by = "1 hour") axis.POSIXct(side = 1, x = x, at = tseq, labels = format(tseq, format = "%H:%M"), las = 2) </code></pre> <p>The resulting plot is shown below:</p> <p><img src="https://i.stack.imgur.com/6EDtH.png" alt="enter image description here"></p> <p><code>format()</code> returns a character string of the formatted datetime. You can <code>paste()</code> on anything else you want or look at the other placeholders that can be used to format datetime objects in <code>?strftime</code></p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload