\documentclass{article}
\usepackage{pgfplots, tikz}
\usepackage{adjustbox} % table scale
\begin{document}
\begin{figure}
\centering
\begin{adjustbox}{max width=.75\textwidth}
\begin{tikzpicture}
\pgfplotsset{
symbolic x coords={40,60,80,100,120},
xtick=data,
legend columns=-1,
legend style={draw=none},
legend to name=named,
}
\begin{axis}[
axis y line*=left,
xlabel=x-axis,
ylabel=y-axis 1,
ybar stacked, ymin=0,
bar width=7mm,
legend entries={a,b},
]
\addplot [fill=blue] coordinates {
({40},15)
({60},25)
({80},35)
({100},15)
({120},10)
};
\addplot [fill=red] coordinates {
({40},10)
({60},35)
({80},30)
({100},25)
({120},10)
};
\end{axis}
\begin{axis}[
axis y line*=right,
ylabel=y-axis 2, legend entries={time},
]
\addplot[smooth,mark=*,black]
coordinates{
({40},15)
({60},25)
({80},35)
({100},15)
({120},10)
};
\end{axis}
\end{tikzpicture}
\end{adjustbox}
\\
Sample: \ref{named}
\end{figure}
\end{document}
Problems:
y-axis 2 label not appearing properly (appears left).
Legends not showing for stacked plots.
The text "Sample" and the legend seems to not appearing horizontally in the same line.


