2

my problem: I want to display two regression lines of different groups/compositions. I just started recently with ggplot and therefore have too little knowledge about regression lines in ggplot.

mel:
composition diametre    volume
mixed   0.261   71.3645893
mixed   0.233   392.9487358
mixed   0.319   284.8683927
pure    0.248   120.7654642
pure    0.274   142.1273373
pure    0.308   215.9924244
pure    0.188   26.11804847
pure    0.124   5.795590982
pure    0.307   136.7732086
pure    0.283   138.0600194
pure    0.175   32.43129359
pure    0.205   32.58726466
pure    0.159   12.27308951

The code below displays the scatterplot perfectly well but produces an error when adding geom_smooth (object 'volume' not found);

ggplot(data = mel, aes(x=diametre, y=volume,col=as.factor(composition))) +
  geom_point()+
  geom_smooth (data =  subset(mel, composition=="mixed"), method='lm',
              formula= log(volume)~I(diametre^2),
              se=FALSE, size=2) +
  geom_smooth (data = subset(mel, composition=="pure"), method = "lm",
              formula = (volume)~I(diametre^2), se = FALSE, size = 2)

I thought that maybe the error occurs because I am subseting the composition?! So I tried to work with one table for each composition hence the code looked like this

ggplot(data = mel, aes(x=diametre, y=volume,col=as.factor(composition))) +
  geom_point()+
  geom_smooth (data =  mel_mix, method='lm',
              formula= log(volume)~I(diametre^2),
              se=FALSE, size=2) +
  geom_smooth (data = mel_pure, method = "lm",
              formula = (volume)~I(diametre^2), se = FALSE, size = 2)

resulting in: invalid argument to unary operator

1 Answer 1

1

Simply this should work (you need to have x,y for geom_smooth):

ggplot(data = mel, aes(x=diametre, y=volume,col=as.factor(composition))) +
  geom_point() +
  geom_smooth(method='lm',
              formula= log(y)~I(x^2),
              se=FALSE, size=2)

or if you want to fit separately on subsetted data:

ggplot(data = mel, aes(x=diametre, y=volume,col=as.factor(composition))) +
  geom_point()+
  geom_smooth (data =  subset(mel, composition=="mixed"), method='lm',
               formula= log(y)~I(x^2),
               se=FALSE, size=2) +
  geom_smooth (data = subset(mel, composition=="pure"), method = "lm",
             formula = log(y)~I(x^2), se = FALSE, size = 2)

but volume~I(diametre^2) is a better fit for your data I think:

ggplot(data = mel, aes(x=diametre, y=volume,col=as.factor(composition))) +
  geom_point() +
  geom_smooth(method='lm',
              formula= y~I(x^2),
              se=FALSE, size=2)

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.