0

I am trying to make a trajectory line representing brain atrophy over time. I have two specific trajectories for two different groups, and I want to add each of these two lines (for comparison) to a scatterplot of Age by Brain Volume.

The y-intercepts of these lines are 565 and 286, yet using the stat_function layer it seems to be placing the intercepts at very weird values, corrupting the overall scatterplot. Of course I can use ylim(), but then the functions don't appear (and they'd still be mis-specified even if they did).

My code:

ggplot(na.omit(GRAPHICSMATRIX), aes(x=Age, y=FrontalPole, colour=Dx_Bl)) +
  geom_point(size=3) +  
  ylim(0, 500) + 
  stat_function(fun=function(x) {-2.129*x^3 - 23.456*x + 565}, colour="blue", size=1) +  
  stat_function(fun=function(x) (286)+((-0.997)*(x)^3), colour="darkred", size=2) 

Here are the graphs.

Correct Scatterplot

Incorrect Scatterplot with two stat_function()

2
  • Did you run function(x) {-2.129*x^3 - 23.456*x + 565} with GRAPHICSMATRIX$Age as input outside of the ggplot2 code? Commented May 29, 2016 at 2:25
  • The y-intercepts are reasonable numbers, but those are evaluated at x=0. Your plots start around x=60 in which case you function gives a result on the order of 216,000. Just plug in x=60 and you can see that. Commented May 29, 2016 at 3:27

1 Answer 1

2

Yes, your y-intercepts are 565 and 286 respectively, but those values are for when x=0. The plots you show in your question start at around x=55. In this case when we plug in x=55 for your functions we get:

-2.129(55^3) - 23.456(55) + 565 = 354,937.455

286 + -0.997*(55^3) = 165,589.875

So what you're thinking is your y-intercept is not actually. I think you stat_functions are working correctly. If you don't like the results I think you need to re-think the functions you want to plot.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you. My model was based on a demographic of 55-90 year olds, but the model calculations presume an intercept of 0. So I solved this issue by shifting the functions to the right by 55, which I did by subtracting 55 at each X (ie (x-55)^2) Thank you!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.