我有一個簡單的折線圖,其中包含 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
提前謝謝了!
uj5u.com熱心網友回復:
選項 1:限制 y 軸顯示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))
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/498308.html