有没有办法提取stat_smooth返回的拟合线的值?
我使用的代码如下所示:
p <- ggplot(df1, aes(x=Days, y= Qty,group=Category,color=Category))
p <- p + stat_smooth(method=glm, fullrange=TRUE)+ geom_point())
Run Code Online (Sandbox Code Playgroud)
这个新用户会非常感谢任何指导.
我正在探索一些数据,所以我想要做的第一件事就是尝试将正态(高斯)分布拟合到它.这是我第一次在R中尝试这个,所以我一步一步.首先我预先分类我的数据:
myhist = data.frame(size = 10:27, counts = c(1L, 3L, 5L, 6L, 9L, 14L, 13L, 23L, 31L, 40L, 42L, 22L, 14L, 7L, 4L, 2L, 2L, 1L) )
qplot(x=size, y=counts, data=myhist)
Run Code Online (Sandbox Code Playgroud)

由于我需要计数,我需要添加一个归一化因子(N)来扩大密度:
fit = nls(counts ~ N * dnorm(size, m, s), data=myhist, start=c(m=20, s=5, N=sum(myhist$counts)) )
Run Code Online (Sandbox Code Playgroud)
然后我创建适合显示的数据,一切都很好:
x = seq(10,30,0.2)
fitted = data.frame(size = x, counts=predict(fit, data.frame(size=x)) )
ggplot(data=myhist, aes(x=size, y=counts)) + geom_point() + geom_line(data=fitted)
Run Code Online (Sandbox Code Playgroud)

当我发现这个线程谈到使用geom_smooth()一步完成所有这一切时,我很兴奋,但我无法让它工作:
这是我尝试的......以及我得到的:
ggplot(data=myhist, aes(x=size, y=counts)) + geom_point() + geom_smooth(method="nls", formula = counts ~ N * …Run Code Online (Sandbox Code Playgroud) 我不知道如何得到回归线方程,我用函数geom_smooth绘制的线性回归的r ^ 2和p值.
这是我的代码:
g <- ggplot(data=data.male, aes(x=mid_year, y=mean_tc, colour=data.male$survey_type))
g <- g + geom_point(shape = 20, size =2)
g <- g + geom_smooth(method=lm, na.rm = FALSE, se = TRUE, aes(group=1), colour = "black")
g <- g + theme_gray(base_size=24)
g <- g+ xlab("Year")
g <- g + ylab("Mean serum total cholesterol (mmol/L)")
g <- g + theme(legend.position="bottom")
g <- g + scale_y_continuous(limits=c(3.5,6.5), breaks=c(3.5,4,4.5,5,5.5,6,6.5))
g <- g + scale_x_continuous(limits=c(1980,2015), breaks=c(1980,1990,2000,2010))
g <- g + scale_colour_manual(name = "Survey Type", values= c("Red", "Blue", …Run Code Online (Sandbox Code Playgroud)