동일한 그래프에 두 개의 선형 회귀 방정식과 계수 (r, r², p, N)를 표시해야합니다. facet_grid를 사용하여이 작업을 수행했지만 이제 두 곡선을 개별적으로 표시 할 수 없습니다.
facet_grid 함수와 같은 코드를 수정했습니다.
equation = function(file) {
mod = lm(y ~ x,data=file)
mod_sum = summary(mod)
formula = sprintf("y= %.3f %+.3f*x", coef(mod)[1], coef(mod)[2])
r = mod_sum$r.squared
r2 = sprintf("r2= %.3f", r)
x = cor.test(~x + y,data=file)
r0 = sprintf("r= %.3f", x[4])
p1 = pf(mod_sum$fstatistic[1],mod_sum$fstatistic[2],mod_sum$fstatistic[3],lower.tail=F)
p =sprintf("p = %.3f", p1)
n0 = length(mod_sum$residual)
n1 = sprintf("N = %.f", n0)
data.frame(formula=formula, r=r0,r2=r2, p=p,n=n1, stringsAsFactors=FALSE)
}
equation_end = ddply(file, c("outlier"), equation)
두 회귀 분석의 데이터는 동일한 열에 있으며 "이상 값"요인으로 구분됩니다.
이 방정식을 같은 그래프에 어떻게 표시 할 수 있습니까?
annotate
그림에 텍스트를 배치 하는 데 사용할 수 있습니다.
library(ggplot2)
ggplot(file, aes(x, y, color=outlier)) +
geom_point() +
annotate("text", c(-1,-1), c(3,4), label=equation_end$formula)
텍스트를 일부 줄과 같은 색상으로 표시하려면 geom_text
,
ggplot(file, aes(x, y, color=outlier)) +
geom_point() +
geom_smooth(fill=NA) +
geom_text(data=equation_end, aes(x=c(-1,-1), y=c(3,4), label=formula), show_guide=F)
데이터:
library(plyr)
x <- rnorm(100)
file <- data.frame(x=x, y=2*x + rnorm(100), outlier=factor(sample(0:1, 100, rep=T)))
equation_end = ddply(file, c("outlier"), equation)
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다