[Tex/LaTex] How to draw an additional line over an existing TikZ figure

pgfplotstikz-pgf

\documentclass[border=5mm] {standalone}
\usepackage{pgfplots, pgfplotstable}


\begin{document}

\begin{tikzpicture}
\pgfplotstableread{ % Read the data into a table macro
Label   First   Second  Third
10      0.1     0.3     0.3
20      0.2     0.3     0.3
30      0.3     0.4     0.5
40      0.3     0.5     0.8
160     0.5     0.9     1.5
}\datatable

\begin{axis}[
    xbar stacked,   % Stacked horizontal bars
    xmin=0,         % Start x axis at 0
    ytick=data,     % Use as many tick labels as y coordinates
    yticklabels from table={\datatable}{Label}  % Get the labels from the Label column of the \datatable
]
\addplot [fill=yellow] table [x=First, y expr=\coordindex] {\datatable};    % Plot the "First" column against the data index
\addplot [fill=green!70!blue]table [x=Second, y expr=\coordindex] {\datatable};
\addplot [fill=red!80!yellow] table [x=Third, y expr=\coordindex] {\datatable};
\end{axis}
\end{tikzpicture}

\end{document}

enter image description here

Now how do I draw a vertical black line parallel to the Y axis going through 1 on x axis?
And how do I put a label on this line?

Best Answer

You can use Joseph's approach from How can I add a zero line to a plot?: By setting extra x ticks={1}, you can define an additional tick that can be formatted differently from the others. By setting extra x tick style={xmajorgrids=true}, you can switch on the grid line for this tick, resulting in a vertical line:

\documentclass[border=5mm] {standalone}
\usepackage{pgfplots, pgfplotstable}
\pgfplotsset{compat=newest}

\begin{document}

\begin{tikzpicture}
\pgfplotstableread{ % Read the data into a table macro
Label   First   Second  Third
10      0.1     0.3     0.3
20      0.2     0.3     0.3
30      0.3     0.4     0.5
40      0.3     0.5     0.8
160     0.5     0.9     1.5
}\datatable

\begin{axis}[
    xbar stacked,   % Stacked horizontal bars
    xmin=0,         % Start x axis at 0
    ytick=data,     % Use as many tick labels as y coordinates
    yticklabels from table={\datatable}{Label},  % Get the labels from the Label column of the \datatable
    extra x ticks={1},  % Add an extra tick at position x=1
    extra x tick style={    % Set styles that only apply to the extra tick
            xticklabel pos=right,   % Put the label on the right ( = top) side of the plot
            xticklabels={Special!}, % Set the label text
            xmajorgrids=true            % Draw grid line
        }
]
\addplot [fill=yellow] table [x=First, y expr=\coordindex] {\datatable};    % Plot the "First" column against the data index
\addplot [fill=green!70!blue]table [x=Second, y expr=\coordindex] {\datatable};
\addplot [fill=red!80!yellow] table [x=Third, y expr=\coordindex] {\datatable};
\end{axis}
\end{tikzpicture}

\end{document}