+ Reply to Thread
Results 1 to 5 of 5

Thread: Overlay function on a scatterplot in ggplot2

  1. #1
    TS Contributor
    Points: 22,866, Level: 93
    Level completed: 52%, Points required for next Level: 484
    spunky's Avatar
    Location
    vancouver, canada
    Posts
    2,157
    Thanks
    166
    Thanked 540 Times in 433 Posts

    Overlay function on a scatterplot in ggplot2




    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?
    for all your psychometric needs! https://psychometroscar.wordpress.com/about/

  2. #2
    Omega Contributor
    Points: 39,007, Level: 100
    Level completed: 0%, Points required for next Level: 0
    hlsmith's Avatar
    Location
    Not Ames, IA
    Posts
    7,068
    Thanks
    402
    Thanked 1,191 Times in 1,152 Posts

    Re: Overlay function on a scatterplot in ggplot2

    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?
    Stop cowardice, ban guns!

  3. #3
    TS Contributor
    Points: 22,866, Level: 93
    Level completed: 52%, Points required for next Level: 484
    spunky's Avatar
    Location
    vancouver, canada
    Posts
    2,157
    Thanks
    166
    Thanked 540 Times in 433 Posts

    Re: Overlay function on a scatterplot in ggplot2

    Quote Originally Posted by hlsmith View Post
    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.
    for all your psychometric needs! https://psychometroscar.wordpress.com/about/

  4. #4
    TS Contributor
    Points: 22,866, Level: 93
    Level completed: 52%, Points required for next Level: 484
    spunky's Avatar
    Location
    vancouver, canada
    Posts
    2,157
    Thanks
    166
    Thanked 540 Times in 433 Posts

    Re: Overlay function on a scatterplot in ggplot2

    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
    for all your psychometric needs! https://psychometroscar.wordpress.com/about/

  5. #5
    Omega Contributor
    Points: 39,007, Level: 100
    Level completed: 0%, Points required for next Level: 0
    hlsmith's Avatar
    Location
    Not Ames, IA
    Posts
    7,068
    Thanks
    402
    Thanked 1,191 Times in 1,152 Posts

    Re: Overlay function on a scatterplot in ggplot2


    What is the plot for? It looks pretty pointless or bent
    Stop cowardice, ban guns!

+ Reply to Thread

           




Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts






Advertise on Talk Stats