Skip to content

Commit

Permalink
Add argument for the number of y-axis grid lines
Browse files Browse the repository at this point in the history
This allows the user to control how many y-axis grid lines are drawn,
instead of being restricted to the default of 5. Thanks to Fredrik
Wartenberg for the feature request and patch!

Fixes #374.
  • Loading branch information
joshuaulrich committed Oct 7, 2022
1 parent 3b2c1fa commit a9f1959
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 2 deletions.
5 changes: 4 additions & 1 deletion R/plot.R
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ plot.xts <- function(x,
yaxis.same=TRUE,
yaxis.left=TRUE,
yaxis.right=TRUE,
yaxis.ticks=5,
major.ticks="auto",
minor.ticks=NULL,
grid.ticks.on="auto",
Expand Down Expand Up @@ -197,6 +198,7 @@ plot.xts <- function(x,
yaxis.same=yaxis.same,
yaxis.left=yaxis.left,
yaxis.right=yaxis.right,
yaxis.ticks=yaxis.ticks,
major.ticks=major.ticks,
minor.ticks=minor.ticks,
grid.ticks.on=grid.ticks.on,
Expand Down Expand Up @@ -257,6 +259,7 @@ plot.xts <- function(x,
cs$Env$theme$las <- if (hasArg("las")) eval.parent(plot.call$las) else 0
cs$Env$theme$cex.axis <- if (hasArg("cex.axis")) eval.parent(plot.call$cex.axis) else 0.9
cs$Env$format.labels <- format.labels
cs$Env$yaxis.ticks <- yaxis.ticks
cs$Env$major.ticks <- if (isTRUE(major.ticks)) "auto" else major.ticks
cs$Env$minor.ticks <- if (isTRUE(minor.ticks)) "auto" else minor.ticks
cs$Env$grid.ticks.on <- if (isTRUE(grid.ticks.on)) "auto" else grid.ticks.on
Expand Down Expand Up @@ -1376,7 +1379,7 @@ new.replot_xts <- function(frame=1,asp=1,xlim=c(1,10),ylim=list(structure(c(1,10

# function to plot the y-axis grid lines
replot_env$Env$y_grid_lines <- function(ylim) {
p <- pretty(ylim, 5)
p <- pretty(ylim, Env$yaxis.ticks)
p <- p[p >= ylim[1] & p <= ylim[2]]
return(p)
}
Expand Down
11 changes: 11 additions & 0 deletions inst/unitTests/runit.plot.R
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,14 @@ test.ylim_set_before_rendering <- function() {
p <- plot(xts(x, .Date(1:10)))
checkEqualsNumeric(range(x), p$get_ylim()[[2]])
}

test.yaxis.ticks <- function() {

x <- xts(rnorm(50), .Date(1:50))
ylim <- c(0, 10) # default case
p1 <- plot(x)
checkIdentical(pretty(ylim, 5), p1$Env$y_grid_lines(ylim))

p2 <- plot(x, yaxis.ticks = 10) # twice as many y-axis grid lines
checkIdentical(pretty(ylim, 10), p2$Env$y_grid_lines(ylim))
}
5 changes: 4 additions & 1 deletion man/plot.xts.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
dn.col = NULL, bg = "#FFFFFF", type = "l", lty = 1, lwd = 2, lend = 1,
main = deparse(substitute(x)), observation.based = FALSE,
ylim = NULL, yaxis.same = TRUE, yaxis.left = TRUE, yaxis.right = TRUE,
major.ticks = "auto", minor.ticks = NULL,
yaxis.ticks = 5, major.ticks = "auto", minor.ticks = NULL,
grid.ticks.on = "auto", grid.ticks.lwd = 1, grid.ticks.lty = 1,
grid.col = "darkgray", labels.col = "#333333", format.labels = TRUE,
grid2 = "#F5F5F5", legend.loc = NULL, extend.xaxis = FALSE)
Expand Down Expand Up @@ -62,6 +62,9 @@ separate panel.}

\item{yaxis.right}{if TRUE, draws the y axis on the right}

\item{yaxis.ticks}{desired number of y axis grid lines. The actual number
of grid lines is determined by the \code{n} argument to \code{\link{pretty}}.}

\item{major.ticks}{period that specifies where tick marks and labels will be
drawn on the x-axis. See Details for possible values.}

Expand Down

0 comments on commit a9f1959

Please sign in to comment.