Moving X axis up/down

Hi, does anybody know a way to bring the X axis up/down in ggplot?

One semi-solution I found is to add geom_hline (to Y=0 point) + vjust option (to move X labels).
I am looking for a way to move everything including ticks.
Thanks!

Replicable R example:
value <- rnorm(19, 5, 15)
year <- seq(2001, 2019, by = 1)
DF <- data.frame(t(rbind(value,year)))
colnames(DF) <- c("value", "year")
ggplot(DF, aes(x = year, y = value))+
geom_line(color = "red")+
geom_hline(yintercept = 0, linetype = "solid", color = "black")+
theme(axis.text.x = element_text(vjust = 35))+
scale_x_continuous(labels = function(x) paste0(x, ""), name = "year", breaks = seq(2001, 2019, by = 2), limits = c(2001, 2019))

image

Take out

theme(axis.text.x = element_text(vjust = 35))+
1 Like

agree, instead use

 + coord_cartesian(ylim = c(0,40))

with whatever params you feel appropriate

1 Like

@nirgrahamuk, thanks! adding good_cartesian cuts the graph at Y=0 point.
And I want to keep negative values.

Thanks, @technocrat!
This moved X-axis text back to the original spot.
I'm looking for a way to move everything (X-axis line, text & ticks) to Y = 0 point?

ok, it seems what you want is for the xaxis ticks labels and alls going through the midline of the plot.
ggplot doesnt give elegant ways to do this at present.
There are a bunch of proposals at : https://stackoverflow.com/questions/17753101/center-x-and-y-axis-with-ggplot2
I would probably try https://stackoverflow.com/a/49202967/11726436

I'd leave the labels and ticks in the default positions and just add a geom_hline to highlight the zero

Thanks, @technocrat.
This is a semi-solution for sure.

1 Like

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.