6

edit I don't need a solution anymore, I decided to not use the "negative" bars on the right side but I am leaving (and not deleting) this question in case someone else is looking for this in the future.

I'd like to produce this xbar plot style, where the two values obivously add up to the full 100%.

enter image description here

I already found a question named How to make a pgfplots stacked xbar plot with bar-“explicit meta” labels computed from input table data, but as far as I understood things it does not really help.

note Please do edit the title if you have a better phrase in mind for this question. I'm not a native speaker and couldn't think of a better description.

MWE of what I have so far:

\documentclass{scrartcl}

\usepackage[T1]{fontenc}
\usepackage{lmodern}

\usepackage{
pgfplots,
relsize,
sansmath
}

\begin{document}
\begin{center}
\begin{tikzpicture}[font=\sffamily\small\sansmath]
\begin{axis}[
height=7cm,
width=14cm,
%
xmin=0,
xmax=100,
%
ymin=2006,
ymax=2011,
%
xbar,
%
%axis lines*=left,
%
scaled y ticks=false,
y tick label style={/pgf/number format/1000 sep={}},
%
xlabel={Something in \%},
ylabel={Years},
%
legend style={at={(0.8,1.25)}},
legend columns=2,
legend cell align=left,
%
enlarge x limits=0.05,
enlarge y limits=0.1,
every axis x label/.style={
at={(ticklabel* cs:0.5)},
anchor=north,
yshift=-10pt
},
every axis y label/.style={
at={(ticklabel* cs:1.02)},
anchor=south,
text width=3cm
},
]
\addplot
[draw=black,fill=black!60]
coordinates
{(82.8,2011) (81.7,2010) (79.3,2009) (70.3,2008) (65.4,2007) (63.8,2006)};
\addlegendentry{abc}
\addplot
[draw=black,fill=white]
coordinates
{(17.2,2011) (18.3,2010) (20.7,2009) (29.7,2008) (34.6,2007) (36.7,2006)};
\addlegendentry{def}
\end{axis}
\end{tikzpicture}

\end{center}
\end{document}

Picture of what I have so far:

enter image description here

5

You can stack bars by using xbar stacked instead of xbar. If you use a table to provide the data, you can furthermore calculate the "negative" bar automatically:

\documentclass{article}


\usepackage{pgfplots, pgfplotstable}
\pgfplotsset{compat=newest}

\pgfplotstableread{
Year Value
2006 63.8
2007 65.4
2008 70.3
2009 79.3
2010 81.7
2011 82.8
}\datatable

\begin{document}
\begin{center}
\begin{tikzpicture}
\begin{axis}[
xmin=0,
xmax=100,
%
ymin=2006,
ymax=2011,
%
xbar stacked,
%
scaled y ticks=false,
y tick label style={/pgf/number format/1000 sep={}},
ytick=data,
%
xlabel={Something in \%},
ylabel={Years},
enlarge y limits={abs=0.5},
%
nodes near coords,
every node near coord/.append style={anchor=east}
]
\addplot
[draw=black,fill=black!60, every node near coord/.append style=white]
table [x=Value, y=Year] {\datatable};
\addplot
[draw=black,fill=white, point meta=explicit]
table [x expr=100-\thisrow{Value}-1e-5, y=Year, meta expr=100-\thisrow{Value}-1e-5] {\datatable};
\end{axis}
\end{tikzpicture}

\end{center}
\end{document}
  • That's very clever! – percusse Sep 5 '13 at 15:51

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.