2

My graph looks like this:

enter image description here

As you can see there are 4 parameters (Treatment): NC, NF, TC, and TF. X axis shows the species number and Y shows the weight of crops. I want to draw a regression line of each with relation to species number? I tried abline but no luck.

Thanks

My code

ggplot(wheatX,aes(x=No.of.species,y=Weight,label=Treatment))+geom_point()+geom_text(aes(label=Treatment),hjust=0, vjust=0)
horseoftheyear
  • 917
  • 11
  • 23
Mr Good News
  • 121
  • 1
  • 3
  • 15
  • Try this: http://stackoverflow.com/questions/15633714/adding-a-regression-line-on-a-ggplot – Chris Nov 22 '15 at 09:02
  • @Chris Thanks. It wasnt accurate. But I found the geom_smooth. I researched it a bit and found the appropriate method that can be applied in my case. Thank you very much – Mr Good News Nov 22 '15 at 13:20
  • Welcome to stackoverflow (SO)! It's more likely that we will be able to help you if you make a minimal reproducible example to go along with your question. Something we can work from and use to show you how it might be possible to solve your problem. You can have a look at [this SO post](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) on how to make a great reproducible example in R. – Eric Fail Nov 22 '15 at 15:08

1 Answers1

1

As the comments suggest, you should provide a minimal reproducible example.

Creating a dummy data set with two types

dd = data.frame(x = 1:5, y = 1:5 + rnorm(10), type=c("A", "B"))

We first fit a regression model

m = lm(y ~ x + type, data=dd)

Then use the predict function

dd_m = data.frame(x=dd$x, y=predict(m, dd), type=dd$type)

to predict from the model using our original data set. This now gives a data frame dd_m that we can put in a standard geom_line call

library(ggplot2)

ggplot(dd) + geom_point(aes(x, y, colour=type)) + 
  geom_line(data=dd_m, aes(x, y, colour=type))

To get

enter image description here

csgillespie
  • 59,189
  • 14
  • 150
  • 185