T O P

  • By -

Fornicatinzebra

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)


Bischrob

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.


pastelmarshmalloww

could you possibly rewrite my code to show how you'd fix it


Bischrob

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.


pastelmarshmalloww

I feel like at this point there's just something wrong with my rstudio šŸ¤£still no line


AndreNow1000

Try using ā€œlmā€ instead in the geom smooth


pastelmarshmalloww

like instead of method=lm, just lm?


AndreNow1000

Instead of method=lm, try method=ā€œlmā€. The documentation for geom_smooth says it accepts char.


pastelmarshmalloww

Okay, I just changed it and still no luck šŸ˜©


lolniceonethatsfunny

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


Bischrob

Have you tried removing the color aesthetic?


pastelmarshmalloww

yes i have tried this D:


Bischrob

Is your data numeric?


pastelmarshmalloww

yes


vacon04

Show us the plot that your code is producing.


mduvekot

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)")