如何使用ggplot2在R中添加r ^ 2值图? [英] How to add r^2 values graph in R using ggplot2?

查看:569
本文介绍了如何使用ggplot2在R中添加r ^ 2值图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是r的新手,但是我将其用于一个项目,我希望在同一个散点图上用相同的x值表示3个不同的y值,包括每个值的线性回归线。我不知道我到目前为止所做的是否是最好的,但是:

pre $ $ $ $ $ $ $ $ $ $> 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 =编译叶数据)

下面是它生成的图:
http://imgur.com/eXNFY5d



任何有关更改x和y标签以及图例的帮助, 。我非常失望。

解决方案

上面的链接似乎做得更好。不过,由于我看到上面的帖子的时候已经完成了这个工作,我想我会发布。

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

r2 <-ddply (x)摘要(lm(x $ yval〜x $ xval))$ r.squared)
名称(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)


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: http://imgur.com/eXNFY5d

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

解决方案

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)

这篇关于如何使用ggplot2在R中添加r ^ 2值图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆