1
votes

I like to make a ggplot theme where the default number of axis breaks is double the default. I'm not sure what setting to update. I can see from this question that ggplot calculates the breaks using labeling::extended. From the doc I can see that the argument m controls the number of breaks:

Usage
extended(dmin, dmax, m, Q = c(1, 5, 2, 2.5, 4, 3), only.loose = FALSE, w = c(0.25, 0.2, 0.5, 0.05))

m number of axis labels

This gets passed from scales::extended_breaks which I can see has the default set to 5:

 function (n = 5, ...) 
    {
        n_default <- n
        function(x, n = n_default) {
            x <- x[is.finite(x)]
            if (length(x) == 0) {
                return(numeric())
            }
            rng <- range(x)
            labeling::extended(rng[1], rng[2], n, ...)
        }
    }

So is there a theme setting I could change to default n to e.g. 10?

theme_more_ticks <- function(nticks = 10) {
    theme_minimal() +
    ...?
}

I know that breaks for an individual plot can be changed in many ways (ref). However, I would like all plots I produce with this theme to have double the number of breaks they would have with the default theme.

1
Maybe I'm missing the point, but why not use the breaks argument in the appropriate scale_xxx_yyy function?Limey
I want to change the default setting instead of adding scale_xxx_yyy to every plot.Pete900

1 Answers

1
votes

My apologies. I overlooked the word "theme" in the first line of your post.

I don't think you can do what you want using themes, because as I understand it, themes affect the appearance of ggplot2 elements, not the algorithms used to calculate their number, position, values etc. The best I could do was to modify the functions used to construct the ggplot object. For example

mtcars %>% ggplot() + geom_point(aes(x=cyl, y=mpg))

gives

enter image description here

But

scale_x_continuous <- function(...) ggplot2::scale_x_continuous(..., breaks=scales::extended_breaks(n=10, ...))
mtcars %>% ggplot() + geom_point(aes(x=cyl, y=mpg))

produces

enter image description here

So rather than redefining the theme, overwrite the various scale_xxxx_yyyy functions. That's a similar one-off task to redefining the default theme.

Check my handling of .... I've not checked it and I've got it wrong in the past.

Does that help?