Debian Codegraph @

R/graph.R

raw
1 #' Minor tick marks
2 #'
3 #' Adds minor tick marks to an existing \code{base} plot.
4 #' @param nx intervals to split on x axis
5 #' @param ny intervals to split on x axis
6 #' @param grid show grid lines at minor ticks?
7 #' @keywords internal
8 #' @references This function is a forked version of \code{Hmisc::minor.tick}
9 #' @importFrom graphics par axis abline
10 add.minor.ticks <- function (nx = 4, ny = 4, grid = TRUE) {
11
12 ax <- function(w, n, grid) {
13 range <- par('usr')[ifelse(w == 'x', 1:2, 3:4)]
14 tick.pos <- ifelse(w == 'x', par('xaxp'), par('yaxp'))
15 distance.between.minor <- (tick.pos[2] - tick.pos[1]) / tick.pos[3] / n
16 possible.minors <- tick.pos[1] - (0:100) * distance.between.minor
17 low.minor <- min(possible.minors[possible.minors >= range[1]])
18 if (is.na(low.minor)) {
19 low.minor <- tick.pos[1]
20 }
21 possible.minors <- tick.pos[2] + (0:100) * distance.between.minor
22 hi.minor <- max(possible.minors[possible.minors <= range[2]])
23 if (is.na(hi.minor)) {
24 hi.minor <- tick.pos[2]
25 }
26 ticks.at <- seq(low.minor, hi.minor, by = distance.between.minor)
27 axis(if (w == 'x')
28 1
29 else 2, ticks.at,
30 labels = FALSE, tcl = par('tcl'))
31 if (w == 'x') {
32 abline(v = ticks.at,
33 col = panderOptions('graph.grid.color'),
34 lwd = 0.2,
35 lty = panderOptions('graph.grid.lty'))
36 } else {
37 abline(h = ticks.at,
38 col = panderOptions('graph.grid.color'),
39 lwd = 0.2,
40 lty = panderOptions('graph.grid.lty'))
41 }
42 }
43
44 if (nx > 1) {
45 ax('x', nx, grid = grid)
46 }
47 if (ny > 1) {
48 ax('y', ny, grid = grid)
49 }
50
51 invisible()
52
53 }
54
55
56 #' Add grids
57 #'
58 #' Exactly the same fn as see \code{latticeExtra::axis.grid} but using \code{grid} colors instead.
59 #' @param side see \code{latticeExtra::axis.grid}
60 #' @param ... see \code{latticeExtra::axis.grid}
61 #' @param ticks see \code{latticeExtra::axis.grid}
62 #' @param scales see \code{latticeExtra::axis.grid}
63 #' @param components see \code{latticeExtra::axis.grid}
64 #' @param line.col see \code{latticeExtra::axis.grid}
65 #' @keywords internal
66 #' @references This function is a forked version of \code{latticeExtra::axis.grid}
67 add.lattice.grid <- function (side = c('top', 'bottom', 'left', 'right'), ..., ticks = c('default', 'yes', 'no'), scales, components, line.col) { #nolint
68
69 side <- match.arg(side)
70 ticks <- match.arg(ticks)
71 scales.tck <- switch(side, left = , bottom = scales$tck[1], right = , top = scales$tck[2]) #nolint
72 comps.major <- components
73 mycomps <- components[[side]]
74
75 if (is.list(mycomps)) {
76 lab <- as.character(mycomps$labels$labels)
77 if (any(lab != '')) {
78 tck <- mycomps$ticks$tck
79 if (any(tck * scales.tck != 0)) {
80 tck <- rep(tck, length = length(lab))
81 comps.major[[side]]$ticks$tck <- ifelse(lab == '', NA, tck)
82 }
83 }
84 } else {
85 ticks <- 'no'
86 }
87
88 lattice::axis.default(side,
89 scales = scales,
90 ticks = ticks,
91 components = comps.major,
92 ...,
93 line.col = panderOptions('graph.grid.color'))
94
95 if (side %in% c('top', 'left')) {
96 return()
97 }
98 if (scales$draw == FALSE) {
99 return()
100 }
101 ref.line <- lattice::trellis.par.get('reference.line')
102
103 if (side == 'bottom') {
104 tck <- abs(mycomps$ticks$tck)
105 lattice::panel.refline(v = mycomps$ticks$at,
106 lwd = ref.line$lwd * tck,
107 alpha = ref.line$alpha * tck / max(tck, na.rm = TRUE))
108 }
109 if (side == 'right') {
110 if (!is.list(mycomps)) {
111 mycomps <- components[['left']]
112 }
113 tck <- abs(mycomps$ticks$tck)
114 lattice::panel.refline(h = mycomps$ticks$at,
115 lwd = ref.line$lwd * tck,
116 alpha = ref.line$alpha * tck / max(tck, na.rm = TRUE))
117 }
118
119 }
120
121 #' Add subticks
122 #'
123 #' See \code{latticeExtra::xscale.components.subticks}.
124 #' @param lim see \code{latticeExtra::xscale.components.subticks}
125 #' @param ... see \code{latticeExtra::xscale.components.subticks}
126 #' @param n see \code{latticeExtra::xscale.components.subticks}
127 #' @keywords internal
128 #' @aliases add.lattice.ysubticks
129 #' @references This is a simplified verwion of \code{latticeExtra::xscale.components.subticks}.
130 add.lattice.xsubticks <- function (lim, ..., n = 2) {
131
132 ans <- lattice::xscale.components.default(lim = lim, ..., n = n)
133 ticks <- ans$bottom$ticks$at
134 ticks2 <- head(ticks, -1) + diff(head(ticks, 2)) / 2
135 ans$bottom$ticks$at <- c(ticks, ticks2)
136 ans$bottom$ticks$tck <- c(rep(1, length(ticks)), rep(0.5, length(ticks2)))
137 ans$bottom$labels$at <- ans$bottom$ticks$at
138 ans$bottom$labels$labels <- c(ans$bottom$labels$labels, rep(' ', length(ticks2)))
139 ans$bottom$labels$check.overlap <- FALSE
140 ans
141
142 }
143
144 #' @keywords internal
145 add.lattice.ysubticks <- function (lim, ..., n = 2) {
146
147 ans <- lattice::yscale.components.default(lim = lim, ..., n = n)
148 ticks <- ans$left$ticks$at
149 ticks2 <- head(ticks, -1) + diff(head(ticks, 2)) / 2
150 ans$left$ticks$at <- c(ticks, ticks2)
151 ans$left$ticks$tck <- c(rep(1, length(ticks)), rep(0.5, length(ticks2)))
152 ans$left$labels$at <- ans$left$ticks$at
153 ans$left$labels$labels <- c(ans$left$labels$labels, rep(' ', length(ticks2)))
154 ans$left$labels$check.overlap <- FALSE
155 ans
156
157 }