如何使x轴以ggplot2中的零拦截y轴

发布于 2025-02-12 04:47:36 字数 458 浏览 1 评论 0原文

我有一个简单的线图,其中包含y轴上的点小于零。 x轴位于图的底部,因此不会在y = 0时截距y轴。

如何使x =轴在ggplot2中截取y = 0?

示例代码:

df <- data.frame(x = 1:20, y = seq(-30, 65, by = 5))
p <- ggplot(df, aes(x, y)) +
    geom_line() +
    geom_hline(yintercept = 0, linetype = "dashed", color = "blue")
p

“

非常感谢!

I have a simple line graph, that contains points less than zero on the y-axis. The x-axis is at the bottom of the graph, and thus does not intercept the y-axis at y = 0.

How can I get the x=axis to intercept at y=0 in ggplot2?

Example code:

df <- data.frame(x = 1:20, y = seq(-30, 65, by = 5))
p <- ggplot(df, aes(x, y)) +
    geom_line() +
    geom_hline(yintercept = 0, linetype = "dashed", color = "blue")
p

Line graph

Many thanks in advance!

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

烂柯人 2025-02-19 04:47:37

选项1:用coord_cartesian

df <- data.frame(x=c(-5, 15),
                 y=c(-25, 25))
ggplot(df, aes(x,y)) + 
  geom_line() +
  geom_hline(yintercept = 0, linetype = "solid", color = "black") +
  coord_cartesian(ylim=c(0,50))

“在此处输入图像说明”

选项2:手动移动Xaxis标签。

ggplot(df, aes(x,y)) + 
  geom_line() +
  geom_hline(yintercept = 0, linetype = "solid", color = "black") +
  theme(axis.text.x = element_text(vjust = 120))

Option 1: Limit the y-axis display with coord_cartesian

df <- data.frame(x=c(-5, 15),
                 y=c(-25, 25))
ggplot(df, aes(x,y)) + 
  geom_line() +
  geom_hline(yintercept = 0, linetype = "solid", color = "black") +
  coord_cartesian(ylim=c(0,50))

enter image description here

Option 2: Shift the xaxis label manually.

ggplot(df, aes(x,y)) + 
  geom_line() +
  geom_hline(yintercept = 0, linetype = "solid", color = "black") +
  theme(axis.text.x = element_text(vjust = 120))

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文