[Tex/LaTex] adding single points to bar figure in pgfplots

bar chartpgfplotstikz-pgf

I have a bar plot, in which I would like to add some single points overlayed on the bars. Those points are in another scale compared to the error bars. Below, I'm illustrating those single points in green. Note that, I would like also to add an entry in the figure's legend referring to those points. Ideally the points should be centered in the middle of each pair of bars.
I was having some difficulties in changing the range of the second y-axis (units), it's displayed in the range [0,1] but would like to have it ideally in the range [1,5]. Is it also possible to add a label for each single point? Any suggestions and improvements on the latex code are welcom 😉
enter image description here

\documentclass[a4paper]{article}
\usepackage{pgfplots}
\usepackage{pgfplotstable}
\pgfplotsset{compat=newest}

\begin{document}

\begin{tikzpicture}

\node [align=center, font=\small, rotate=45,
text width=2.15cm, inner sep=0.25cm] at (0.6cm, -0.65cm) {\textsc{year 1}};

\node [align=center, font=\small, rotate=45,
text width=2.15cm, inner sep=0.25cm] at (2.90cm, -0.65cm) {\textsc{year 2}};

\node [align=center,font=\small, rotate=45,
text width=2.15cm, inner sep=0.25cm] at (5.30cm, -0.73cm) {\textsc{year 3}};

\begin{axis}[
    title style={align=center, yshift=-0.35em},
    ybar,
    ymin={60}, ymax={93.5},
    width={8cm}, height={5cm},
    bar width={7pt},
    ticks=both,
    ytick={60,70,80,90,100.0},
    ylabel style={yshift=-0.65em},
    ylabel={Percent},
    ymajorgrids,
    nodes near coords = \rotatebox{90}{{\pgfmathprintnumber[fixed zerofill, precision=2]{\pgfplotspointmeta}}},
    nodes near coords align={vertical},
    every node near coord/.append style={font=\small, yshift=0.25mm},
    xtick = data,
    table/header=false,
    table/row sep=\\,
    xticklabels={},
    enlarge y limits={value=0.2,upper},
    legend style={font=\tiny},
    %legend pos=north west
    %legend style={at={(0.5, 1.20)}, anchor=north, legend columns=2},
    legend style={at={(0.5, -0.52)}, anchor=north, legend columns=2},
]
\addplot table[x expr=\coordindex,y index=0]{89.17\\71.71\\81.41\\};
\addplot table[x expr=\coordindex,y index=0]{70.60\\64.11\\84.92\\};
\draw (axis cs:0.52,0) -- ({axis cs:0.52,0}|-{rel axis cs:0.5,1});
\draw (axis cs:1.49,0) -- ({axis cs:1.49,0}|-{rel axis cs:0.5,1});
\draw (axis cs:2.50,0) -- ({axis cs:2.50,0}|-{rel axis cs:0.5,1});
\legend{\textsc{Prod 1}\hspace*{8pt}, \textsc{Prod 2} }
\end{axis}

\begin{axis}[
    axis y line*={right},
    axis x line={none},
    ymin={1}, ymax={5},
    width={8cm}, height={5cm},
    ylabel style={yshift=0.60em},
    ylabel={units},
    ytick={1,2,3,4,5}
]
% add points here?
\end{axis}

\end{tikzpicture}
\end{document}

Best Answer

You can plot those circles like

\begin{axis}[
    axis y line*={right},
    axis x line={none},
    ymin={1}, ymax={5},
    width={8cm}, height={5cm},
    ylabel style={xshift=0.60em},
    ylabel={units},
]
\addplot[only marks,mark=*,mark size=3pt,green,
         nodes near coords = \rotatebox{90}{{\pgfmathprintnumber[fixed zerofill,
                                    precision=2]{\pgfplotspointmeta}}},
        nodes near coords align={vertical},
        point meta=y,
        every node near coord/.append style={font=\small, yshift=0.25mm},
        ]  coordinates {
    (1,3.5) (2,4) (3,2)
};
\end{axis}

and add

\addlegendimage{draw=green,fill=green!30}
\addlegendentry{\textsc{Legend}}

to add the extra legend. You have to make legend columns=3 additionally.

Full code:

\documentclass[a4paper]{article}
\usepackage{pgfplots}
\usepackage{pgfplotstable}
\pgfplotsset{compat=1.11}

\begin{document}

\begin{tikzpicture}

\node [align=center, font=\small, rotate=45,
text width=2.15cm, inner sep=0.25cm] at (0.6cm, -0.65cm) {\textsc{year 1}};

\node [align=center, font=\small, rotate=45,
text width=2.15cm, inner sep=0.25cm] at (2.90cm, -0.65cm) {\textsc{year 2}};

\node [align=center,font=\small, rotate=45,
text width=2.15cm, inner sep=0.25cm] at (5.30cm, -0.73cm) {\textsc{year 3}};

\begin{axis}[
    title style={align=center, yshift=-0.35em},
    ybar,
    ymin={60}, ymax={93.5},
    width={8cm}, height={5cm},
    bar width={7pt},
    ticks=both,
    ytick={60,70,80,90,100.0},
    ylabel style={yshift=-0.65em},
    ylabel={Percent},
    ymajorgrids,
    nodes near coords = \rotatebox{90}{{\pgfmathprintnumber[fixed zerofill, precision=2]{\pgfplotspointmeta}}},
    nodes near coords align={vertical},
    every node near coord/.append style={font=\small, yshift=0.25mm},
    xtick = data,
    table/header=false,
    table/row sep=\\,
    xticklabels={},
    enlarge y limits={value=0.2,upper},
    legend style={font=\tiny},
    %legend pos=north west
    %legend style={at={(0.5, 1.20)}, anchor=north, legend columns=2},
    legend style={at={(0.5, -0.52)}, anchor=north, legend columns=3},
]
\addplot table[x expr=\coordindex,y index=0]{89.17\\71.71\\81.41\\};
\addplot table[x expr=\coordindex,y index=0]{70.60\\64.11\\84.92\\};
\draw (axis cs:0.52,0) -- ({axis cs:0.52,0}|-{rel axis cs:0.5,1});
\draw (axis cs:1.49,0) -- ({axis cs:1.49,0}|-{rel axis cs:0.5,1});
\draw (axis cs:2.50,0) -- ({axis cs:2.50,0}|-{rel axis cs:0.5,1});
\legend{\textsc{Prod 1}\hspace*{8pt}, \textsc{Prod 2}}
\addlegendimage{draw=green,fill=green!30}
\addlegendentry{\textsc{Legend}}
\end{axis}

\begin{axis}[
    axis y line*={right},
    axis x line={none},
    ymin={1}, ymax={5},
    width={8cm}, height={5cm},
    ylabel style={xshift=0.60em},
    ylabel={units},
]
\addplot[only marks,mark=*,mark size=3pt,green,
         nodes near coords = \rotatebox{90}{{\pgfmathprintnumber[fixed zerofill,
                                    precision=2]{\pgfplotspointmeta}}},
        nodes near coords align={vertical},
        point meta=y,
        every node near coord/.append style={font=\small, yshift=0.25mm},
        ]  coordinates {
    (1,3.5) (2,4) (3,2)
};
\end{axis}

\end{tikzpicture}
\end{document}

enter image description here

If you want to show circles only at some points, better draw them.

\documentclass[a4paper]{article}
\usepackage{pgfplots}
\usepackage{pgfplotstable}
\pgfplotsset{compat=1.11}

\begin{document}

\begin{tikzpicture}

\node [align=center, font=\small, rotate=45,
text width=2.15cm, inner sep=0.25cm] at (0.6cm, -0.65cm) {\textsc{year 1}};

\node [align=center, font=\small, rotate=45,
text width=2.15cm, inner sep=0.25cm] at (2.90cm, -0.65cm) {\textsc{year 2}};

\node [align=center,font=\small, rotate=45,
text width=2.15cm, inner sep=0.25cm] at (5.30cm, -0.73cm) {\textsc{year 3}};

\begin{axis}[
    title style={align=center, yshift=-0.35em},
    ybar,
    ymin={60}, ymax={93.5},
    width={8cm}, height={5cm},
    bar width={7pt},
    ticks=both,
    ytick={60,70,80,90,100.0},
    ylabel style={yshift=-0.65em},
    ylabel={Percent},
    ymajorgrids,
    nodes near coords = \rotatebox{90}{{\pgfmathprintnumber[fixed zerofill, precision=2]{\pgfplotspointmeta}}},
    nodes near coords align={vertical},
    every node near coord/.append style={font=\small, yshift=0.25mm},
    xtick = data,
    table/header=false,
    table/row sep=\\,
    xticklabels={},
    enlarge y limits={value=0.2,upper},
    legend style={font=\tiny},
    %legend pos=north west
    %legend style={at={(0.5, 1.20)}, anchor=north, legend columns=2},
    legend style={at={(0.5, -0.52)}, anchor=north, legend columns=3},
]
\addplot table[x expr=\coordindex,y index=0]{89.17\\71.71\\81.41\\};
\addplot table[x expr=\coordindex,y index=0]{70.60\\64.11\\84.92\\};
\draw (axis cs:0.52,0) -- ({axis cs:0.52,0}|-{rel axis cs:0.5,1});
\draw (axis cs:1.49,0) -- ({axis cs:1.49,0}|-{rel axis cs:0.5,1});
\draw (axis cs:2.50,0) -- ({axis cs:2.50,0}|-{rel axis cs:0.5,1});
%\fill[green] (axis cs:0,85) circle(3pt) node [right,rotate=90] {85};
\fill[green] (axis cs:1,90) circle(3pt) node [right,rotate=90] {90};
\fill[green] (axis cs:2,70) circle(3pt) node [right,rotate=90] {70};
\legend{\textsc{Prod 1}\hspace*{8pt}, \textsc{Prod 2}}
\addlegendimage{draw=green,fill=green!30}
\addlegendentry{\textsc{Legend}}
\end{axis}

\begin{axis}[
    axis y line*={right},
    axis x line={none},
    ymin={1}, ymax={5},
    xmin=0,xmax=5,
    width={8cm}, height={5cm},
    ylabel style={xshift=0.60em},
    ylabel={units},
]
\end{axis}

\end{tikzpicture}
\end{document}

enter image description here