Setting default x axis breaks and title using ggplot

18 Views Asked by At

I am trying to find a way to set the default x axis breaks at 0, 20, 40, 60, 80 when plotting using ggplot, and automatically name the x axis time [days], without having to specify this in every single plot.

is this possible, and how would it be possible?

i have tried running this in the beginning of my script, but it resulted in an error due to infinite recursion:

scale_x_continuous<- function(...) {
  scale_x_continuous(..., name = "time [days]", breaks = c(0, 20, 40, 60, 80))
}
1

There are 1 best solutions below

2
stefan On

One option would be to name your custom function different from scale_x_continuous:

library(ggplot2)

scale_x_continuous1 <- function(...) {
  scale_x_continuous(..., name = "time [days]", breaks = c(0, 20, 40, 60, 80))
}

ggplot(mtcars, aes(hp, mpg)) +
  geom_point() +
  scale_x_continuous1()