使用 ggplot 设置默认 x 轴分隔符和标题

问题描述 投票:0回答:1

我正在尝试找到一种方法,在使用 ggplot 绘图时将默认的 x 轴中断设置为 0、20、40、60、80,并自动命名 x 轴时间 [天],而不必在每个图中指定它.

这可能吗?怎么可能?

我尝试在脚本的开头运行它,但由于无限递归而导致错误:

scale_x_continuous<- function(...) {
  scale_x_continuous(..., name = "time [days]", breaks = c(0, 20, 40, 60, 80))
}
r ggplot2 default x-axis
1个回答
0
投票

一种选择是将自定义函数命名为不同于

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

© www.soinside.com 2019 - 2024. All rights reserved.