我想保留ggplot2下面为 Y 轴提供的默认标签,但始终有一个 Y 轴刻度和/或标签y = 100来突出显示水平线截距。
library(ggplot2)
maxValue <- 1000
df <- data.frame(
var1 = seq(1, maxValue, by = 25),
var2 = seq(1, maxValue, by = 50)
)
ggplot(df, aes(x = var1, y = var2)) +
geom_point() +
geom_hline(yintercept = 100, color = "red")
Run Code Online (Sandbox Code Playgroud)

由reprex 包于 2022 年 4 月 9 日创建(v2.0.1.9000)
预期输出:
请注意,maxValue可以是任何东西。所以仅仅增加 100 步的解决方案是行不通的。例如:
plot <- plot +
scale_y_continuous(
breaks = seq(0, max(df$y) + 100, 100),
labels = …Run Code Online (Sandbox Code Playgroud)