mrip mrip - 2 months ago 21
R Question

making a single gridline thicker in ggplot2

I have a plot of a bunch of values between -1 and 1. Let's say it looks like this:

data <- data.frame(x=1:20,y=runif(20)*2-1)
p <- ggplot(data=data,aes(x=x,y=y)) + geom_line() + scale_y_continuous(breaks=seq(-1,1,0.5),limits=c(-1,1))
p


enter image description here

What I want is for the horizontal line with y=0, to be thicker than all the other gridlines. Is there a simple way to do this?

Answer

You can add a line at y = 0 via geom_hline. Put this layer before geom_line so it is behind the rest of the plot.

ggplot(data=data,aes(x=x,y=y)) + 
    geom_hline(yintercept = 0, color = "white", size = 2) +
    geom_line() + 
    scale_y_continuous(breaks=seq(-1,1,0.5),limits=c(-1,1))