EcologyTom
EcologyTom

Reputation: 2510

Multiple geom_hline in ggplot

I would like to include two horizontal lines using geom_hline in ggplot. The scale on my graph runs from 0 to 20,000 and I would like to have single lines at 400 and 17,000.

?geom_hline gives examples for single lines ( geom_hline(yintercept = 20) ) and for multiple lines ( (geom_hline(yintercept = 1:5) ). But the latter will give a line at every point between the two numbers.

So geom_hline(yintercept = 400: 17000) creates lines with intercepts at 400, 401, 402 etc. And geom_hline(yintercept = 400, 17000) only produces a single line for the first number. I'm sure there's something apart from : and , I need to try, does anyone have any suggestions?

Upvotes: 11

Views: 16083

Answers (1)

Jan Sila
Jan Sila

Reputation: 1593

I simulated some random data and plot them as a line. It works fine. Are your data in a data.frame?

Does the following code work for you?

x <- rnorm(500)
d <- data.frame("x" = x, "den" = dnorm(x))
ggplot(d, aes(x, den)) + geom_line() + geom_hline(yintercept = c(0.1,0.2))

Upvotes: 12

Related Questions