Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple geom_hline in ggplot

Tags:

r

ggplot2

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?

like image 985
EcologyTom Avatar asked Aug 11 '16 09:08

EcologyTom


1 Answers

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))
like image 100
Jan Sila Avatar answered Sep 25 '22 23:09

Jan Sila