4

I'm doing a grouped plot using groupplot. I liked the graphic but I can't put the legend correctly. Here is the code:

\documentclass[12pt,a4paper]{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage{makeidx}
\usepackage{graphicx}
\usepackage{times}
\usepackage[brazil]{babel}
\usepackage{tikz}
\usepackage{pgfplots}
\usepgfplotslibrary{groupplots}
\usepackage{filecontents}

\begin{document}

\begin{filecontents*}{dose_sulfato.csv}
Dosagem SA, Turbidez,   Cor,    Potencial,  Remocao T,      Remocao C,
7.8,        2.75,       35,     -5.43,      68.85617214,    79.76878613
8.4,        1.78,       39,     -3.69,      79.8414496,     77.4566474
9,          1.57,       30,     -5.04,      82.21970555,    82.65895954
9.6,        1.3,        27,     -0.746,     85.27746319,    84.39306358
10.2,       1.67,       28,     -2.71,      81.08720272,    83.8150289
\end{filecontents*}
%
\begin{filecontents*}{dose_quitosana.csv}
Dosagem Q,  Turbidez,   Cor,    Potencial,  Remocao T,      Remocao C
0.5,        1.32,       22,     -6.61,      85.05096263,    87.28323699
0.7,        0.7,        10,     -2.48,      92.07248018,    94.21965318
\end{filecontents*}

\begin{tikzpicture}
\begin{groupplot}[
group style={group size=2 by 2},
width=0.4\textwidth,
xtick=data,
]
\nextgroupplot[title=Turbidez]
\addplot table[x=Dosagem SA, y=Remocao T, col sep=comma,] {dose_sulfato.csv};
\nextgroupplot[title=Cor Aparente]
\addplot table[x=Dosagem SA, y=Remocao C, col sep=comma,] {dose_sulfato.csv};
\nextgroupplot
\addplot[red, mark=*] table[x=Dosagem Q, y=Remocao T, col sep=comma,] {dose_quitosana.csv};
\nextgroupplot
\addplot[red, mark=*] table[x=Dosagem Q, y=Remocao C, col sep=comma,] {dose_quitosana.csv};    
\end{groupplot}
\node[anchor=north] (title-x) at ($(group c1r2.south east)!0.5!(group c2r2.south west)-(0,0.5cm)$) {Dosagem de Coagulante $(mg/L)$};
\node[anchor=south, rotate=90] (title-y) at ($(group c1r1.south west)!0.5!(group c1r2.north west)-(0.85,0cm)$) {Remoção (\%)};
\end{tikzpicture}

\end{document}

I'm getting:

Groupp1

But I want to get something like that

enter image description here

I don't know how to put the legend that way. Anyone can help me?

EDIT 1: The legend can be placed anywhere in the graphic

1

1 Answer 1

5

You can use legend to name as John Kormylo suggests in a comment, but there is one extra step that is needed. I don't think you can collect legend entries from multiple axes, so instead you use \addlegendimage{<plot options>} which doesn't plot anything, but makes a legend entry. So for example, modify the first groupplot to read

\nextgroupplot[title=Turbidez,legend to name={CommonLegend},legend style={legend columns=2}]
\addplot table[x=Dosagem SA, y=Remocao T, col sep=comma,] {dose_sulfato.csv};
\addlegendimage{red, mark=*}
\addlegendentry{Sulfato}
\addlegendentry{Quitosana}

Note the new options. To print the legend, use \ref{CommonLegend}, which you can add in a node positioned wherever you like. I placed it between the two rows of plots with

\path (group c1r2.north east) -- node[above]{\ref{CommonLegend}} (group c2r2.north west);

To make space for it I did as John Kormylo mentioned, and increased the vertical space between axes with vertical sep=1.5cm in the group style.

Finally, an unrelated note: units should generally not be typeset in italics, so I removed the dollar signs around mg/L.

enter image description here

\documentclass[12pt,a4paper]{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{times}
\usepackage{pgfplots}
\usepgfplotslibrary{groupplots}
\usepackage{filecontents}

\begin{document}

\begin{filecontents*}{dose_sulfato.csv}
Dosagem SA, Turbidez,   Cor,    Potencial,  Remocao T,      Remocao C,
7.8,        2.75,       35,     -5.43,      68.85617214,    79.76878613
8.4,        1.78,       39,     -3.69,      79.8414496,     77.4566474
9,          1.57,       30,     -5.04,      82.21970555,    82.65895954
9.6,        1.3,        27,     -0.746,     85.27746319,    84.39306358
10.2,       1.67,       28,     -2.71,      81.08720272,    83.8150289
\end{filecontents*}
%
\begin{filecontents*}{dose_quitosana.csv}
Dosagem Q,  Turbidez,   Cor,    Potencial,  Remocao T,      Remocao C
0.5,        1.32,       22,     -6.61,      85.05096263,    87.28323699
0.7,        0.7,        10,     -2.48,      92.07248018,    94.21965318
\end{filecontents*}

\begin{tikzpicture}
\begin{groupplot}[
group style={group size=2 by 2,vertical sep=1.5cm},
width=0.4\textwidth,
xtick=data,
]
\nextgroupplot[title=Turbidez,legend to name={CommonLegend},legend style={legend columns=2}]
\addplot table[x=Dosagem SA, y=Remocao T, col sep=comma,] {dose_sulfato.csv};
\addlegendimage{red, mark=*}
\addlegendentry{Sulfato}
\addlegendentry{Quitosana}

\nextgroupplot[title=Cor Aparente]
\addplot table[x=Dosagem SA, y=Remocao C, col sep=comma,] {dose_sulfato.csv};
\nextgroupplot
\addplot[red, mark=*] table[x=Dosagem Q, y=Remocao T, col sep=comma,] {dose_quitosana.csv};
\nextgroupplot
\addplot[red, mark=*] table[x=Dosagem Q, y=Remocao C, col sep=comma,] {dose_quitosana.csv};    
\end{groupplot}
\node[anchor=north] (title-x) at ($(group c1r2.south east)!0.5!(group c2r2.south west)-(0,0.5cm)$) {Dosagem de Coagulante (mg/L)};
\node[anchor=south, rotate=90] (title-y) at ($(group c1r1.south west)!0.5!(group c1r2.north west)-(0.85,0cm)$) {Remoção (\%)};

\path (group c1r2.north east) -- node[above]{\ref{CommonLegend}} (group c2r2.north west);
\end{tikzpicture}

\end{document}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .