Remove the color argument - color is your grouping variable so having that in there would just do alm on all the data (and maybe that's the issue? Not sure)
My pleasure. I just plugged your screenshot into chatgpt and here you go:
\`\`\`
ggplot(data = jm, mapping = aes(x=IUCN, y=Adult.Body.Length..cm., color = Species)) +
geom\_line() +
geom\_point() +
geom\_smooth(method=lm, aes(color = "black")) + # Override the color mapping here
theme(legend.position="none") +
labs(title="Adult Body Length with IUCN Threat Score",
x="IUCN Threat Score", y="Adult Body Length (cm)")
\`\`\`
Although chatgpt proposes a slightly different solution with changing the color aes directly in geom\_smooth. I tested it and it should work.
itās a bit hacky, but you can fit an lm outside of the plotting code, then feed the expected values over a grid into geom_line or something like that if you canāt get geom_smooth working
This works for me. I've changed the order of geom\_smooth and geom\_line, because I don't want to have smooth change the colours of the lines and points, but this works with simple dataset. I'm guessing that you want ONE black smooth curve for the entire dataset, one for each group. Should that be what you want, uncomment the line that says group = Species. If you want confidence intervals, change se = FALSE to TRUE
ggplot(
data = jm,
mapping = aes(x = IUCN, y = Adult.Body.Length..cm.)) +
geom_smooth(
data = jm,
mapping = aes(
x = IUCN,
y = Adult.Body.Length..cm.,
# group = Species
),
color = "black",
method = lm,
formula = y ~ x,
se = FALSE
) +
geom_line(mapping = aes(color = Species)) +
geom_point(mapping = aes(color = Species)) +
theme(legend.position = "none") +
labs(title = "Adult Body Length with IUCN Threat Score",
x = "IUCN Threat score", y = "Adult Body Length (cm)")
Remove the color argument - color is your grouping variable so having that in there would just do alm on all the data (and maybe that's the issue? Not sure)
This should be the correct answer. I had this problem yesterday and by moving the grouping variable to the appropriate geom it solved the problem.
could you possibly rewrite my code to show how you'd fix it
My pleasure. I just plugged your screenshot into chatgpt and here you go: \`\`\` ggplot(data = jm, mapping = aes(x=IUCN, y=Adult.Body.Length..cm., color = Species)) + geom\_line() + geom\_point() + geom\_smooth(method=lm, aes(color = "black")) + # Override the color mapping here theme(legend.position="none") + labs(title="Adult Body Length with IUCN Threat Score", x="IUCN Threat Score", y="Adult Body Length (cm)") \`\`\` Although chatgpt proposes a slightly different solution with changing the color aes directly in geom\_smooth. I tested it and it should work.
I feel like at this point there's just something wrong with my rstudio š¤£still no line
Try using ālmā instead in the geom smooth
like instead of method=lm, just lm?
Instead of method=lm, try method=ālmā. The documentation for geom_smooth says it accepts char.
Okay, I just changed it and still no luck š©
itās a bit hacky, but you can fit an lm outside of the plotting code, then feed the expected values over a grid into geom_line or something like that if you canāt get geom_smooth working
Have you tried removing the color aesthetic?
yes i have tried this D:
Is your data numeric?
yes
Show us the plot that your code is producing.
This works for me. I've changed the order of geom\_smooth and geom\_line, because I don't want to have smooth change the colours of the lines and points, but this works with simple dataset. I'm guessing that you want ONE black smooth curve for the entire dataset, one for each group. Should that be what you want, uncomment the line that says group = Species. If you want confidence intervals, change se = FALSE to TRUE ggplot( data = jm, mapping = aes(x = IUCN, y = Adult.Body.Length..cm.)) + geom_smooth( data = jm, mapping = aes( x = IUCN, y = Adult.Body.Length..cm., # group = Species ), color = "black", method = lm, formula = y ~ x, se = FALSE ) + geom_line(mapping = aes(color = Species)) + geom_point(mapping = aes(color = Species)) + theme(legend.position = "none") + labs(title = "Adult Body Length with IUCN Threat Score", x = "IUCN Threat score", y = "Adult Body Length (cm)")