Search code examples
rggplot2regressionlinear-regression

Adding custom regression line with set intercept and slope to ggplot


I have created a plot using the following code:

ggplot(Data, aes(x=damMean, y=progenyMean)) + 
    geom_point()

enter image description here

I want to overlay on the plot a regression line of the form: y = 69.88 + 5.58*x

I tried to do so by adding the following:

ggplot(Data, aes(x=damMean, y=progenyMean)) + 
    geom_point() + 
    geom_smooth(method = "lm", formula = y~69.88+5.58*x)

But this doesn't add a line to the plot.

Is this possible to do using ggplot?


Solution

  • Here's an example with some fake data:

    mydat <- tibble(x=runif(100, 40, 90), 
                    y = 80 + 5.5*x + rnorm(100, 0, 10))
    
    ggplot(mydat, aes(x=x, y=y)) + 
      geom_point() + 
      geom_abline(slope=5.58, intercept=69.88)
    

    enter image description here