PGF - 如何交换条形的顺序?

PGF - 如何交换条形的顺序?

我制作了以下xbar图表:

在此处输入图片描述

正如你所见,尽管2019 年第四季度首先绘制的系列(深蓝色),它出现在2020 年第一季度系列(橙色)。我该如何修复此问题?

请注意,图例顺序是正确的 - 4Q19 位于 1Q20 之上。为什么这没有反映在图中?

生成上述内容的代码:

\documentclass{standalone}
\usepackage{pgfplots}
\usepackage{pgfplotstable}
\usepgfplotslibrary{dateplot}
\pgfplotsset{compat=1.8}

%colors
\usepackage{color} % colors
\usepackage{xcolor} 
\definecolor{cone}{RGB}{18,32,132} % blue
\definecolor{ctwo}{RGB}{255,153,0} % orange
\definecolor{ctwol}{RGB}{255, 192, 0} % lighter orange
\definecolor{cthree}{RGB}{0, 176, 80} % green 
\definecolor{cfour}{RGB}{255, 0, 0} % red
\definecolor{cfive}{RGB}{0, 176, 240} % light blue

\begin{document}

\pgfplotstableread[col sep=comma]{
country, 4Q19, 1Q20
PR China, 5.0, -9.8
Malaysia, 0.3, -15.0
Korea, 6.5, -20.2
Singapore, 3.3, -23.0
Indonesia, 2.1, -27.9
Thailand, -3.5, -28.7
Philippines, 0.5, -31.9 
}\chartnineteen

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

\begin{tikzpicture}
\renewcommand{\familydefault}{\sfdefault}
\begin{axis}[
y=6.5mm,
x = 1mm,
xbar,
bar width=2mm,
axis lines=left,
enlarge y limits=0.13,
%
% x ticks style and label
xlabel={Annual change (\%)},
xmin = -40,
xmax = 10,
xtick distance = 10,
xlabel shift = 5pt,
%
% y axis ticks and style
axis y line shift=-40,
yticklabel shift = 120pt,
ytick=data,
yticklabels from table={\chartnineteen}{country},  
table/y expr = -\coordindex,                     
y tick label style = {rotate=0},
%
% legends and labels
legend style = {fill = none, draw=none,
    legend columns=2,
    at={(0.3,1.15)},
    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=0cm, xshift = 0cm,
},
]
%
% done with the axis, now the plots
\addplot [cone, fill=cone, nodes near coords, draw opacity = 0]
table [x=4Q19]  {\chartnineteen};
\addlegendentry{4Q19};
\addplot [ctwo, fill=ctwo, nodes near coords, draw opacity = 0]
table [x=1Q20]  {\chartnineteen};
\addlegendentry{1Q20};
\end{axis}
\end{tikzpicture}

\end{document}

答案1

解决方案是使用reverse legend(h/t这里)然后交换情节顺序:

\documentclass{standalone}
\usepackage{pgfplots}
\usepackage{pgfplotstable}
\usepgfplotslibrary{dateplot}
\pgfplotsset{compat=1.8}

%colors
\usepackage{color} % colors
\usepackage{xcolor} 
\definecolor{cone}{RGB}{18,32,132} % blue
\definecolor{ctwo}{RGB}{255,153,0} % orange
\definecolor{ctwol}{RGB}{255, 192, 0} % lighter orange
\definecolor{cthree}{RGB}{0, 176, 80} % green 
\definecolor{cfour}{RGB}{255, 0, 0} % red
\definecolor{cfive}{RGB}{0, 176, 240} % light blue

\begin{document}

\pgfplotstableread[col sep=comma]{
country, 4Q19, 1Q20
PR China, 5.0, -9.8
Malaysia, 0.3, -15.0
Korea, 6.5, -20.2
Singapore, 3.3, -23.0
Indonesia, 2.1, -27.9
Thailand, -3.5, -28.7
Philippines, 0.5, -31.9 
}\chartnineteen

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

\begin{tikzpicture}
\renewcommand{\familydefault}{\sfdefault}
\begin{axis}[
y=6.5mm,
x = 1mm,
xbar,
bar width=2mm,
axis lines=left,
enlarge y limits=0.13,
%
% x ticks style and label
xlabel={Annual change (\%)},
xmin = -40,
xmax = 10,
xtick distance = 10,
xlabel shift = 5pt,
%
% y axis ticks and style
axis y line shift=-40,
yticklabel shift = 120pt,
ytick=data,
yticklabels from table={\chartnineteen}{country},  
table/y expr = -\coordindex,                     
y tick label style = {rotate=0},
%
% legends and labels
reverse legend,
legend style = {fill = none, draw=none,
    legend columns=2,
    at={(0.3,1.15)},
    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=0cm, xshift = 0cm,
},
]
%
% done with the axis, now the plots
\addplot [ctwo, fill=ctwo, nodes near coords, draw opacity = 0]
table [x=1Q20]  {\chartnineteen};
\addlegendentry{1Q20};
\addplot [cone, fill=cone, nodes near coords, draw opacity = 0]
table [x=4Q19]  {\chartnineteen};
\addlegendentry{4Q19};
\end{axis}
\end{tikzpicture}

\end{document}

制作这个:

在此处输入图片描述

相关内容