![](/img/trans.png)
[英]How to get both positive and negative number for x-axis in ggplot2 and rescale the y-axis in R?
[英]How to get the x-axis to intercept the y-axis at zero in ggplot2
我有一个简单的折线图,其中包含 y 轴上小于零的点。 x 轴位于图表的底部,因此不会在 y = 0 处与 y 轴相交。
如何让 x=axis 在 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
提前谢谢了
选项 1:使用coord_cartesian
限制 y 轴显示
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 label。
ggplot(df, aes(x,y)) +
geom_line() +
geom_hline(yintercept = 0, linetype = "solid", color = "black") +
theme(axis.text.x = element_text(vjust = 120))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.