[Tex/LaTex] PGF barplot with two y axis

bar chartpgfplotstikz-pgf

I added a new axis to my barplot and that has a different scale as the first one.
The second axis is shifted and does not have the same height as the first one.
What do I need to change so that the right Y-Axis is not shifted and the line goes from the left to the right axis?
My Code so far:

\documentclass[12pt,tikz, border=10pt]{standalone}

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

\begin{document}
\begin{tikzpicture}
    \begin{axis}[
        width  = \textwidth,
        height = 9cm,
        major x tick style = transparent,
        ybar=2*\pgflinewidth,
        bar width=14pt,
        ymajorgrids = true,
        ylabel = {Time},
        xlabel = {Subject},
        symbolic x coords={1,2,3,4,5},
        xtick = data,
            scaled y ticks = false,
            enlarge x limits=0.25,
            ymin=0,
        legend columns=2,
        legend cell align=left,
        legend style={
                at={(0.5,-0.2)},
                anchor=north,
                column sep=1ex
        }
    ]
        \addplot[style={blue,fill=blue,mark=none}]
           coordinates {(1, 09.06) (2, 09.52) (3, 12.20) (4, 12.10) (5, 14.05)};

        \addplot[style={red,fill=red,mark=none}]
             coordinates {(1, 19.22) (2, 21.29) (3, 21.58) (4, 23.27) (5, 37.03)};

        \legend{Data1, Data2}
    \end{axis}

    \begin{axis}[
    scale only axis,
    axis y line*=right,
    axis x line=none,
    width = \textwidth,
    height = 9cm,
    major x tick style = transparent,
    ybar=2*\pgflinewidth,
    enlarge x limits=0.25,
    bar width=14pt,
    ylabel = {Advantage},
    xmin=0, xmax=6,
    ymin=0, ymax=4,
    legend columns=2,
    legend cell align=left,
    legend style={
        at={(0.5,-0.2)},
        anchor=north,
        column sep=1ex
    }
    ]
    \addplot[style={green,fill=green,mark=none}]
    coordinates {(1, 2) (2, 2) (3, 2) (4, 2) (5,2)};

    \addplot[green,sharp plot,update limits=false] coordinates {(0,1.5) (6,1.5)} node[above] at (axis cs:3,1.5) {1.5};


    \legend{advantage}
    \end{axis}

\end{tikzpicture}
\end{document}

Best Answer

You have to use same attributes to the y ticks in both the axis like

        scaled y ticks = false,
        ymin=-0.1,ymax=40,

and remove scale only axis, too. Further, you have to plot all bars inside the same axis environment so that they are properly spaced. I have commented out the lines that are not necessary and we keep the second axis environment just for the right y axis. Also, I have drawn the second x axis and used axis on top so that bars don't cover the x axis. So many changes that I can't write on all of them. Please see the code.

\documentclass[12pt, border=10pt]{standalone}

\usepackage{pgfplots}
\pgfplotsset{compat=1.12}

\begin{document}
\begin{tikzpicture}
    \begin{axis}[
        width  = \textwidth,
        axis y line*=left,
        axis x line=bottom,
        height = 9cm,
        major x tick style = transparent,
        %axis on top,
        ybar=5*\pgflinewidth,
        bar width=14pt,
        ymajorgrids = true,
        ylabel = {Time},
        xlabel = {Subject},
        symbolic x coords={1,2,3,4,5},
        xtick = data,
            scaled y ticks = false,
            enlarge x limits=0.25,
            axis line style={-},
            ymin=-0.1,ymax=40,
        legend columns=2,
        legend cell align=left,
        legend style={
                at={(0.5,-0.2)},
                anchor=north,
                column sep=1ex
        }
    ]
        \addplot[style={blue,fill=blue,mark=none}]
           coordinates {(1, 09.06) (2, 09.52) (3, 12.20) (4, 12.10) (5, 14.05)};

        \addplot[style={red,fill=red,mark=none}]
             coordinates {(1, 19.22) (2, 21.29) (3, 21.58) (4, 23.27) (5, 37.03)};
        \addplot[style={green,fill=green,mark=none}]
             coordinates {(1, 2) (2, 2) (3, 2) (4, 2) (5,2)};
        \legend{Data1, Data2,Advantage}
    \end{axis}
    \begin{axis}[
    %scale only axis,
    axis y line*=right,
    axis x line=none,%axis on top,
    %xtick=\empty,
    width = \textwidth,
    height = 9cm,
    %major x tick style = transparent,
    %ybar=5*\pgflinewidth,
    enlarge x limits=0.25,
    axis line style={-},
    %bar width=14pt,
    ylabel = {Advantage},
    xmin=0, xmax=6,
    scaled y ticks = false,
    ymin=-0.1, ymax=40,
    %legend columns=2,
%    legend cell align=left,
%    legend style={
%        at={(0.5,-0.2)},
%        anchor=north,
%        column sep=1ex
%    }
    ]
    \addplot[green,sharp plot,update limits=false] coordinates {(0,1.5) (6,1.5)} node[above,pos=0.5] {1.5};
    %\legend{advantage}
    \end{axis}

\end{tikzpicture}
\end{document}

enter image description here