TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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

example chart

It involves doing a couple things that I can't figure out:

  1. 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)
  2. 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.
  3. creating the lines that show the values from the SUM columns and creating data labels.
  4. creating a legend using the column headers of columns 2 and 3 (even though column 3 contains no real data).
  5. 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).






\pgfplotstableset{col sep=comma}

yticklabels from table={\datatable}{category},

\addplot table[
y expr=\coordindex,


I appreciate any help!

share|improve this question
If you don't want to reformat the data table 'outside' your typesetting process, you can do it 'inside' with tools like pgfplotstable, datatool or lua. Look, for example, at: pgfplotstable or datatool?, Insert libreoffice table as input, Rearrange data in pgfplotstable – Ignasi Jul 23 '12 at 7:36
Yes, I am aware of the ability to manipulate tables within tex. However, it seems like there should be a way to accomplish this plot without reformatting. It's not that complicated.... – Trevor Jul 23 '12 at 14:14

For 2.: you can filter the rows like in Calculate and draw difference of two plots automatically

    /tr/rowfilter/.style 2 args={
        /pgfplots/x filter/.append code={

but this filter is only applicable for fixed contens

\addplot table[/tr/rowfilter={category}{cat1sum}] {data.dat};

but this gives you only the 6th row. Unfortunately I don't know TeX enough to check for a pattern like 'cart[0-9]sum'

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.