geom_abline() 强制 y 轴为 0

发布于 2025-01-11 13:59:30 字数 1527 浏览 0 评论 0原文

有没有办法在不改变比例的情况下添加geom_abline()?我做了一些挖掘,但没有找到任何专门解决这个问题的东西,并且手动重新缩放轴会删除 abline:

library(tidyverse)

# generate noise that should be roughly 1:1
plot <- 
  runif(100, 3000, 6000) %>%
  as_tibble() %>%
  rename(x = value) %>%
  rowwise() %>%
  mutate(y = rnorm(1, x, 150)) %>%
  ggplot(aes(x, y)) +
  geom_point()

plot

# geom_abline forces y-axis to 0!
plot +
  geom_abline()

# adding in manual scales removes geom_abline
plot +
  geom_abline() +
  scale_y_continuous(limits = c(3000, 6000))
#> Warning: Removed 1 rows containing missing values (geom_abline).

# reording when `scale_y_continous()` is called doesn't help unfortunately
plot +
  scale_y_continuous(limits = c(3000, 6000)) +
  geom_abline()
#> Warning: Removed 1 rows containing missing values (geom_abline).

packageVersion("ggplot2")
#> [1] '3.3.4'

reprex 包 (v2.0.1)

Is there a way to add geom_abline() without changing the scales? I've done a bit of digging and haven't found anything that addresses this specifically, and manually rescaling the axes removes the abline:

library(tidyverse)

# generate noise that should be roughly 1:1
plot <- 
  runif(100, 3000, 6000) %>%
  as_tibble() %>%
  rename(x = value) %>%
  rowwise() %>%
  mutate(y = rnorm(1, x, 150)) %>%
  ggplot(aes(x, y)) +
  geom_point()

plot

# geom_abline forces y-axis to 0!
plot +
  geom_abline()

# adding in manual scales removes geom_abline
plot +
  geom_abline() +
  scale_y_continuous(limits = c(3000, 6000))
#> Warning: Removed 1 rows containing missing values (geom_abline).

# reording when `scale_y_continous()` is called doesn't help unfortunately
plot +
  scale_y_continuous(limits = c(3000, 6000)) +
  geom_abline()
#> Warning: Removed 1 rows containing missing values (geom_abline).

packageVersion("ggplot2")
#> [1] '3.3.4'

Created on 2022-03-04 by the reprex package (v2.0.1)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文