Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I am collecting data using the NASA Task Load Index, which gives 6 scales and has a weight associated with each scale. I am trying to plot the mean value for each scale using a ybar interval graph, whereby the width of each bar is set by the weight for the corresponding scale, similar to the picture below taken from the manual.

Desired output

I have managed to create an additional column that should give me the right interval data (see table), but the bar width doesn't look correct, nor am I able to then use the symbolic x coords as labels for the bars.

\documentclass{article}

\usepackage{pgfplots}
\usepackage{pgfplotstable}

\begin{document}

\pgfplotstableread{
  Scale    Mean  Weight
  MD      53.25    3.50
  PD      12.22    1.50
  TD      18.46    1.75
  FR      45.75    3.00
  OP      59.18    2.45
  EF      22.15    2.80
  Overall 38.175   2.50
}{\tlxweightedratings}

\pgfplotstabletypeset[
  columns/Scale/.style={string type},
  create on use/Interval/.style={
    create col/expr={\pgfmathaccuma + \thisrow{Weight}}},
  columns={Scale, Mean, Weight, Interval},
]{\tlxweightedratings}

\begin{tikzpicture}
  \begin{axis}[ybar interval, xtick=data,
      nodes near coords,
      %symbolic x coords={MD, PD, TD, FR, OP, EF, Overall},
  ]
    \addplot+ table[x=Interval, y=Mean,
      create on use/Interval/.style={
        create col/expr={\pgfmathaccuma + \thisrow{Weight}}},
    ] {\tlxweightedratings};
  \end{axis}
\end{tikzpicture}
\end{document}
share|improve this question
add comment

1 Answer

up vote 4 down vote accepted

The ybar interval plot style uses the x value of the current data point as the left edge of the column, and the x value of the next data point as the right edge of the column. With your method of calculating the Interval, the first column starts at 3.5 and stretches to 5, so it has a width of 1.5. You want it to start at 0 and stretch to 3.5, however, so you should change your code to use \prevrow instead of \thisrow. You also need to add a dummy data row to your table, so the width of the last column can be calculated as well.

You don't actually want symbolic x coordinates, since they're always equally spaced. Simply use xticklabels={MD, ... to set the labels.

nodes near coordinates don't currently work satisfactorily with ybar interval, since they're always positioned above the start of the columns, not their centers.

\documentclass{article}

\usepackage{pgfplots}
\usepackage{pgfplotstable}

\begin{document}

\pgfplotstableread{
  Scale    Mean  Weight
  MD      53.25    3.50
  PD      12.22    1.50
  TD      18.46    1.75
  FR      45.75    3.00
  OP      59.18    2.45
  EF      22.15    2.80
  Overall 38.175   2.50
  Dummy    0       0
}{\tlxweightedratings}

\pgfplotstabletypeset[
  columns/Scale/.style={string type},
  create on use/Interval/.style={
    create col/expr={\pgfmathaccuma + \thisrow{Weight}}},
  columns={Scale, Mean, Weight, Interval},
]{\tlxweightedratings}

\begin{tikzpicture}
  \begin{axis}[
    ymin=0,
    ybar interval, xtick=data,
      xticklabels={MD, PD, TD, FR, OP, EF, Overall},
  ]
    \addplot+ table[x=Interval, y=Mean,
      create on use/Interval/.style={
        create col/expr={\pgfmathaccuma + \prevrow{Weight}}},
    ] {\tlxweightedratings};
  \end{axis}
\end{tikzpicture}
\end{document}
share|improve this answer
1  
Spot on as usual, thanks a million. I don't actually need the overall column in this chart, so it serves as a nice dummy column. Encouraging to see that I wasn't completely off either :-) –  ThomasH Jul 11 '12 at 16:49
add comment

Your Answer

 
discard

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.