0

I am new to r but am using it for a project in which I wish to represent 3 different y values with same x value on the same scatteplot including linear regression lines for each along with the value. I don't know if what I have done so far is the best but:

leafdata.long<-melt(leafdata, id="Percent.Area.Loss", measure=c("R...mean", "G.mean", "B.mean"))

ggplot(leafdata.long, aes(Percent.Area.Loss, value, color=variable))+
geom_point()+geom_smooth(method=lm, se=FALSE)+opts(title="Compiled Leaf Data")

Here's the plot that it produced: https://i.stack.imgur.com/JmgtD.jpg

Any help with changing the x and y labels along with the legend would be appreciated also. I'm very much lost.

Stephen Henderson
  • 6,340
  • 3
  • 27
  • 33
user3088665
  • 11
  • 1
  • 1
  • 1
  • Have a look at `?labs`, for the legend title (`color`), and the x and y axis labels. The individual line labels are the levels of the factor, which you can change in your data. – Gregor Thomas Dec 10 '13 at 21:33
  • Also, `opts` has been deprecated for more than a year, you should update your `ggplot2` package. – Gregor Thomas Dec 10 '13 at 21:36
  • 1
    Have a look at previous posts on SO on the same topic, e.g. [**this**](http://stackoverflow.com/questions/7549694/ggplot2-adding-regression-line-equation-and-r2-on-graph). See also 'Linked' and 'Related' to the left. – Henrik Dec 10 '13 at 21:48

1 Answers1

7

The above link seems to do this way nicer. However, since I already had this done by the time I saw the post above I figured I'd post anyways.

library(plyr)
d<-data.frame(cat = sample(c("a","b","c"),100,replace=T), xval=seq_len(100), yval = rnorm(100))

r2<-ddply(d,.(cat),function(x) summary(lm(x$yval ~ x$xval))$r.squared)
names(r2)<-c("cat","r2")

g<-ggplot(d, aes(x = xval, y = yval, group = cat))+geom_point(aes(color=cat))
g<-g+geom_smooth(method="lm",aes(color=cat),se=F)

g+geom_text(data=r2,aes(color=cat, label = paste("R^2: ", r2,sep="")),parse=T,x=100,y=c(1,1.25,1.5), show_guide=F)

enter image description here

JPC
  • 1,891
  • 13
  • 29