I was plotting a regression line using geom_smooth()
alias from ggplot2
as below:
library(ggplot2)
X <- c(3, 7, 10, 14, 15, 11, 13, 18)
Y <- c(11, 5, 3, 9, 7, 5, 2, 1)
ggplot(data = data.frame(X, Y), aes(x = X, y = Y)) + theme_bw() +
geom_point(col = 'red') + geom_smooth(method = 'lm', formula = Y ~ X)
Surprisingly, I got a warning message as:
Warning messages:
1: 'newdata' had 80 rows but variables found have 8 rows
2: In base::data.frame(x = xseq, fit, se = pred$se.fit) :
row names were found from a short variable and have been discarded
With the following graph:
I was expecting a linear graph (a straight line) as:
which I found by correcting the R codes to:
library(ggplot2)
x <- c(3, 7, 10, 14, 15, 11, 13, 18)
y <- c(11, 5, 3, 9, 7, 5, 2, 1)
ggplot(data = data.frame(x, y), aes(x = x, y = y)) + theme_bw() +
geom_point(col = 'red') + geom_smooth(method = 'lm', formula = y ~ x)
Why did it happen? What is happening if we write that formula
in uppercase?