我在ggplot2中有一个由分类变量分割的线图,并且在每个分面图中,这些线由不同的分类变量着色。我根据刻面图中的第二个分类变量拟合模型。然后我希望这些图显示每个回归线的斜率+ r ^ 2。
this question的答案有效,但仅限于使用单一回归线的非刻面情节,而我无法在我的情况下找到如何实现这一点。
我创建了一些示例数据......
ex.ID <- c(1,1,1,1,1,2,2,2,2,2,3,3,3,3,3,4,4,4,4,4,5,5,5,5,5,6,6,6,6,6,7,7,7,7,7,8,8,8,8,8)
ex.month <- c(0,6,12,18,24,0,6,12,18,24,0,6,12,18,24,0,6,12,18,24,0,6,12,18,24,0,6,12,18,24,0,6,12,18,24,0,6,12,18,24)
ex.label <- c("A","A","A","A","A","B","B","B","B","B","B","B","B","B","B","A","A","A","A","A","A","A","A","A","A","B","B","B","B","B","B","B","B","B","B","A","A","A","A","A")
ex.score <- c(100,100,89,85,70,95,90,90,86,80,84,80,71,68,60,100,100,98,90,92,100,95,90,85,80,98,96,90,83,81,85,80,78,72,69,95,90,89,85,80)
ex.status <- c("Y","Y","Y","Y","Y","Z","Z","Z","Z","Z","Y","Y","Y","Y","Y","Y","Y","Y","Y","Y","X","X","X","X","X","Z","Z","Z","Z","Z","Y","Y","Y","Y","Y","X","X","X","X","X")
ex.df <- data.frame(ex.ID,ex.month,ex.label,ex.score,ex.status)
然后我在ggplot2包中绘制。
ggplot(ex.df)+
geom_line(aes(x=ex.month,y=ex.score,group=ex.ID,color=ex.status)) +
facet_grid(.~ex.label) +
geom_smooth(aes(x=ex.month,y=ex.score,group=ex.status),method="lm")
我希望能够在我的图表上显示每个单独模型的斜率和r ^ 2。有没有办法在具有多条回归线的多面图上实现这一目标?
答案 0 :(得分:1)
以下是使用by
为每个组生成注释的解决方案:
# Generate a data.frane with the annotation for each group
lm_eqn <- function(df){
m <- lm(ex.score~ex.month , 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)))
data.frame(ex.label=df$ex.label[1],ex.status = df$ex.status[1],label=as.character(as.expression(eq)) )
}
# Group by ex.label(used in facet) and ex.ID (aes group on geom_smooth)
ll <-by(ex.df, list(ex.df$ex.label, ex.df$ex.status),lm_eqn, simplify = F)
# bind the groups
ldf <- do.call(rbind,ll)
# Generate the plot
ggplot(ex.df)+
geom_line(aes(x=ex.month,y=ex.score,group=ex.ID,color=ex.status)) +
facet_grid(.~ex.label) +
geom_text(aes(x=15, y = 100+as.numeric(ldf$ex.status), label = label, color=ex.status), data=ldf, parse = TRUE, show.legend = F) +
geom_smooth(aes(x=ex.month,y=ex.score,group=ex.status, color=ex.status),method="lm")