Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I want the make the following population regression function after some modifications with pgfplot (response from previous question here).

enter image description here

Required Modifications

  1. Draw small circle at each normal distributions mean.
  2. Draw a line that passes through the means.
  3. Join means with xticklabs and yticklabs.

Here is my MWE. Thanks for any help.

\documentclass{standalone}
\usepackage{pgfplots}

\begin{document}

\begin{tikzpicture}[ % Define Normal Probability Function
declare function={
            normal(\x,\m,\s) = 1/(2*\s*sqrt(pi))*exp(-(\x-\m)^2/(2*\s^2));
        }
       ]

\draw [help lines] (0,0) grid (6,6);

\begin{axis}[
    no markers,
    domain=-3.2:3.2,
    samples=100,
    axis lines=left,
    enlarge x limits=true,
    enlarge y limits=true,
    xtick={0,0.5,1},
    ytick={0,0.5,1},
    xticklabels={$X_1$, $X_2$, $X_3$},
    yticklabels={$Y_1$, $Y_2$, $Y_3$},
    xlabel=$X$, xlabel style={at=(xticklabel cs:1), anchor=south},
    ylabel=$Y$, ylabel style={at=(yticklabel cs:1), rotate=-90, anchor=west},
  ]


\addplot [samples=2, domain=0:1.4] {2.5+4*x};
\addplot[cyan!50!black, thick, domain=0:6] ({normal(x, 3, 1)+0.0},x);
\addplot[cyan!50!black, thick, domain=2:8] ({normal(x, 5, 1)+0.5},x);
\addplot[cyan!50!black, thick, domain=4:10] ({normal(x, 7, 1)+1},x);

\draw (0, 0) --(5, 16) -- (0, 16);
\draw[fill] (5, 16) circle [radius=2.0pt];

\node[above right] (5,15) {$\mathbf{E}\left(Y|X_{i}\right)=\beta_{0}+\beta_{1}X$};

\node[above right] (15,25) {Distribution of \\$Y$ values \\given $X=x_3$};

\end{axis}


\end{tikzpicture}

\end{document}

enter image description here

My node placement is not correct.

Edited

Following @Qrrbrbirlbel comment, I have partial solution:

\documentclass{standalone}
\usepackage{pgfplots}

\begin{document}

\begin{tikzpicture}[ % Define Normal Probability Function
declare function={
            normal(\x,\m,\s) = 1/(2*\s*sqrt(pi))*exp(-(\x-\m)^2/(2*\s^2));
        }
       ]

\draw [help lines] (0,0) grid (6,6);

\begin{axis}[
    no markers,
    domain=-3.2:3.2,
    samples=100,
    axis lines=left,
    enlarge x limits=true,
    enlarge y limits=true,
    xtick={0, 0.5, 1},
    ytick={3, 5, 7},
    xticklabels={$X_1$, $X_2$, $X_3$},
    yticklabels={$Y_1$, $Y_2$, $Y_3$},
    xlabel=$X$, xlabel style={at=(xticklabel cs:1), anchor=south},
    ylabel=$Y$, ylabel style={at=(yticklabel cs:1), rotate=-90, anchor=west},
  ]


\addplot[cyan!50!black, thick, domain=0:6] ({normal(x, 3, 1)+0.0},x);
\addplot[cyan!50!black, thick, domain=2:8] ({normal(x, 5, 1)+0.5},x);
\addplot[cyan!50!black, thick, domain=4:10] ({normal(x, 7, 1)+1},x);

\draw[dashed] (axis cs: 0, -1) -- (axis cs: 0, 3);
\draw[dashed] (axis cs: -1, 3) -- (axis cs: 0, 3);
\draw[fill] (axis cs: 0.0, 3) circle [radius=2.0pt];

\draw[dashed] (axis cs: 0.5, -1) -- (axis cs: 0.5, 5);
\draw[dashed] (axis cs: -1, 5) -- (axis cs: 0.5, 5);
\draw[fill] (axis cs: 0.5, 5) circle [radius=2.0pt];

\draw[dashed] (axis cs: 1, -1) -- (axis cs: 1, 7);
\draw[dashed] (axis cs: -1, 7) -- (axis cs: 1, 7);
\draw[fill] (axis cs: 1, 7) circle [radius=2.0pt];

\addplot [samples=2, domain=0:1.4] {3+4*x};

\node[above right] (cs:1, cs:7) {$\mathbf{E}\left(Y|X_{i}\right)=\beta_{0}+\beta_{1}X$};

\node[above right] (axis cs: 15, 25) {Distribution of \\$Y$ values \\given $X=x_3$};

\end{axis}


\end{tikzpicture}

\end{document}

enter image description here

Still Issues

  1. Text nodes are correctly placed.
  2. Line passes through the means does not intersect y-axis.
share|improve this question
2  
You should use the axis cs: instead, I guess: \draw (axis cs: 0, 0) -- (axis cs: 5, 16) -- (axis cs: 0, 16); –  Qrrbrbirlbel Nov 10 '13 at 6:21
    
Thenks @Qrrbrbirlbel for your comment, please see my edited question. Thanks –  MYaseen208 Nov 10 '13 at 6:48
    
What happened there? (cs:1, cs:7)? Should probably read (axis cs: 1, 7), too. That the line does not pass until the y axis is due the enlarge x limits style. You can however just give a smaller x value on the domain, say domain:-1:1.4. The plots are usually clipped. –  Qrrbrbirlbel Nov 10 '13 at 6:59
    
@Qrrbrbirlbel: I tried that too but did not work. –  MYaseen208 Nov 10 '13 at 7:00
1  
Ah, yes. You forgot at, i.e. at (axis cs: 1,7) and at (axis cs: 15, 25). In your case, you just name the nodes. The placement defaults to (0, 0) in this case. –  Qrrbrbirlbel Nov 10 '13 at 7:03

1 Answer 1

up vote 3 down vote accepted

The coordinate system from TikZ is of no use when pgfplots does its works. It redefines the x and the y vectors to its own usage. You should use the axis cs: and the rel axis cs: coordinate system instead to refer to coordinates from the axes. (There are other cs:s like the xticklabel cs: used in your code and helpful (pseudo-)nodes that refer to the axes, the labels or the plot.)

I have added an insert path style for your dashed markers that takes two arguments: the x value and the y value. A black dot is automatically drawn at the (<x>, <y>) coordinate with the filled circular node. So that these touch the axes, I have used the rel axis cs: with which one can access coordinate relatively to the axes. The lower-left corner is (rel axis cs: 0, 0), the upper-right is (rel axis cs: 1, 1).

I have used the same for your nodes with (rel axis cs: 1, 0) to refer to the lower-right corner. I’e just placed these description in the corner because I didn’t know where else to place them. There might as well be used in a legend (pgfplots can do this, too).

The enlarge x limits styles makes the axes a little bit longer than the values from the domain would suggest. This is also the reason why the straight line doesn’t touch the y axis. I have fixed this with a manual set xmin=0 in the axis options and a domain that starts at -1.

Code

\documentclass[tikz]{standalone}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}[ % Define Normal Probability Function
  declare function={normal(\x,\m,\s) = 1/(2*\s*sqrt(pi))*exp(-(\x-\m)^2/(2*\s^2));},
  xy marker/.style 2 args={
    insert path={
      ({rel axis cs: 0,0}-|{axis cs: #1,0})
        |- node[shape=circle, fill, inner sep=+0pt, minimum size=+4pt]{}
      ({rel axis cs: 0,0}|-{axis cs: 0,#2})}}]

\begin{axis}[
    no markers, xmin=0, axis lines=left,
    samples=100,
    enlarge x limits=true,  enlarge y limits=true,
    xtick={0, 0.5, 1}, ytick={3, 5, 7},
    xticklabels={$X_1$, $X_2$, $X_3$},
    yticklabels={$Y_1$, $Y_2$, $Y_3$},
    xlabel=$X$, xlabel style={at=(xticklabel cs:1), anchor=south},
    ylabel=$Y$, ylabel style={at=(yticklabel cs:1), rotate=-90, anchor=west},
  ]

\addplot[cyan!50!black, thick, domain=0:6] ({normal(x, 3, 1)+0.0},x);
\addplot[cyan!50!black, thick, domain=2:8] ({normal(x, 5, 1)+0.5},x);
\addplot[cyan!50!black, thick, domain=4:10] ({normal(x, 7, 1)+1},x);

\draw[dashed, xy marker/.list={{0}{3},{.5}{5},{1}{7}}];
\addplot [samples=2, domain=-1:1.4] {3+4*x};

\begin{scope}[nodes={align=center, fill=white, fill opacity=.7, text opacity=1}]
\node[rounded corners=+7pt, above left] (@)
    % rounded corners so that the node doesn't overlay the arrow tip form the x axes
  at (rel axis cs: 1, 0) {Distribution of \\$Y$ values \\given $X=x_3$};
\node[above left] at (@.north east) {$\mathbf{E}\left(Y|X_{i}\right)=\beta_{0}+\beta_{1}X$};
\end{scope}
\end{axis}
\end{tikzpicture}
\end{document}

Output

enter image description here

share|improve this answer
    
Thanks a lot for nice solution. –  MYaseen208 Nov 10 '13 at 7:36
    
What about 3D version? –  MYaseen208 Nov 10 '13 at 15:00

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.