Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p><code>axis.POSIXct()</code> already works quite hard to guess suitable pretty values for the axis, so I would start by hacking that. At the moment, it relies internally on using <code>pretty()</code> applied to the some function of the datetimes. It uses the defaults for <code>pretty()</code> so you could hack the function to add an <code>n</code> or <code>min.n</code> argument which would increase the number of pretty marks selected.</p> <p>Copy <code>axis.POSIXct()</code> to your own function/file (give it a new name). Add a <code>n</code> or <code>min.n</code> argument to the definition, probably with larger values as defaults than those used by the <code>pretty()</code> function. And pass that to each of the <code>pretty()</code> calls that is made.</p> <p>Try it out. If it works reasonably well, then you can do <code>fixInNamespace(axis.POSIXct)</code> to make the same changes to the actual function so it gets used on all plots for which it is called.</p> <p>P.S. Here is a possible hack</p> <pre><code>function (side, x, at, format, labels = TRUE, n = 5, ...) { mat &lt;- missing(at) || is.null(at) if (!mat) x &lt;- as.POSIXct(at) else x &lt;- as.POSIXct(x) range &lt;- par("usr")[if (side%%2) 1L:2L else 3L:4L] d &lt;- range[2L] - range[1L] z &lt;- c(range, x[is.finite(x)]) attr(z, "tzone") &lt;- attr(x, "tzone") if (d &lt; 1.1 ) { sc &lt;- 0.001 if (missing(format)) format &lt;- "%H:%M:%OS6" } else if (d &lt; 1.1 * 30) { sc &lt;- 1 if (missing(format)) format &lt;- "%H:%M:%OS3" } else if (d &lt; 1.1 * 60) { sc &lt;- 1 if (missing(format)) format &lt;- "%H:%M:%S" } else if (d &lt; 1.1 * 30 * 60) { sc &lt;- 60 if (missing(format)) format &lt;- "%H:%M:%S" } else if (d &lt; 1.1 * 60 * 60) { sc &lt;- 60 if (missing(format)) format &lt;- "%H:%M" } else if (d &lt; 1.3 * 60 * 60 * 24) { sc &lt;- 60 * 60 if (missing(format)) format &lt;- "%H:%M" } else if (d &lt; 2 * 60 * 60 * 24) { sc &lt;- 60 * 60 if (missing(format)) format &lt;- "%a %H:%M" } else if (d &lt; 7 * 60 * 60 * 24) { sc &lt;- 60 * 60 * 24 if (missing(format)) format &lt;- "%a" } else { sc &lt;- 60 * 60 * 24 } if (d &lt; 60 * 60 * 24 * 50) { zz &lt;- pretty(z/sc,n=n) z &lt;- zz * sc z &lt;- .POSIXct(z, attr(x, "tzone")) if (sc == 60 * 60 * 24) z &lt;- as.POSIXct(round(z, "days")) if (missing(format)) format &lt;- "%b %d" } else if (d &lt; 1.1 * 60 * 60 * 24 * 365) { z &lt;- .POSIXct(z, attr(x, "tzone")) zz &lt;- as.POSIXlt(z) zz$mday &lt;- zz$wday &lt;- zz$yday &lt;- 1 zz$isdst &lt;- -1 zz$hour &lt;- zz$min &lt;- zz$sec &lt;- 0 zz$mon &lt;- pretty(zz$mon,n=n) m &lt;- length(zz$mon) M &lt;- 2 * m m &lt;- rep.int(zz$year[1L], m) zz$year &lt;- c(m, m + 1) zz &lt;- lapply(zz, function(x) rep(x, length.out = M)) zz &lt;- .POSIXlt(zz, attr(x, "tzone")) z &lt;- as.POSIXct(zz) if (missing(format)) format &lt;- "%b" } else { z &lt;- .POSIXct(z, attr(x, "tzone")) zz &lt;- as.POSIXlt(z) zz$mday &lt;- zz$wday &lt;- zz$yday &lt;- 1 zz$isdst &lt;- -1 zz$mon &lt;- zz$hour &lt;- zz$min &lt;- zz$sec &lt;- 0 zz$year &lt;- pretty(zz$year,n=n) M &lt;- length(zz$year) zz &lt;- lapply(zz, function(x) rep(x, length.out = M)) z &lt;- as.POSIXct(.POSIXlt(zz)) if (missing(format)) format &lt;- "%Y" } if (!mat) z &lt;- x[is.finite(x)] keep &lt;- z &gt;= range[1L] &amp; z &lt;= range[2L] z &lt;- z[keep] if (!is.logical(labels)) labels &lt;- labels[keep] else if (identical(labels, TRUE)) labels &lt;- format(z, format = format) else if (identical(labels, FALSE)) labels &lt;- rep("", length(z)) axis(side, at = z, labels = labels, ...) } </code></pre> <p>Differences to the original function can be seen <a href="https://www.diffchecker.com/89tc49wj" rel="nofollow">here</a></p>
 

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