
With R and knitr this plot is relatively simple. However, the MWE is a bit complex to show automatically the actual coefficients (intercept, slope and error) as well as to place legend, arrow and label automatically, so one can change the values at some range (for instance, the second y from 2 to -3) and still have a correct output in all aspects, even in the text out of the figure.
\documentclass{article}
\usepackage{lipsum}
\usepackage[german]{babel}
\usepackage[utf8]{inputenc}
<<Daten,echo=F>>=
df <- data.frame(x=c(1,2,3,3,4,5),y=c(1,2,6,7,10,8))
@
\begin{document}
\lipsum[2]
<<Streudiagramm,echo=F,dev="tikz", fig.cap="Regressionszeile zeigt den Fehler $\\varepsilon_2$", fig.width=4.2, fig.height=3.5,fig.align='center',fig.pos="h">>=
par(mar=c(4,4,1,4)) # optional, just to crop
mod <- lm(df$y~df$x)
with(df,plot(x,y, pch=21, col="red",bg="yellow",ylim=c(min(df$y-.1),max(df$y+.1))))
abline(mod,col="blue",lwd=3)
legend(1, max(df$y), legend=c("$f(x_i)=\\beta_0+\\beta_1x_i$",
paste("$y=",
signif(mod$coefficients[1],3),"+",
signif(mod$coefficients[2],3),"x$")),
col=c("blue","white"), lty=1:2, cex=0.8)
arrows(df$x[2],df$y[2],df$x[2],predict(mod)[2], length=0.05, col =2, code=3)
text(df$x[2]+.1,mean(c(df$y[2],predict(mod)[2])),paste('Str\\"{o}erm: $\\varepsilon_i=y_i-f(x_i)$ =',signif(df$y[2]-predict(mod)[2],3)),adj=0)
@
Die Abbildung \ref{fig:Streudiagramm} zeigt das $\varepsilon_2 =
\Sexpr{signif(df$y[2],3)} -
\Sexpr{signif(predict(mod)[2],3)} =
\Sexpr{signif(df$y[2]-(mod$coefficients[1]+(mod$coefficients[2]*df$x[2])),3)} $.
\lipsum[3]
\end{document}
pgfplotsallows you to do this, see p 396Fitting Lines - Regression: https://ctan.org/pkg/pgfplots – AndréC Dec 01 '18 at 13:15pstricks(more preciselypst-plot) defines aplotstyle=LSMfor data files. – Bernard Dec 01 '18 at 13:34how she can create this graph. If she knew, she wouldn't have asked that question. – AndréC Dec 01 '18 at 13:39TikZor one of its descendants. She wants to know which packages allow her to do what she wants because she taggedgraphics(and notTikZ). I told herpgfplots, Bernard told herpstrics. – AndréC Dec 01 '18 at 15:15building a normal linear regression. Please modify this title to make the question easier to search with a search engine by clicking on theeditbutton. – AndréC Dec 02 '18 at 05:44