# Graphing 3 function

I wish to graph this

This graph has 3 functions on it

t=x-r, r<0, x<0 x=r, r>1, x>1 (x-r)/(1-r), 0

My problem is I am wishing to stop the (x-r)/(1-r) lines at the point (1,1) as seen in my drawing below. Could anyone help me with this please?

Currently have this, also struggling with drawing the vertical lines.

\documentclass[border=2mm]{standalone}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
xmin=-1, xmax=1.5,
ymin=0, ymax=1.5,
axis lines=center,
axis on top=true,
domain=-2:3,
]
\end{axis}
\end{tikzpicture}
\end{document}

-
Please add a Minimal Working Example. – Peter Ebelsberger Feb 28 at 13:02
Welcome to TeX.SX! Please make your code compilable (if possible), or at least complete it with \documentclass{...}, the required \usepackage's, \begin{document}, and \end{document}. That may seem tedious to you, but think of the extra work it represents for TeX.SX users willing to give you a hand. Help them help you: remove that one hurdle between you and a solution to your problem. – Martin Schröder Feb 28 at 13:07

You have to add domain as an option to addplot. The vertical lines you can simply draw using the draw macro. By adding the axis cs prefix to a coordinate you can use the axis coordinate system.

\documentclass{article}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
xmin=-1, xmax=1.5,
ymin=0, ymax=1.5,
axis lines=center,
axis on top=true,
]
\draw[draw=blue, ultra thick] (axis cs:1,0) -- (axis cs:1,1.5);
\draw[draw=blue, ultra thick] (axis cs:1.5,0) -- (axis cs:1.5,1.5);
\end{axis}
\end{tikzpicture}
\end{document}


Output:

-
If you use a version of PGFplots >=1.11, then axis cs is not needed and automatically used. You have to make sure to add \pgfplotsset{compat=1.13} in the preamble (1.13 is the current PGFplots version). – JP-Ellis Feb 28 at 13:18
@JP-Ellis Very convenient. I did not know that. Thanks for making me aware of it. – Benjamin Feb 28 at 13:44

Here's yet another solution (wow, they came fast!)

In order to avoid repeating yourself, I've used \foreach to loop over values; however, it requires a little trick in certain circumstances due to the way things are expanded and parsed. Essentially, a temporary macro, \tmp, is created so that it contains (in the case of the first iteration):

\tmp = \addplot [green] (0.25, 0) -- (1, 1);


In order to get \i to become 0.25 but \addplot to remain as is, \edef is used which expands all macros it encounters within the definition. In order to prevent \addplot for being expanded though, \noexpand is added in front and \tmp becomes defined as above.

I also made the red lines stop at x=1, but you can easily change that by removing domain=-1:1.

Lastly, I use \pgfkeysvalueof{/pgfplots/ymax} in order to get the value of ymax so that the vertical lines are guaranteed to go to the top of the plot.

\documentclass[tikz]{standalone}

\usepackage{tikz}
\usepackage{pgfplots}
\pgfplotsset{compat=1.13}

\begin{document}
\begin{tikzpicture}
\begin{axis}[
xmin=-1,
xmax=1.5,
ymin=0,
ymax=1.5,
axis lines=center,
axis on top=true,
domain=-2:3,
]

\foreach \i in {0, 0.25, ..., 2} {
\addplot [red, domain=-1:1] {x + \i};
}
\foreach \i in {0.25, 0.50, 0.76} {
\edef\tmp{
\noexpand\draw [green] (\i, 0) -- (1, 1);
}
\tmp
}
\foreach \i in {1.00, 1.25, 1.50} {
\edef\tmp{
\noexpand\draw [blue] (\i, 0) -- (\i, \pgfkeysvalueof{/pgfplots/ymax});
}
\tmp
}
\end{axis}
\end{tikzpicture}
\end{document}


-

The domain for this specific line(s) can be set separately by adding, e.g., domain=.5:1 like this:

\documentclass [10pt] {article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.3}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
xmin=-1, xmax=1.5,
ymin=0, ymax=1.5,
axis lines=center,
axis on top=true,
domain=-2:3,
]