I am trying to create linear correlation graph between to variables using ggplot2:
dput(sum)
structure(list(Date = structure(c(15218, 15248, 15279, 15309,
15340, 15371, 15400, 15431, 15461, 15492, 15522, 15553), class = "Date"),
Teams = c(87, 142, 173, 85, 76, 76, 93, 49, 169, 139, 60,
120), Scores = c("67101651", "62214988", "63183320", "66750198",
"61483322", "67546775", "75290893", "60713372", "77879142",
"70290302", "83201853", "83837301")), .Names = c("Date",
"Teams", "Scores"), row.names = c(NA, 12L), class = "data.frame")
this is my command:
ggplot(sum, aes(x = Scores, y = Teams, group=1)) +
geom_smooth(method = "lm", se=TRUE, size=2, formula = lm(Teams ~ Scores))
I get this error:
Error in eval(expr, envir, enclos) : object 'Teams' not found
any ideas?
If you want to specify the formula for, e.g., linear model, use
y ~ poly(x, 1). You don’t need to change theformulaparameter as long as you want a simple linear regression (it’s the default formethod = "lm"):I also would recommend using
Scoresas numeric values (as.numeric(Scores)) if you don’t want this variable to be categorial. This would change the regression line.Scoreas categorial variable:Scoreas numeric variable: