# Help to dual Y axis, dual stacked plot

The B means the block will be Blue, R means Red, Y means yellow, etc ... In plot, it will not be written, it represents only the colour of the plot.

MWE:

\documentclass{article}
\usepackage{pgfplots}
\usepackage{graphicx}
\usepackage{xcolor}

\begin{document}
\begin{figure}
\centering
\begin{tikzpicture}
\pgfplotsset{
%           scale only axis,
%           scaled x ticks=base 10:3,
%           xmin=0, xmax=0.06
symbolic x coords={40,60,80,100,120},
xtick=data,
xlabel=x-axis,
legend columns=-1,
legend style={draw=none},
legend to name=named,
}

\begin{axis}[
axis y line*=left,
%       ymin=0, ymax=80,
ylabel=y-axis 1,
ylabel near ticks,
ybar stacked, ymin=0,
%       ybar legend,
bar width=7mm,
legend style={at={(0.5,-0.2)},anchor=north},
%       legend entries={A,B},
ymajorgrids=true,
major grid style={dotted,black},
]
({40},15)
({60},25)
({80},35)
({100},15)
({120},10)
};\label{A1nm}
({40},10)
({60},35)
({80},30)
({100},25)
({120},10)
};\label{plot_two}
\end{axis}

\begin{axis}[
set layers,axis background,
axis y line*=right,
ymode=log,
%       axis x line=none,
%       ymin=0, ymax=100,
ylabel=y-axis 2,
ylabel near ticks,
yticklabel pos=right,
%       legend entries={A,B,time},
%grid=both,hide x axis,
%major grid style={dotted,black},
ymajorgrids=true,
major grid style={dashed, gray}
]

coordinates{
({40},1)
({60},2)
({80},35)
({100},1)
({120},10)
\end{axis}
\end{tikzpicture}
\\
\ref{named}
\end{figure}
\end{document}

• What exactly is your problem? Mar 21, 2018 at 13:39
• I don't know, how to draw like the figure and legend shown. Mar 21, 2018 at 13:40
• Usually a good idea to say specifically where you're stuck. Agreed the legend isn't entirely straightforward, but you linked to a solution for the bars yourself ... Mar 21, 2018 at 13:57

For the bars, you can use the solution you link to yourself (pgfplots: Multiple (shifted) stacked plots in one diagram) almost directly, you just need to change the x-coordinates in the definition of \resetstackedplots so they match your symbolic coordinates. Then just do exactly the same thing: first two (or more) plots with a negative bar shift, then \resetstackedplots, then more plots this time with a positive bar shift. The size of the shift that is required depends on the bar width.

For the legend, the most straightforward method might be to not use a pgfplots legend at all, but build it yourself, by adding a \label after each plot, and \refing these in a tabular.

\documentclass{article}
\usepackage{pgfplotstable} % loads graphicx, and xcolor
\makeatletter
\newcommand\resetstackedplots[1]{% argument is comma separated list of x-values
\pgfplots@stacked@isfirstplottrue
% transpose to get column instead of row
\pgfplotstabletranspose{\tmpTab}{\tmpTab}
% empty plot
% clear table, don't need it
\pgfplotstableclear{\tmpTab}
}
\makeatother

\begin{document}
\begin{figure}
\centering
\begin{tikzpicture}
\pgfplotsset{
symbolic x coords={40,60,80,100,120},
xtick=data,
width=0.75\linewidth
}

\begin{axis}[
axis y line*=left,
ylabel=y-axis 1,
ylabel near ticks,
ybar stacked,
ymin=0,
bar width=4mm, % reduced bar width
ymajorgrids=true,
major grid style={dotted,black},
xlabel=x-axis % only need one x-label, so move here
]
bar shift=-3mm, % added this -- the value depends on the bar shift
fill={rgb:red,4;green,2;blue,1}, % changed yellow to blue
draw=none,
area legend
] coordinates {
(40,15)
(60,25)
({80},35)
({100},15)
({120},10)
};\label{A1nm}

% added negative bar shift here as well
\addplot [bar shift=-3mm,fill=yellow,draw=none,area legend] coordinates {
(40,10)
(60,35)
(80,30)
(100,25)
(120,10)
};\label{plot_two}

\resetstackedplots{40,60,80,100,120}
% positive bar shift for the next two, with different colours
% added a \label after each plot

\addplot [bar shift=3mm,fill={rgb:red,40;green,4;blue,50},draw=none,area legend] coordinates {
(40,10)
(60,20)
(80,30)
(100,10)
(120,20)
};\label{three}

\addplot [bar shift=3mm,fill=red!30,draw=none,area legend] coordinates {
(40,13)
(60,39)
(80,32)
(100,35)
(120,13)
};\label{four}

\end{axis}

\begin{axis}[
set layers,
axis background,
axis y line*=right,
axis x line=none, % hide x-axis
ymode=log,
ylabel=y-axis 2,
ylabel near ticks,
yticklabel pos=right,
ymajorgrids=true,
major grid style={dashed, gray}
]

coordinates{
({40},1)
({60},2)
({80},35)
({100},1)
({120},10)
};\label{C} % just a label here

coordinates{
({40},2)
({60},3)
({80},45)
({100},2)
({120},20)
};
\label{D}

\end{axis}
\end{tikzpicture}

% now generate the legend, using a normal tabular
\begin{tabular}{lccc}
& A & B & C \\
S1 & \ref{A1nm} & \ref{plot_two} & \ref{C} \\
S2 & \ref{three} & \ref{four} & \ref{D}
\end{tabular}

\end{figure}
\end{document}

• What do forget plot do? Mar 21, 2018 at 16:24
• @Mr.EU The pgfplots manual (useful reference for questions like that) says "Allows to include plots which are not remembered for legend entries, which do not increase the number of plots and which are not considered for cycle lists." Mar 21, 2018 at 17:03
• Can we generalize \resetstackedplots, as I have multiple plots, where X axis are not same? Mar 21, 2018 at 17:05
• @Mr.EU Well, you can for example make the coordinate list be an argument to the macro, see edit. Mar 21, 2018 at 17:26
• @Mr.EU Updated again, now you can supply a comma separated list of x-coordinates as an argument to the macro. Mar 21, 2018 at 17:35