[Tex/LaTex] How to properly arrange group plot for bar chart using pgfplot

groupplotspgfplotstikz-pgf

I'm using pgfplot for plotting in Latex. I want to achieve this kind of graph

enter image description here

And this is the best I can do so far [Update #4],

enter image description here

Here's my latex code [Updated #4] :

\begin{tikzpicture}
\begin{groupplot}[
       group name=plot,
       group size=2 by 1,
       xlabels at=edge bottom,
       ylabels at=edge left,
       horizontal sep=-10pt,
       vertical sep=0pt,
      /pgf/bar width=12pt},
legend columns=-1,
legend  style={at={(6.5 ,3.6)}},
legend entries={{\color{black}{\footnotesize Win}},{\color{black}{\footnotesize Lose}}},
legend to name=CombinedLegendBar,
ybar legend,
ylabel={Participant},
major x tick style=transparent,
ybar= 1*\pgflinewidth,
ymax=6,
width=6cm,
x axis line style={opacity=0},
x tick label style={rotate=0, anchor=center},
symbolic x coords={{Task 1}, {Task 2}, {Task 3}},
xticklabel style={yshift=-2mm,xshift={ifthenelse(\ticknum==2,0,0)}}, 
xtick=data,
ymajorgrids=true,
grid style=dashed,
nodes near coords,
scale only axis,
point meta=explicit symbolic
]

%modifier
\nextgroupplot[
     xlabel=Multiplayer,
     width=0.2\textwidth,
     height=0.2\textwidth,
     enlarge x limits = 0.5]
\addplot[draw=green,thick,fill=green,fill opacity=0.6] coordinates {
  (Task 1,4)[\textcolor{blue!60}{4}]
  (Task 2,4)[\textcolor{blue!60}{4}]
  (Task 3,4)[\textcolor{blue!60}{4}]};

\addplot[draw=orange,thick,fill=orange,fill opacity=0.6] coordinates {
  (Task 1,2)[\textcolor{orange!100}{2}]
  (Task 2,1)[\textcolor{orange!100}{1}]
  (Task 3,3)[\textcolor{orange!100}{3}]};
  
\nextgroupplot[
     xlabel=Single Player,
     yticklabels={,,},
     width=0.13\textwidth,
     height=0.2\textwidth,
     enlarge x limits = 0.5]
\addplot[draw=green,thick,fill=green,fill opacity=0.6] coordinates {
  (Task 1,4)[\textcolor{blue!60}{4}]
  (Task 2,4)[\textcolor{blue!60}{4}]};

\addplot[draw=orange,thick,fill=orange,fill opacity=0.6] coordinates {
  (Task 1,2)[\textcolor{orange!100}{2}]
  (Task 2,1)[\textcolor{orange!100}{1}]};
\end{groupplot}

\end{tikzpicture}
\ref{CombinedLegendBar}

My problem now :

  1. My plot looks untidy, the space between each groupplot also doesn't look nice.
    My solution : set width, height and enlarge x limits for EACH group plot (\nextgroupplot) instead of on global setting (group plot setting). Don't forget "scale only axis" to help modify width and height properly.

  2. The dashed line overlapped between two group plot. So what I notice is at the tick of the grid is solid at the end and beginning, that's why when it overlaps with another dashed line, it doesn't look nice.

  3. Graph title. I tried to use \node to put my title (of all subplots). Doesn't work properly.

  4. Is it possible to put my legend on the upper empty space inside of the graph ? DONE !! Use legend setting and call it with \ref

    Any advice ?

Best Answer

  1. You could perhaps do some calculations to get useful widths, see example below. Not sure they're "correct", but the result looked OK.
  2. ytick pos=left for the first groupplot, ytick pos=right for the second.
  3. No idea what you mean by "doesn't work properly", adding a node at a useful location works fine. But of course, if you want a numbered caption, use \caption.
  4. Do you really need legend to name and all that? Seems like just adding legend columns=-1 is enough (given that the height of the diagram is sufficient).

There are also some other modifications, such as making a custom cycle list, and setting the style of the nodes near coords instead of using \textcolor everywhere.

That said, it is also possible to do this with just one axis, see second tikzpicture in code below.

\documentclass[tikz,border=5mm]{standalone}
\usepackage{pgfplots}
\usepgfplotslibrary{groupplots}
\pgfplotsset{compat=1.16}
\begin{document}
\begin{tikzpicture}

\pgfmathsetmacro{\NPlotsFirstAx}{3}
\pgfmathsetmacro{\NPlotsSecondAx}{2}
\pgfmathsetmacro{\SumPlots}{\NPlotsFirstAx+\NPlotsSecondAx+2}
\pgfmathsetmacro{\WdFirst}{\NPlotsFirstAx/\SumPlots}
\pgfmathsetmacro{\WdSecond}{\NPlotsSecondAx/\SumPlots}
\begin{groupplot}[
       group style={
       group name=plot,
       group size=2 by 1,
       xlabels at=edge bottom,
       ylabels at=edge left,
       horizontal sep=0pt,
       vertical sep=0pt,
       /pgf/bar width=10pt},
ylabel={Participant},
major x tick style=transparent,
ybar= \pgflinewidth,
ymax=6,
%width=6cm,
x axis line style={opacity=0},
x tick label style={rotate=0, anchor=center},
%symbolic x coords={{Task 1}, {Task 2}, {Task 3}},
xticklabel style={yshift=-2mm,xshift={ifthenelse(\ticknum==2,0,0)}}, 
xtick=data,
xticklabels={Task 1, Task 2, Task 3},
ymajorgrids=true,
grid style=dotted,
nodes near coords,
scale only axis,
point meta=explicit symbolic,
enlarge x limits = {abs=1},
cycle list={
  draw=blue,thick,fill=blue,fill opacity=0.6,nodes near coords style={blue!60}\\
  draw=orange,thick,fill=orange,fill opacity=0.6,nodes near coords style={orange}\\
  },
legend columns=-1,
height=0.3\textwidth,
]

%modifier
\nextgroupplot[
     xlabel=Color,
     width=\WdFirst\textwidth,
     ytick pos=left
      ]
\addplot  coordinates {
  (1,4)[4]
  (2,4)[4]
  (3,4)[4]};

\addplot  coordinates {
  (1,2)[2]
  (2,1)[1]
  (3,3)[3]};

\nextgroupplot[
     xlabel=Brightness,
     yticklabels={},
     width=\WdSecond\textwidth,
     ytick pos=right
     ]
\addplot coordinates {
  (1,4)[4]
  (2,4)[4]};

\addplot coordinates {
  (1,2)[2]
  (2,1)[1]};

\legend{Win, Lose}
\end{groupplot}


\node [above] at (current bounding box.north) {Title of entire thing};

\end{tikzpicture}

\begin{tikzpicture}
\begin{axis}[
  xticklabels={Task 1, Task 2, Task 3, Task 1, Task 2},
  xtick=data,
  ylabel={Participant},
  major x tick style=transparent,
  ybar= \pgflinewidth,
  ymax=6,
  x axis line style={opacity=0},
  x tick label style={rotate=0, anchor=center},
  xticklabel style={yshift=-2mm}, 
  xtick=data,
  ymajorgrids=true,
  grid style=dotted,
  nodes near coords,
  scale only axis,
  point meta=explicit symbolic,
  enlarge x limits = {abs=1},
  cycle list={
    draw=blue,thick,fill=blue,fill opacity=0.6,nodes near coords style={blue!60}\\
    draw=orange,thick,fill=orange,fill opacity=0.6,nodes near coords style={orange}\\
  },
  legend columns=-1,
  legend pos=north east,
  height=0.3\textwidth,
  width=0.7\textwidth,
  extra x ticks={2, 5.5},
  extra x tick labels={Color,Brightness},
  extra x tick style={yshift=-15pt},
  title={Title of entire thing}
]

\addplot  coordinates {
  (1,4)[4]
  (2,4)[4]
  (3,4)[4]
  (5,4)[4]
  (6,4)[4]};


\addplot  coordinates {
  (1,2)[2]
  (2,1)[1]
  (3,3)[3]
  (5,2)[2]
  (6,1)[1]};

\legend{Win,Lose}
\end{axis}
\end{tikzpicture}
\end{document}

enter image description here