10

I'm making a bar chart with pgfplots and would like to put an asterisk above some bars to show statistical significance (p < 0.05). Any ideas?

Here's an example graph:

\begin{tikzpicture}
\begin{axis}[
    symbolic x coords={(a),(b),(c),(d),(e)},
    xtick=data]
    \addplot[ybar] coordinates {
        ((a),51365) % I'd like to put an asterisk above some of these
        ((b),74531)
        ((c),52862)
        ((d),78999)
        ((e),71825)
    };
\end{axis}
\end{tikzpicture}

2 Answers2

9

I would use the nodes near coords functionality together with point meta=explicit symbolic for this:

\documentclass[border=5mm]{standalone}

\usepackage{pgfplots}

\begin{document}
\begin{tikzpicture}
\begin{axis}[
    symbolic x coords={(a),(b),(c),(d),(e)},
    xtick=data,
    ymin=0,
    point meta=explicit symbolic,
    nodes near coords    
    ]
    \addplot[ybar] table [meta index=2, header=false] {
       (a) 51365 *
       (b) 74531 \\
       (c) 52862 *
       (d) 78999 \\
       (e) 71825 \\
    };
\end{axis}
\end{tikzpicture}

\end{document}
David Carlisle
  • 757,742
Jake
  • 232,450
  • This method is slightly nicer than Peter's, so I'm marking it as the answer, thanks. Why is there a comma between Y and Sig on the first line of the table? – sjmeverett Oct 03 '12 at 13:54
  • @stewartml: Sorry, that comma wasn't supposed to be there. I've edited the answer slightly to do away with the column names. It sounds like you're not entirely happy with the answer, though? Could you elaborate on how you'd rather specify which results to highlight (or if there's anything else that's you're not happy with)? – Jake Oct 03 '12 at 14:03
  • No, I think the answer is great thanks! Although Peter's works, I prefer yours, is all I meant. Nice and straightforward. – sjmeverett Oct 03 '12 at 23:32
5

Not sure that this is a satisfactory answer but you could specify the xticklabels={(a),(b),(c),(d),(e)}, use numerical values for the x-values, and then place a node above the ones you want to highlight:

enter image description here

Code:

\documentclass{article}
\usepackage{pgfplots}

\begin{document} \begin{tikzpicture} \begin{axis}[ xticklabels={(a),(b),(c),(d),(e)}, xtick=data, ] \addplot[ybar] coordinates { (1,51365) % I'd like to put an asterisk above some of these (2,74531) (3,52862) (4,78999) (5,71825) }; \node [above, red, font=\Large] at (axis cs: 1,51365) {$\ast$}; \end{axis} \end{tikzpicture} \end{document}

Peter Grill
  • 223,288