Im trying to fit a linear model here, yet instead of points plotted with geom_point i get a line through every datapoint. Does anyone know how to solve this so i get points rather than this jagged line. Data and code below
data snippet
height weight age gender
<dbl> <dbl> <dbl> <fct>
1 152. 47.8 63 1
2 140. 36.5 63 0
3 137. 31.9 65 0
4 157. 53.0 41 1
5 145. 41.3 51 0
6 164. 63.0 35 1
7 149. 38.2 32 0
8 169. 55.5 27 1
9 148. 34.9 19 0
10 165. 54.5 54 1
11 154. 49.9 47 0
12 151. 41.2 66 1
code used;
linear_mod = lm(weight ~ height, data = weight_height)
#plot predictions
#function
plot_predicts <- function(model, df) {
#make predictions
preds <- predict(model, df)
#plot
df %>%
mutate(predicted=preds) %>%
dplyr::select(weight, height, predicted) %>%
pivot_longer(-height) %>%
ggplot(aes(x=height, y=value, colour=name)) +
geom_point(data=. %>% filter(name=="height")) +
geom_line(data=. %>% filter(name!="height"))
}
plot_predicts(linear_mod, weight_height)
the data snipet might not be enough to re-run the graph i get so i have attatched it below



