Quero comparar porcentagens de dois anos diferentes usando gráficos de barras horizontais.
Os valores para os 2 anos (1996 e 2011) são dados através de uma tabela pgfplots. Quero que os valores de 1996 sejam mostrados acima dos valores correspondentes de 2011. Mas ao adicioná-los na ordem correta, as entradas da legenda são ordenadas de forma inversa (2011 mostrado primeiro, depois 1996).
MWE:
\documentclass[a4paper,11pt,twoside]{memoir}
\usepackage{pgfplots}
\usepackage{pgfplotstable}
\begin{document}
\pgfplotstableread[col sep=comma,header=true]{
Type,1996,2011
type1,41.26,46.57
type2,55.42,38.76
type3,0.14,0.11
type4,0.24,0.05
type5,0.79,13.20
type6,2.14,1.31
}\data
\begin{figure} [tb]%
\centering
\begin{tikzpicture}
\begin{axis}[
width=12cm,
xbar,
xtick={0,10,20,...,100},
xmin=0,
xmax=100,
grid=major,
nodes near coords, nodes near coords align={horizontal},
symbolic y coords={type6,type5,type4,type3,type2,type1},
ylabel={Type},
xlabel={Percentage},
y label style={at={(-0.1,0.5)}},
enlarge x limits={abs=0}
]
\addplot table [x=1996, y=Type] {\data};
\addplot table [x=2011, y=Type] {\data};
\legend{1996,2011}
\end{axis}
\end{tikzpicture}
\label{fig:distribution}
\end{figure}
\end{document}
Tentei alterar a ordem da legenda manualmente, conforme descrito emesta postagem, mas isso levou a um alinhamento vertical feio entre as barras e o texto na legenda:
\documentclass[a4paper,11pt,twoside]{memoir}
\usepackage{pgfplots}
\usepackage{pgfplotstable}
\begin{document}
\pgfplotstableread[col sep=comma,header=true]{
Type,1996,2011
type1,41.26,46.57
type2,55.42,38.76
type3,0.14,0.11
type4,0.24,0.05
type5,0.79,13.20
type6,2.14,1.31
}\data
\begin{figure} [tb]%
\centering
\begin{tikzpicture}
\begin{axis}[
width=12cm,
xbar,
xtick={0,10,20,...,100},
xmin=0,
xmax=100,
grid=major,
nodes near coords, nodes near coords align={horizontal},
symbolic y coords={type6,type5,type4,type3,type2,type1},
ylabel={Type},
xlabel={Percentage},
y label style={at={(-0.1,0.5)}},
enlarge x limits={abs=0},
extra description/.code={
\matrix[/pgfplots/every axis legend]
{
\ref{1996} \pgfmatrixnextcell \node{1996};\\
\ref{2011} \pgfmatrixnextcell \node{2011};\\
};
}
]
\addplot table [x=2011, y=Type] {\data};
\label{2011}
\addplot table [x=1996, y=Type] {\data};
\label{1996}
\end{axis}
\end{tikzpicture}
\label{fig:distribution}
\end{figure}
\end{document}
Conforme sugerido no post, consultei o tikz
manual, mas não encontrei solução para esse problema (pelo menos para minhas habilidades em látex). Alguma dica?
Responder1
Existe alguma chave chamada reverse legend
que deve fazer exatamente o que você deseja.
Adicioná-lo à lista de opções resulta
\documentclass[a4paper,11pt,twoside]{memoir}
\usepackage{pgfplots}
\usepackage{pgfplotstable}
\begin{document}
\thispagestyle{empty}
\pgfplotstableread[col sep=comma,header=true]{
Type,1996,2011
type1,41.26,46.57
type2,55.42,38.76
type3,0.14,0.11
type4,0.24,0.05
type5,0.79,13.20
type6,2.14,1.31
}\data
\begin{figure} [tb]%
\centering
\begin{tikzpicture}
\begin{axis}[
width=12cm,
xbar,
xtick={0,10,20,...,100},
xmin=0,
xmax=100,
grid=major,
nodes near coords, nodes near coords align={horizontal},
symbolic y coords={type6,type5,type4,type3,type2,type1},
ylabel={Type},
xlabel={Percentage},
y label style={at={(-0.1,0.5)}},
enlarge x limits={abs=0},
reverse legend,
]
\addplot table [x=2011, y=Type] {\data};
\addplot table [x=1996, y=Type] {\data};
\legend{2011,1996}
\end{axis}
\end{tikzpicture}
\label{fig:distribution}
\end{figure}
\end{document}