I'm trying to recreate this bar chart from the data table found in the MWE below.

It involves doing a couple things that I can't figure out:
- stack positive and negative bars on the same axis, such that the negative numbers don't merely subtract from the positive ones (this is the default behavior of
xbar stacked) - selecting only certain rows from the table. For the blue bars I need only rows 1-3 (counting the column headers as row 0). For the red bars I need two instances of row 4 and one of row 5.
- creating the lines that show the values from the SUM columns and creating data labels.
- creating a legend using the column headers of columns 2 and 3 (even though column 3 contains no real data).
- reverse the ordering of the items on the y axis. Notice that the categories in the MWE count down from top to bottom; I'd like the opposite.
You'll see where I've got so far by looking at the MWE. As for the rest, I'm rather baffled. I need to automate this process and this is the format the data comes in (so reformatting the underlying data table is a suboptimal solution).
MWE:
\documentclass{article}
\usepackage{pgfplots}
\usepackage{filecontents}
\begin{document}
\begin{filecontents}{data.dat}
category,value,negvalue
cat1,3,0
cat2,5,0
cat3,1,0
cat1and2n,-6,0
cat3n,-3,0
cat1sum,-3,0
cat2sum,-1,0
cat3sum,-2,0
\end{filecontents}
\pgfplotstableset{col sep=comma}
\pgfplotstableread{data.dat}\datatable
\begin{tikzpicture}
\begin{axis}[
xbar,
yticklabels from table={\datatable}{category},
ytick=data]
\addplot table[
y expr=\coordindex,
x=value]
{data.dat};
\end{axis}
\end{tikzpicture}
\end{document}
I appreciate any help!
pgfplotstable,datatoolorlua. Look, for example, at: pgfplotstable or datatool?, Insert libreoffice table as input, Rearrange data in pgfplotstable – Ignasi Jul 23 '12 at 07:36