You can remove the actual x axis, and manually add an axis line that sits underneath the data by using geom_hline(yintercept = 0)
. It's important to put your geom_hline()
axis before the geom_line()
that plots your data in your plot code. ggplot plots items in the order they are written, so if you call geom_line()
after geom_hline()
your data will be drawn on top of the axis line.
#some made up data
df <- data.frame(x = 1:12, y = c(rep(0,12), rep(c(1,2), 6)),
group = c(rep("zeros", 12), rep("not zeros", 12)))
ggplot(df, aes(x = x, y = y, color = group)) +
geom_hline(yintercept = 0) + #first, add an axis line using geom_hline
geom_line() + #next, add the geom for your data
theme_classic() +
coord_cartesian(expand = FALSE) +
theme(axis.line.x = element_blank()) #remove actual x axis line
