有没有办法在ggplot中设置中断步长而不定义序列.例如:
x <- 1:10
y <- 1:10
df <- data.frame(x, y)
# Plot with auto scale
ggplot(df, aes(x,y)) + geom_point()
# Plot with breaks defined by sequence
ggplot(df, aes(x,y)) + geom_point() +
scale_y_continuous(breaks = seq(0,10,1))
# Plot with automatic sequence for breaks
ggplot(df, aes(x,y)) + geom_point() +
scale_y_continuous(breaks = seq(min(df$y),max(df$y),1))
# Does this exist?
ggplot(df, aes(x,y)) + geom_point() +
scale_y_continuous(break_step = 1)
Run Code Online (Sandbox Code Playgroud)
你可能会说我是懒惰,但也出现了几次,我不得不改变min和max我的极限seq由于添加的错误吧.所以我只想说...使用x的中断大小,具有自动缩放限制.
您可以定义自己的函数以传递给breaks参数.一个适用于您的案例的例子是
f <- function(y) seq(floor(min(y)), ceiling(max(y)))
Run Code Online (Sandbox Code Playgroud)
然后
ggplot(df, aes(x,y)) + geom_point() + scale_y_continuous(breaks = f)
Run Code Online (Sandbox Code Playgroud)
给
你可以修改它来传递休息的步骤,例如
f <- function(k) {
step <- k
function(y) seq(floor(min(y)), ceiling(max(y)), by = step)
}
Run Code Online (Sandbox Code Playgroud)
然后
ggplot(df, aes(x,y)) + geom_point() + scale_y_continuous(breaks = f(2))
Run Code Online (Sandbox Code Playgroud)
会创建一个y轴,刻度为2,4,...,10等.
您可以通过编写自己的缩放功能来进一步学习
my_scale <- function(step = 1, ...) scale_y_continuous(breaks = f(step), ...)
Run Code Online (Sandbox Code Playgroud)
并称之为
ggplot(df, aes(x,y)) + geom_point() + my_scale()
Run Code Online (Sandbox Code Playgroud)