3

I am currently producing the following chart:

enter image description here

With this code:

\documentclass{standalone}
\usepackage{tikz}
\usepackage{pgfplots}
\usepackage{pgfplotstable}
\usepackage{pdfpages}
\pgfplotsset{compat=1.8}  


\usepackage{color} % colors
\usepackage{xcolor} % more control over colors
\newcommand{\titlecolour}{blue!70!white}

% chart 1 data
\pgfplotstableread[col sep=comma]%  % <--- 
{
    country,          3Q19,   4Q19
    US,             2.0,    1.9
    Euro,           1.2,    1.1
    PR China,       6.1,    6.0
    Singapore,      0.2,    0.1
    Indonesia,      5.1,    5.0
    Malaysia,       4.7,    4.4
    Korea,          2.1,    2.0
    Taiwan,         1.7,    2.9
    Philippines,    5.6,    5.9
}\chartone

\pgfplotsset{compat=1.11,
    /pgfplots/ybar legend/.style={
        /pgfplots/legend image code/.code={%
            \draw[ ##1,/tikz/.cd,yshift=-0.25em]
            (0cm,0cm) rectangle (0.6em,0.6em);},
    },
}

\begin{document}


\begin{tikzpicture}
\small
\begin{axis}[
x=9mm,          
ybar,
x = 0.85cm,
bar width = 2mm,       
axis lines=left,
enlarge x limits=0.1,           
enlarge y limits={.2, upper}, 
% 
% y and x axis ticks
y tick label style={/pgf/number format/.cd, fixed, fixed zerofill, precision=1,
    /tikz/.cd, font=\scriptsize},
ymin = 0,
xtick=data,
x tick label style={rotate = 90},
symbolic x coords={US, Euro, PR China, Singapore, 
    Indonesia, Malaysia, Korea, Taiwan, Philippines},
%
% legends and labels
legend style={draw=none, legend columns=-1, at={(0.8, 1)},anchor=north east,                                      % 
    /tikz/every even column/.append style={column sep=1em}},  % <---
y label style={at={(0.15,1)},rotate=-90,anchor=south},  % I prefer label along y axes
nodes near coords style={/pgf/number format/.cd, fixed, fixed zerofill, precision=1,
    /tikz/.cd, font=\scriptsize, color = black},
legend style={legend columns=-1},
ylabel={Annual change (\%)},
]
\addplot[\titlecolour, fill = \titlecolour]
table [y=3Q19]  \chartone;    % <---
\addplot[yellow!80!black, fill = yellow!80!black, nodes near coords]
table [y=4Q19]  \chartone;    % <---
\legend{~3Q19, ~4Q19}
\end{axis}
\end{tikzpicture}

\end{document}

I'd like to make two adjustments:

  1. Raise the nodes near coords slightly, so they aren't so close to the bars.
  2. Set the values of the legend labels (3Q19 and 4Q19) as well as the x axis labels directly from the chartone table. Currently, they're being set manually, with the \legend and symbolic x coords commands, which is not ideal as it leaves room for human error.

Greatly appreciate any help!

Thev
  • 1,568

2 Answers2

5

2 of your 3 points can be done quite easily. That is, add yshift=... to the nodes near coords style to shift the nodes and replace the symbolic coords with xticklabels from table and table/x expr=\coordindex.

You can get legend entries from table headers, but this only makes sense if you don't do something special to each and every \addplot command and therefore you could loop through the table columns (using \pgfplotsinvokeforeach or the like). As an example have a look at my answer given here.

% used PGFPlots v1.16
\documentclass[border=5pt]{standalone}
\usepackage{pgfplots}
\usepackage{pgfplotstable}
    \pgfplotsset{compat=1.11,
        /pgfplots/ybar legend/.style={
            /pgfplots/legend image code/.code={%
                \draw[ ##1,/tikz/.cd,yshift=-0.25em]
                (0cm,0cm) rectangle (0.6em,0.6em);},
        },
    }
    \colorlet{titlecolour}{blue!70!white}       % <-- (adjusted)
    \pgfplotstableread[col sep=comma]{
        country,        3Q19,   4Q19
        US,             2.0,    1.9
        Euro,           1.2,    1.1
        PR China,       6.1,    6.0
        Singapore,      0.2,    0.1
        Indonesia,      5.1,    5.0
        Malaysia,       4.7,    4.4
        Korea,          2.1,    2.0
        Taiwan,         1.7,    2.9
        Philippines,    5.6,    5.9
    }\chartone
\begin{document}
\begin{tikzpicture}
        \small
    \begin{axis}[
        x=9mm,
        ybar,
        x=0.85cm,
        bar width=2mm,
        axis lines=left,
        enlarge x limits=0.1,
        enlarge y limits={.2, upper},
        %
        % y and x axis ticks
        y tick label style={
            /pgf/number format/.cd,
                fixed,
                fixed zerofill,
                precision=1,
            /tikz/.cd,
            font=\scriptsize,
        },
        ymin=0,
        xtick=data,
        xticklabels from table={\chartone}{country},    % <-- added
        table/x expr=\coordindex,                       % <-- added
        x tick label style={rotate=90},
        %
        % legends and labels
        legend style={
            draw=none,
            legend columns=-1,
            at={(0.8, 1)},
            anchor=north east,
            /tikz/every even column/.append style={
                column sep=1em,
            },
        },
        y label style={
            at={(0.15,1)},
            rotate=-90,
            anchor=south,
        },
        nodes near coords style={
            /pgf/number format/.cd,
                fixed,
                fixed zerofill,
                precision=1,
            /tikz/.cd,
            font=\scriptsize,
            color=black,
            yshift=1ex,             % <-- added
        },
        ylabel={Annual change (\%)},
    ]

        \addplot [
            titlecolour,
            fill=titlecolour,
        ] table [y=3Q19]  {\chartone};
            \addlegendentry{3Q19};

        \addplot [
            yellow!80!black,
            fill=yellow!80!black,
            nodes near coords,
        ] table [y=4Q19]  {\chartone};
            \addlegendentry{4Q19};

    \end{axis}
\end{tikzpicture}
\end{document}

image showing the result of above code

Stefan Pinnow
  • 29,535
  • Brilliant; I learned a lot from this! – Thev Mar 10 '20 at 19:58
  • 2
    You are welcome. That's what we -- the helpers -- are good for. Especially if they try to understand the answer to do it better the next time instead of just copy-pasting them without saying "thank you" (by upvoting and/or accepting an answer) which results in a "No thank you for just doing the job for me so I don't have to do it.". – Stefan Pinnow Mar 10 '20 at 20:38
2

To long for comment ...

Some off-topic remarks (since your main problem is solved by nice @Stefan Pinnow answer: +1) I wonder, why you in your MWE:

  • define version of used pgfplots twice, first compat=1.8, than compat=1.11 (at legend's style definition in preamble)
  • define style of the diagram legend twice, ones in preamble, onse in the axis' optios
  • why y-axis label is put on the top of diagram. There is usual place for diagram title, for clarity people put axis label along axis
  • also I son't see any advantage to write y-ticks as 2,0, 4.0, ..., to my opinion it is sufficient 2, 4, ...

Considering aforementioned, The MWE can be:

\documentclass[border=5pt]{standalone}
\usepackage{pgfplots}
\usepackage{pgfplotstable}
\pgfplotsset{compat=1.16}
\colorlet{titlecolour}{blue!70!white}    

\pgfplotstableread[col sep=comma]{
    country,        3Q19,   4Q19
    US,             2.0,    1.9
    Euro,           1.2,    1.1
    PR China,       6.1,    6.0
    Singapore,      0.2,    0.1
    Indonesia,      5.1,    5.0
    Malaysia,       4.7,    4.4
    Korea,          2.1,    2.0
    Taiwan,         1.7,    2.9
    Philippines,    5.6,    5.9
            }\chartone
\begin{document}
    \begin{tikzpicture}
\begin{axis}[x=12mm,
    ybar,
    bar width=4.4mm,
    axis lines=left,
    enlarge x limits=0.1,
    enlarge y limits={.2, upper},
% y ticks style and label
ylabel={Annual change (\%)},
    ymin=0,
% x axis ticks and style
    xtick=data,
    xticklabels from table={\chartone}{country},    % <-------------------
    table/x expr = \coordindex,                     % <-------------------
    x tick label style = {rotate=90},
% legends and labels
    legend style = {draw=none,
                    legend columns=-1,
                    at={(0.5,1)},
                    anchor=north,
                    /tikz/every even column/.append style={column sep=2em}
                    },
% nodes near coordinates
    nodes near coords style = { /pgf/number format/.cd,
                                fixed, fixed zerofill, precision=1,
                                /tikz/.cd, font=\scriptsize, color=black,
                                yshift=0.5ex,
                                },
    ]
\addplot [fill=titlecolour]
            table [y=3Q19]  {\chartone};
            \addlegendentry{3Q19};
\addplot [fill=yellow!80!black,
          nodes near coords]                        % <-------------------
            table [y=4Q19]  {\chartone};
            \addlegendentry{4Q19};
    \end{axis}
\end{tikzpicture}
\end{document}

enter image description here

Zarko
  • 296,517
  • Thank you for the further suggestions; I am learning a lot from how you write up the code too. – Thev Mar 10 '20 at 19:58