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
Many thanks in advance!
CodePudding user response:
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))
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))