当使用facet_wrap时,显示每个散点图的回归方程和R ^ 2 [英] Display regression equation and R^2 for each scatter plot when using facet_wrap
问题描述
我有一个data.frame(我使用熔化函数进行了融合),从中生成多个散点图并使用以下代码拟合回归线:
ggplot(dat,aes(id,value))+ geom_point()+ geom_smooth(method =lm,se = FALSE)+ facet_wrap(变量〜var1,scales =free)
我想在每个中添加回归方程和R ^ 2 >这些散点图的相关回归(即每个散点图中由geom_smooth产生的散点图)。只是熔化数据的id列之一的名称,而且我正面临与facet_wrap的facet_grid instad相同的问题。
我实际上已经解决了这个问题,请参阅下面的一个解决因变量为var1的例子。这是一个时间序列数据集,如果与您的问题无关,请忽略日期部分。
library(plyr)
库(ggplot2)
rm(dat)
dat < - read.table(data.txt,header = TRUE,sep =,)
dat < - transform(dat,date = as.POSIXct(strptime(date,%Y-%m-%dT%H:%M:%OS)))
rm m)
dat.m< - melt(dat,id = c('ccy','date','var1'))
lm_eqn = function(df){
m = lm(var1〜value,df);
eq < - 替代(italic(y)== a + b%。%italic(x)*,~~ italic(r)^ 2〜=〜r2,
list (格式(coef(m)[1],digits = 2),
b =格式(coef(m)[2],digits = 2),
r2 =格式$ r.squared,digits = 3)))
as.character(as.expression(eq));
mymax = function(df){
max(df $ value)
}
rm(regs)
regs < - ddply(dat.m,。(ccy,variable),lm_eqn)
regs.xpos < - ddply(dat.m,。(variable),function(df)(min(df $值)+ max(df $值))/ 2)
regs.ypos < - ddply(dat.m,。(ccy,variable),function(df)min(df $ var1)+ 0.05 *( max(df $ var1)-min(df $ var1)))
regs $ y< - regs.ypos $ V1
regs $ x< - regs.xpos $ V1 $ b + b + b + gomplot(data = dat.m,aes(value,var1))+ geom_point(size = 1,alpha = 0.75)+ geom_smooth()+ geom_smooth(method =lm,se = FALSE,color =red)+ geom_text(data = regs,size = 3,color =red,aes(x = x,y = y,label = V1), parse = TRUE)+ facet_grid(ccy〜variable,scales =free)
ggsave(data.png,gp,scale = 1.5,width = 11,height = 8)
I have a data.frame (which I melted using the melt function), from which I produce multiple scatter plots and fit a regression line using the following:
ggplot(dat, aes(id, value)) + geom_point() + geom_smooth(method="lm", se=FALSE) + facet_wrap(variable~var1, scales="free")
I would like to add the regression equation and the R^2 in each of these scatter plots for the relevant regression (i.e. the one produced by geom_smooth in each scatter plot).
var1 above is just the name of one of the id columns of the melted data and I am facing the same question with facet_grid instad of facet_wrap.
I actually solved this, please see below a worked out example where the dependent variable is var1. This was a time series dataset, please ignore the date part if not relevant for your problem.
library(plyr)
library(ggplot2)
rm(dat)
dat <- read.table("data.txt", header = TRUE, sep = ",")
dat <- transform(dat, date = as.POSIXct(strptime(date, "%Y-%m-%dT%H:%M:%OS")))
rm(dat.m)
dat.m <- melt(dat, id = c('ccy','date','var1'))
lm_eqn = function(df){
m = lm(var1 ~ value, df);
eq <- substitute(italic(y) == a + b %.% italic(x)*","~~italic(r)^2~"="~r2,
list(a = format(coef(m)[1], digits = 2),
b = format(coef(m)[2], digits = 2),
r2 = format(summary(m)$r.squared, digits = 3)))
as.character(as.expression(eq));
}
mymax = function(df){
max(df$value)
}
rm(regs)
regs <- ddply(dat.m, .(ccy,variable), lm_eqn)
regs.xpos <- ddply(dat.m, .(variable), function(df) (min(df$value)+max(df$value))/2)
regs.ypos <- ddply(dat.m, .(ccy,variable), function(df) min(df$var1) + 0.05*(max(df$var1)-min(df$var1)))
regs$y <- regs.ypos$V1
regs$x <- regs.xpos$V1
rm(gp)
gp <- ggplot(data=dat.m, aes(value, var1)) + geom_point(size = 1, alpha=0.75) + geom_smooth() + geom_smooth(method="lm", se=FALSE, color="red") + geom_text(data=regs, size=3, color="red", aes(x=x, y=y, label=V1), parse=TRUE) + facet_grid(ccy~variable, scales="free")
ggsave("data.png", gp, scale=1.5, width=11, height=8)
这篇关于当使用facet_wrap时,显示每个散点图的回归方程和R ^ 2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!