I have the following MWE:
\documentclass{article}
\usepackage[dvipsnames]{xcolor}
\usepackage{pgf,pgfplots,pgfplotstable}
\pgfplotsset{compat=newest}
\usepgfplotslibrary{colormaps}
\usepgfplotslibrary{groupplots}
\begin{filecontents}{data1.dat}
t1 a1 t2 a2 t3 a3
0.142 1.000 0.154 1.000 0.139 0.995
0.284 1.000 0.308 1.000 0.279 0.990
0.426 1.000 0.461 1.000 0.418 0.989
0.569 1.000 0.615 1.000 0.558 0.989
0.711 1.000 0.769 1.000 0.697 0.989
0.853 1.000 0.923 1.000 0.836 0.989
0.995 1.000 1.077 1.000 0.976 0.989
1.137 1.000 1.230 1.000 1.115 0.989
1.279 1.000 1.384 1.000 1.255 0.989
1.422 0.995 1.538 1.000 1.394 0.986
1.564 0.990 1.692 1.000 1.534 0.981
\end{filecontents}
\begin{filecontents}{data2.dat}
t1 a1 t4 a4 t3 a3
3.554 0.920 3.845 0.950 3.485 0.913
3.696 0.915 3.999 0.945 3.625 0.908
3.838 0.910 4.153 0.939 3.764 0.903
3.980 0.905 4.306 0.934 3.904 0.898
4.123 0.900 4.460 0.929 4.043 0.893
4.265 0.895 4.614 0.923 4.182 0.888
4.407 0.890 4.768 0.918 4.322 0.883
4.549 0.885 4.922 0.912 4.461 0.878
4.691 0.880 5.075 0.907 4.601 0.873
4.833 0.875 5.229 0.902 4.740 0.868
4.976 0.870 5.383 0.896 4.880 0.864
5.118 0.865 5.537 0.891 5.019 0.859
5.260 0.860 5.691 0.885 5.158 0.854
\end{filecontents}
\begin{document}
\begin{figure}[htbp]
\centering
\begin{tikzpicture}[scale=0.9]
\begin{groupplot}[group style={
group name=rzrxcomp,
group size=1 by 2,
xlabels at=edge bottom,
ylabels at=edge left,
vertical sep=4em
},
xmin = 0, xmax = 2,
ymin = 0.8, ymax = 1,
xtick distance = 0.2,
ytick distance = 0.2,
xlabel={$time;[s]$},
ylabel={$mass$},
grid = both,
grid style = {dotted},
minor tick num = 1,
major grid style = {lightgray!75},
minor grid style = {lightgray!75},
width = 0.85\textwidth,
height = 0.50\textwidth,
%scale only axis,
%legend cell align = {left},
%legend pos = north east
legend style={at={(0.5,-0.3)},
anchor=north,legend columns=-1},
legend cell align = {left},
]
\nextgroupplot
\addplot[smooth, black, very thick] table [x = {t1}, y = {a1}] {data1.dat};
\addplot[smooth, Lavender, very thick] table [x = {t2}, y = {a2}] {data1.dat};
\addplot[smooth, Violet, very thick] table [x = {t3}, y = {a3}] {data1.dat};
\nextgroupplot[legend entries={n1,n2,n3,n4}]
\addlegendimage{black, very thick, no markers}
\addlegendimage{Lavender, very thick, no markers}
\addlegendimage{Violet, very thick, no markers}
\addlegendimage{BurntOrange, dotted, very thick, no markers}
\addplot[smooth, black, very thick] table [x = {t1}, y = {a1}] {data2.dat};
\addplot[smooth, BurntOrange, dotted, very thick] table [x = {t4}, y = {a4}] {data2.dat};
\addplot[smooth, Violet, very thick] table [x = {t3}, y = {a3}] {data2.dat};
%\legend{
%n1,
%n2,
%n3,
%}
\end{groupplot}
\end{tikzpicture}
\end{figure}
\end{document}
Now imagine that we consider: (t1,a1)=n1, (t2,a2)=n2, (t3,a3)=n3, (t4,a4)=n4.
Is there a way to add the second column-set of values in data2.dat, i.e. (t4, a4), as n4 inside the legend? The issue is that I don't know how to add inside the legend n4 without messing up with n2, which is in the same position of n4 but in the first \groupplot. I need to keep one single legend, highlighting the four different plots with the related different colors.
UPDATE
Following @TorbjornT suggestion, I've used the \addlegendimage command in order to deal with plots' order and names in one single legend, and it works fine. The MWE has been updated with the new part. Now I have another question, how can I make solid tick, very tick... lines inside the generated legend? The \addlegendimage command doesn't seem to recognize or accept those style features.
\addlegendimagehere. (Finally, please test your example :) You forgot to close thefigure,groupplotandtikzpictureenvironment, and withoutdvipsnamestheLavendercolour is undefined.) – Torbjørn T. Jun 24 '21 at 21:50\addlegendimage, for the plot that is missing from that axis. I don't understand what you mean by the new question though, ticks have nothing to do with legends. – Torbjørn T. Jun 25 '21 at 14:23\legend{n1,n2,n3}I was able to inherit all the features of the related plots, so for example, if n1 plot was built with a solid ultra thick line, the same thickness was reported into the legend. If I use\addlegendimageI can set the color, but the thickness is not eligible, and the command set just a standard value. So the final result is lacking in graphical correlation @TorbjørnT. – Catarella Jun 25 '21 at 14:31