1

I want to make a grouped bar chart in LaTeX, using data from a csv (or xlsx) file. I want it to be styled similarly to my other bar charts. I don't know how to get it into the right format and everytime I try to adjust grouped bar chart examples in to my style, I get errors. This is the bar chart from my data (made it in Excel as placeholder):enter image description here

And this the style for the other bar charts in my thesis (ignore the line, that shouldn't be in the grouped bar chart): enter image description here

What I would like:

  • A grouped bar chart as in the Excel example
  • The same bar and legend style as the second picture
  • The same axis label style as in the second picture (instead of 17 to 29, 28-5 to 5-6)

My code is:

\PassOptionsToPackage{table,dvipsnames,svgnames}{xcolor}
\documentclass[11pt, twoside, a4paper]{report}
\usepackage[inner = 30mm, outer = 20mm,  top = 30mm, bottom = 20mm, headheight = 13.6pt]{geometry}
\usepackage{tikz}
\usepackage[pdfpagelayout=TwoPageRight]{hyperref}
\usepackage[export]{adjustbox}
\hypersetup{colorlinks=true, linktoc=all, allcolors=green!30!black,}

\usepackage{pgfplots}
\pgfplotsset{
    compat=1.16,
    % created a style for the common `axis' options
    my axis style/.style={
        width=\linewidth,
        height=0.35\linewidth,
        bar width=0.9,
        enlarge x limits={abs=0.45},    % <-- changed to absolute coordinates
        ymin=0,
        legend style={
            at={(0.5,1.05)},    % <-- adapted
            anchor=south,       % <-- changed from `north'
            legend columns=2,
        },
        ylabel={PR\textsubscript{A}},
        xtick=data,
        axis lines*=left,
        ymajorgrids,
        %
        table/x=x,
    },
    %made the beginnings of a second axis style, because I figured it needs to be different for grouped
    my second axis style/.style={
    width=\linewidth,
    height=0.35\linewidth,
    bar width=0.3,
    enlarge x limits={abs=0.45},    % <-- changed to absolute coordinates
    ymin=0,
    legend style={
        at={(0.5,1.05)},    % <-- adapted
        anchor=south,       % <-- changed from `north'
        legend columns=2,
    },
    ylabel={PR\textsubscript{A}},
    xtick=data,
    axis lines*=left,
    ymajorgrids,
    %
    table/x=x,
    },
    % created a style for the common `ybar' options
    my ybar style/.style={
        ybar,
        my ybar legend,            % <-- change legend image accordingly
        #1!50!black,
        fill=white!70!black,
        nodes near coords,      % <-- moved from `axis' options here
        % state absolute positions for the `nodes near coords'
        scatter/position=absolute,
        node near coords style={
            % state where the nodes should appear
            at={(\pgfkeysvalueof{/data point/x},0.5)},
            anchor=center,
            % make the font a bit smaller
            font=\footnotesize,
            % set the number format of the `nodes near coords'
            /pgf/number format/.cd,
            fixed,
            precision=2,
            zerofill,
        },
    },
    my ybar legend/.style={
        /pgfplots/legend image code/.code={
            \draw [
            ##1,
            /tikz/.cd,
            yshift=-0.25em,
            ] (0cm,0cm) rectangle (3pt,0.8em);
        },
    },
}



%data for the grouped bar chart
\pgfplotstableread{
    x          SP_cSi_2_3   SP_cSi_2_4  Reference
    1   0.500   0.627   0.868
    2   0.781   0.778   0.859
    3   0.819   0.868   0.871
    4   0.732   0.824   0.876
    5   0.853   0.873   0.954
    6    0.813   0.838   0.940
    7    0.712   0.759   0.876
    8    0.864   0.894   0.887
    9    0.465   0.614   0.891
}{\loadedtablesppr}


\begin{document}

    \begin{figure}
        \begin{tikzpicture}
        \begin{axis}[my second axis style,
        ybar,
        enlargelimits=0.1,
        legend style={at={(0.5,-0.15)},
            anchor=north,legend columns=-1},
        ylabel={PR\textsubscript{A}},
        xtick=data,
        ]
        \addplot [my ybar style=red!50!black,] table [y=SP_cSi_2_3] {\loadedtablesppr};
        \addplot [my ybar style=red!50!black,] table [y=SP_cSi_2_4] {\loadedtablesppr};
        \addplot [my ybar style=red!50!black,] table [y=Reference] {\loadedtablesppr};
        \legend{SP cSi 2.3,SP cSi 2.4,ref}
        \end{axis}
        \end{tikzpicture}
    \end{figure}

\end{document}

Which gives me: enter image description here

Edit:

Thanks to Marmot, I'm very close now. My new code:

\PassOptionsToPackage{table,dvipsnames,svgnames}{xcolor}
\documentclass[11pt, twoside, a4paper]{report}
\usepackage[inner = 30mm, outer = 20mm,  top = 30mm, bottom = 20mm, headheight = 13.6pt]{geometry}
\usepackage{tikz}
\usepackage[pdfpagelayout=TwoPageRight]{hyperref}
\usepackage[export]{adjustbox}
%\usepackage{showframe}
\hypersetup{colorlinks=true, linktoc=all, allcolors=green!30!black,}

\usepackage{pgfplots}
\pgfplotsset{
    compat=1.16,
    %made the beginnings of a second axis style, because I figured it needs to be different for grouped
    my second axis style/.style={
    width=\linewidth,
    height=0.35\linewidth,
    bar width=0.2, %<- changed
    enlarge x limits={abs=0.45},    % <-- changed to absolute coordinates
    ymin=0,
    legend style={
        at={(0.5,1.15)},    % <-- adapted
        anchor=north,       % <-- changed from `north'
        legend columns=3,
    },
    ylabel={PR\textsubscript{A}},
    xtick=data,
    axis lines*=left,
    ymajorgrids,
    %
    table/x=x,
    },
    % created a style for the common `ybar' options
    my second ybar style/.style={
        ybar,
        my ybar legend,            % <-- change legend image accordingly
        #1!50!black,
        fill=white!70!black,, %<- changed back
        nodes near coords,      % <-- moved from `axis' options here
        % state absolute positions for the `nodes near coords'
        scatter/position=absolute,
        node near coords style={
            % state where the nodes should appear
            at={(\pgfkeysvalueof{/data point/x},0.5*\pgfkeysvalueof{/data point/y})},
            anchor=center,rotate=90, %<-added
            % make the font a bit smaller
            font=\footnotesize,
            % set the number format of the `nodes near coords'
            /pgf/number format/.cd,
            fixed,
            precision=2,
            zerofill,
        },
    },
    my ybar legend/.style={
        /pgfplots/legend image code/.code={
            \draw [
            ##1,
            /tikz/.cd,
            yshift=-0.25em,
            ] (0cm,0cm) rectangle (3pt,0.8em);
        },
    },
}



%data for the grouped bar chart
\pgfplotstableread{
    x          SP_cSi_2_3   SP_cSi_2_4  Reference
    28-5   0.500   0.627   0.868
    29-5   0.781   0.778   0.859
    30-5   0.819   0.868   0.871
    31-5   0.732   0.824   0.876
    1-6   0.853   0.873   0.954
    2-6    0.813   0.838   0.940
    3-6    0.712   0.759   0.876
    4-6    0.864   0.894   0.887
    5-6    0.465   0.614   0.891
}{\loadedtablesppr}


\begin{document}

    \begin{figure}
        \begin{tikzpicture}
        \begin{axis}[my second axis style,
        ybar,
        ylabel={PR\textsubscript{A}},
        xtick= data,
        ]
        \addplot [my second ybar style=blue!50!black,] table [y=SP_cSi_2_3] {\loadedtablesppr};
        \addplot [my second ybar style=orange!50!black,] table [y=SP_cSi_2_4] {\loadedtablesppr};
        \addplot [my second ybar style=red!50!black,] table [y=Reference] {\loadedtablesppr};
        \legend{Floating 2.3~~ , Floating 2.4~~ , Reference}
        \end{axis}
        \end{tikzpicture}
    \end{figure}

\end{document}

Final question that remains:

  • Change the x ticks to the dates in the excel example (I get errors when I change them to {28-5, 29-5, etc.} in the \loadedtable)
  • Zero fill the y-axis label (as in the excel example)

Any ideas?

  • Thank you for posting an example! At the same time, much of your code does not seem to be relevant to the question you're asking here. Please limit the example to only the code required for your issue to appear. You can have a look at this guide for how to prune your code for this purpose. Furthermore, your example does not compile correctly, can you fix this please? (To do so, create a new folder and create a new tex file in it containing this code - if this code does not produce any errors, post it again by editing your question.) – Tom Jul 30 '18 at 15:19
  • I changed the code, so now it compiles properly. I think I removed all the unnecessary stuff from the preamble too. Most of it is copied from other people who helped me, because I'm still a bit of a LaTeX noob – Hans Jul 30 '18 at 15:45
2

I guess that one reason why no one dared to answer this nice question so far is that some of the things that you do seem to go against what you want to achieve. So I post a proposal here and you could perhaps use it in order to explain in more detail what you want to change. I indicated the changes in the code.

\PassOptionsToPackage{table,dvipsnames,svgnames}{xcolor}
\documentclass[11pt, twoside, a4paper]{report}
\usepackage[inner = 30mm, outer = 20mm,  top = 30mm, bottom = 20mm, headheight = 13.6pt]{geometry}
\usepackage{tikz}
\usepackage[pdfpagelayout=TwoPageRight]{hyperref}
\usepackage[export]{adjustbox}
\hypersetup{colorlinks=true, linktoc=all, allcolors=green!30!black,}

\usepackage{pgfplots}
\pgfplotsset{
    compat=1.16,
    % created a style for the common `axis' options %<- removed since not used
    %made the beginnings of a second axis style, because I figured it needs to be different for grouped
    my second axis style/.style={
    width=\linewidth,
    height=0.35\linewidth,
    bar width=0.15, %<- changed
    enlarge x limits={abs=0.45},    % <-- changed to absolute coordinates
    ymin=0,
    legend style={
        at={(0.5,1.05)},    % <-- adapted
        anchor=south,       % <-- changed from `north'
        legend columns=2,
    },
    ylabel={PR\textsubscript{A}},
    xtick=data,
    axis lines*=left,
    ymajorgrids,
    %
    table/x=x,
    },
    % created a style for the common `ybar' options
    my ybar style/.style={
        ybar,
        my ybar legend,            % <-- change legend image accordingly
        #1!50!black,
        fill=#1, %<- changed
        nodes near coords,      % <-- moved from `axis' options here
        % state absolute positions for the `nodes near coords'
        scatter/position=absolute,
        node near coords style={
            % state where the nodes should appear
            at={(\pgfkeysvalueof{/data point/x},0.5*\pgfkeysvalueof{/data point/y})},
            anchor=center,rotate=90,text=white,font=\small\sffamily, %<-added
            % make the font a bit smaller
            font=\footnotesize,
            % set the number format of the `nodes near coords'
            /pgf/number format/.cd,
            fixed,
            precision=2,
            zerofill,
        },
    },
    my ybar legend/.style={
        /pgfplots/legend image code/.code={
            \draw [
            ##1,
            /tikz/.cd,
            yshift=-0.25em,
            ] (0cm,0cm) rectangle (3pt,0.8em);
        },
    },
}



%data for the grouped bar chart
\pgfplotstableread{
    x          SP_cSi_2_3   SP_cSi_2_4  Reference
    1   0.500   0.627   0.868
    2   0.781   0.778   0.859
    3   0.819   0.868   0.871
    4   0.732   0.824   0.876
    5   0.853   0.873   0.954
    6    0.813   0.838   0.940
    7    0.712   0.759   0.876
    8    0.864   0.894   0.887
    9    0.465   0.614   0.891
}{\loadedtablesppr}


\begin{document}

    \begin{figure}
        \begin{tikzpicture}
        \begin{axis}[my second axis style,
        ybar,
        enlargelimits=0.1,
        legend style={at={(0.5,-0.15)},
            anchor=north,legend columns=-1},
        ylabel={PR\textsubscript{A}},
        xtick=data,
        ]
        \addplot [my ybar style=blue!50!black,] table [y=SP_cSi_2_3] {\loadedtablesppr};
        \addplot [my ybar style=orange!50!black,] table [y=SP_cSi_2_4] {\loadedtablesppr};
        \addplot [my ybar style=red!50!black,] table [y=Reference] {\loadedtablesppr};
        \legend{SP cSi 2.3,SP cSi 2.4,ref}
        \end{axis}
        \end{tikzpicture}
    \end{figure}

\end{document}

enter image description here

  • Thanks for your answer. I really like the nodes. I edited my question to include my code based on your answer. Could you take a look please? – Hans Jul 31 '18 at 6:18
  • @Hans Could you please replace the entries in the first column by the dates you want to use? (Otherwise I don't know which dates to use.) – user121799 Jul 31 '18 at 11:07
  • Edited it in the second code box – Hans Jul 31 '18 at 11:11
  • @Hans I am sorry, I give up for now, I get errors I have never seen before. Naively I would think that with symbolic x coordinates it should work, but for some reason it does not even though it always worked before. – user121799 Jul 31 '18 at 11:54
  • Okay, thanks for trying anyway. Do you know where I could change the line thickness around the bars? – Hans Jul 31 '18 at 12:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.