0

I'm trying to put a regression line into my graph, but it's just not working and I can't figure out why.

Here is my current code:

p1<-ggplot(data=artemia, aes(x=x1,y=BPM,color=Treatment))+
  geom_point()+
  geom_smooth(method="lm", formula= y~x)

x1 is just a factor() so that I can keep the axis organised. I have 6 sets of data within the 'artemia' dataset - 3 for each treatment. Ideally, I can put a regression line on each set of data. If that's impossible, then one regression line for each treatment would work perfectly fine, just to illustrate the data. With my current code, it seems to register that I want a line, as the legend to the side shows a line within it. However, it doesn't show up on the graph.

I've checked out other posts, but I can't find what I'm looking for, or a concise answer to this.

What am I doing wrong, and how can I fix it?

L Tyrone
  • 1,268
  • 3
  • 15
  • 24
eiion
  • 11
  • 4
  • 1
    As `x1` is a factor try with setting the `group` aes explicitly using `geom_smooth(aes(group = Treatment), ...)` to get a line per `Treatment`. For more help please provide [a minimal reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) including a snippet of your data or some fake data. – stefan May 06 '23 at 09:45
  • @stefan That worked! Thank you so much, this was a total lifesaver. I didn't realise that the factor might mess with the lines. Lab report saved. – eiion May 06 '23 at 09:52

0 Answers0