9

I am trying to create a grouped bar chart with pgfplots, like this one:

enter image description here

but I can't put the values over the bars, like this one:

enter image description here

My code is:

\documentclass{article}
\usepackage{pgfplots}
\usepackage{pgfplotstable}
\pgfplotsset{compat=newest}
\usepackage{caption}
\usepackage{polyglossia}
\setdefaultlanguage{portuges}

\begin{document}

\begin{figure}[h]
\makeatletter
\pgfplotsset{
    calculate offset/.code={
        \pgfkeys{/pgf/fpu=true,/pgf/fpu/output format=fixed}
        \pgfmathsetmacro\testmacro{(\pgfplotspointmeta *10^\pgfplots@data@scale@trafo@EXPONENT@y)*\pgfplots@y@veclength)}
        \pgfkeys{/pgf/fpu=false}
    },
    every node near coord/.style={
        /pgfplots/calculate offset,
        yshift=-\testmacro
    }
}
%0 - aramente   1 - Às vezes   2 - Quase sempre   4 - Sempre
\pgfplotstableread{
  %2013-2014    %2012-2013  %2011-2012
0 32        35      20
1 28        45      23
2 30        24      25
3 10        68      70
}\dataset
\begin{tikzpicture}
\begin{axis}[ybar,
        width=12cm,
        height=8cm,
        ymin=0,
        ymax=100,        
        ylabel={Percentagem},
        xtick=data,
        xticklabels = {
            Raramente,
            Às vezes,
            Quase sempre,
            Sempre
            %Category 5,
            %Category 6
        },
        xticklabel style={yshift=-10ex},
        major x tick style = {opacity=0},
        minor x tick num = 1,
        minor tick length=2ex,
        every node near coord/.append style={
                anchor=east,
                rotate=90
        }
        ]
\addplot[draw=black,fill=blue!20, nodes near coords=2013-2014] table[x index=0,y index=1] \dataset; %ano de 2013-2014
\addplot[draw=black,fill=blue!40, nodes near coords=2012-2013] table[x index=0,y index=2] \dataset; %ano de 2012-2013
\addplot[draw=black,fill=blue!60, nodes near coords=2011-2012] table[x index=0,y index=3] \dataset; %ano de 2011-2012
\end{axis}
\end{tikzpicture}
\captionsetup{justification=centerlast, margin=10ex, labelfont=bf, textfont=it, format=plain, labelformat=default, labelsep=endash, font=small, name=Gráfico\,}
\caption{Em sua casa é costume desligar os equipamentos no controlo remoto, deixando-os em standby (modo de “espera”)?}\label{Questao01}

\end{figure}

\end{document}
9

nodes near coords alone, without any further customization, results in the desired nodes on top of the bars.

This has the disadvantage that your legend description are no longer a standard nodes near coords instruction. The way that pgfplots handles such a case would be to use legend entries, perhaps as in the following example:

enter image description here

\documentclass{article}
\usepackage{pgfplots}
\usepackage{pgfplotstable}
\pgfplotsset{compat=newest}
\usepackage{caption}

\begin{document}
\thispagestyle{empty}

\begin{figure}[h]
%0 - aramente   1 - Às vezes   2 - Quase sempre   4 - Sempre
\pgfplotstableread{
  %2013-2014    %2012-2013  %2011-2012
0 32        35      20
1 28        45      23
2 30        24      25
3 10        68      70
}\dataset
\begin{tikzpicture}
\begin{axis}[ybar,
        width=12cm,
        height=8cm,
        ymin=0,
        ymax=100,        
        ylabel={Percentagem},
        xtick=data,
        xticklabels = {
            \strut Raramente,
            \strut Às vezes,
            \strut Quase sempre,
            \strut Sempre
            %Category 5,
            %Category 6
        },
        %xticklabel style={yshift=-10ex},
        major x tick style = {opacity=0},
        minor x tick num = 1,
        minor tick length=2ex,
        every node near coord/.append style={
                anchor=west,
                rotate=90
        },
        legend entries={2013-2014 ,2012-2013 ,2011-2012 },
        legend columns=3,
        legend style={draw=none,nodes={inner sep=3pt}},
        ]
\addplot[draw=black,fill=blue!20, nodes near coords] table[x index=0,y index=1] \dataset; %ano de 2013-2014
\addplot[draw=black,fill=blue!40, nodes near coords] table[x index=0,y index=2] \dataset; %ano de 2012-2013
\addplot[draw=black,fill=blue!60, nodes near coords] table[x index=0,y index=3] \dataset; %ano de 2011-2012
\end{axis}
\end{tikzpicture}
\captionsetup{justification=centerlast, margin=10ex, labelfont=bf, textfont=it, format=plain, labelformat=default, labelsep=endash, font=small, name=Gráfico\,}
\caption{Em sua casa é costume desligar os equipamentos no controlo remoto, deixando-os em standby (modo de “espera”)?}\label{Questao01}

\end{figure}

\end{document}

Here, nodes near coords draws the individual bar values. The legend entries, together with a bit of legend customization, results in the description on top of the plot.

Note that \strut in the tick labels causes all tick labels to have the same size (even below the text's base line), such that they are aligned vertically (thanks @egreg for the suggestion).

  • Would it be possible to align the baselines in the group captions? A \strut would be sufficient, but perhaps there's also an option for it. – egreg Jan 3 '14 at 21:21
  • @egreg good point. One could work around it by grabbing the node at anchor=baseline and change yshift accordingly -- but \strut is certainly the best solution. In wonder if it makes sense to inject \strut whenever someone uses xticklabels... I will think about whether this might have unanticipated side--effects. I will edit my answer. – Christian Feuersänger Jan 4 '14 at 8:52
  • Thank you Christian Feuersänger! I'll follow your directions. – Luis Ferreira Jan 4 '14 at 12:08
  • @LuisFerreira Please consider accepting this answer (and possibly upvoting it) if it answered your question. That way, other users can easily see that the question has a solution and reputation can be distributed accordingly (even to you!). – cfr Jan 19 '14 at 0:50

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.