So I am trying to add a simple equation to my graph using the text function in R but for some reason its not working.
This is how I generated my randon simulated data:
a <- 5 b <- 7 sigma <- 3 x <- runif(100, min = 0, max = 50) n <- length(x) y <- a + b * x + rnorm(n, 0, sigma) fakeData <- data.frame(x, y) Then I plugged in the data frame and added a regression line like so.
ggplot(data = fakeData, aes(y, x)) + geom_point() + geom_smooth(method = 'lm', se = FALSE) + labs(x = "random uniform distribution from range 0 - 50.", y = "Linear model points based on the Uniform dist", title = "Fake data simulation") Which results in a graph like this
Now all I want to do is add the equation of the line to the graph using the text function but for some reason, R just seems to be throwing errors at me. I tried googling the issue but I wasnt able to resolve it. I tried this method also but the lack of documentation is appalling I have no idea what the guy was doing in the function.



geom_text?text()is a base R graphics function and cannot be used withggplot. I would encourage you to use the answer at the duplicate you already identified: stackoverflow.com/questions/7549694/…. I'm not sure which answer was appalling, but there are a few answers there.text()would work if you were just using baseplot()but you are not. There are different methods required if you are using base R graphics vs ggplot graphics and you basically can't combine them. It's unclear what your textbook may have intended.