Normally you can change the color of the entire axis line with:
theme ( axis.line.y = element_line(color = "gray", size = 1) )
However, I want to draw a Y-axis where different parts have different colors. For example, a blue line below 0, and red above zero. A small example, where the axis has been "painted" manually:
df <- data.frame(x = 1:8, y = -3:4)
ggplot(df, aes(x, y)) +
geom_point()
Thanks a lot!
EDIT: The answer by Allan Cameron is correct but doesn´t work for me since I´ve got a time series plot where my x-axis is in time format (POSIXct). His post gave me some an idea how this works and I feel like I´ve got it working with some tricks. Here is how I do it, it can be adapted for different cases / ranges. Need package "lubridate" to add / substract times.
- Get min and max of my x-axis:
minX <- min(myData$Timestamp)
maxX <- max(myData$Timestamp)
- Set limit:
lim <- as.POSIXct(c(minX -minutes (5), maxX), origin = "1970-01-01")
- coord_cartesian like this:
+ coord_cartesian(xlim=c(minX - minutes(5), maxX),c(60,100), expand = FALSE)
- Draw line:
+ geom_segment(aes(x = minX - minutes(5), y = 60, xend = minX - minutes(5), yend = 70), size = 1, color = "red"
Keep in mind this is for my use case. I may need to adjust this (and edit this post more)

