0

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 Answer 1

0

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()

Sign up to request clarification or add additional context in comments.

2 Comments

thank you, but let me clarify: is it possible to do this, without having to create a new scale_x_continuous1 function, but by just changing the default for the original scale_x_continuous call?
Personally I'm not a of overwriting an existing function but of course could you do scale_x_continuous <- function(...) { ggplot2::scale_x_continuous(..., name = "time [days]", breaks = c(0, 20, 40, 60, 80)) }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.