# Overlay function on a scatterplot in ggplot2

#### spunky

##### Doesn't actually exist
So I've been trying to figure this one out for a while and I haven't been able to.

I have this nice scatterplot

Code:
x1 <- rnorm(200,   3,    .28)
y1 <- rnorm(200,   3,    .28)

data <- data.frame(x = c(x1), y = c(y1))
ggplot(data, aes(x=x, y=y)) + geom_point()
That gives me this nice graph

I also have this nice quadratic function

Code:
fun.1 <- function(x) x^2 + 2
p1 <- ggplot(data = data.frame(x = 0), mapping = aes(x = x))
p1 + stat_function(fun = fun.1) + xlim(-10,10)+ ylim(-3, 100)
That gives me this nice graph

What I'm trying to figure out here is how to overlay the two. So that the points are contained within the parabola (or outside of it if that's the case).

Any ideas?

#### hlsmith

##### Not a robit
So the parabola plot needs to be rescaled (since it goes to 100 and no point is > 4) and then you just want it overlaid on the scatterplot. No additional bells or whistles. So kind of like when you add abline to a plot?

#### spunky

##### Doesn't actually exist
So the parabola plot needs to be rescaled (since it goes to 100 and no point is > 4) and then you just want it overlaid on the scatterplot. No additional bells or whistles. So kind of like when you add abline to a plot?
Yup. It's pretty straightforward but I can't seem to find any examples online where the curve is not estimated or fitted from the data itself. And here I have two things going on, the basic scatterplot that just shows the relation between X and Y and a theoretical curve, my parabola, that I wanna show with the points inside of it.

#### spunky

##### Doesn't actually exist
So.... yeah. Spunky would like to apologize for creating garbage posts on the internet. Apparently, what I was originally trying was correct but I sort of forgot that ggplot2 pushes sections of the plot outside of the viewable area unless the scaling is done right.

SO, if you do something simple like this:

Code:
fun.1 <- function(x) x^2-30
x1 <- rnorm(200,   5,    10)
y1 <- rnorm(200,   5,    10)

data <- data.frame(x = c(x1), y = c(y1))
p11<-ggplot(data, aes(x=x, y=y)) + geom_point()
p11 + stat_function(fun = fun.1) + xlim(-10,10)+ ylim(-30,100)
You get something like this

Which is kind of what I was looking for

#### hlsmith

##### Not a robit
What is the plot for? It looks pretty pointless or bent