In a scatterplot, I would like to display both the correlation coefficient along an equation describing the relationship between x and y. I have created my datamaterial, here is my code so far:
library(tidyverse)
# Creation of datamaterial
salary <- c(95, 100, 105, 110, 120, 124, 135, 150, 165, 175, 225, 230, 235, 260)
height <- c(160, 150, 182, 165, 172, 175, 183, 187, 174, 193, 201, 172, 180, 188)
fakenumbers <- data.frame(salary, height)
cor(height, salary, method = c("pearson"))
# Creation of scatterplot
r <- ggplot(fakenumbers, aes(x = height, y = salary)) +
geom_point(size = 3, shape = 21, color = "black", fill = "blue") +
labs(y = "Hourly salary
(sek)", x = "height (cm)", title = "Relationship between height and salary (made up data)") +
theme_classic() + theme(plot.title = element_text(hjust = 0.5, size = 18),
axis.title = element_text(size = 15),
axis.title.y = element_text(angle = 0, vjust = 0.5),
axis.text = element_text(size = 11))
# Adding a regressionline
r + geom_smooth(method = lm, formula = y ~ x, se = FALSE)
Inside of the coordinate system, next to the regressionline, I would like an "r = 0.588" displayed and some equation describing the linear relationship. How can I accomplish this, using preferably ggplot(), or some other function?


