4

I have a vertical bar chart with error bars. I want to put a star just above or below the error bar where the results are significant.

I achieved this using the very neat trick provided in this answer.

Here is a minimal example:

vertical bar chart with error bars

It works, but my problem is that, as opposed to what was the case in the original question, my x coordinates are symbolic. This results in error messages like this:

! Package pgfplots Error: Sorry, the input coordinate `0' has not been defined 
with 'symbolic x coords={α,β,γ,δ}... Maybe it has been misspelled?.

My real plots have a lot more stuff than that, but here is the code for producing the example above:

\documentclass{article} 
\usepackage[no-math]{fontspec}
\setmainfont{Linux Libertine O}
\usepackage[libertine]{newtxmath}
\usepackage{pgfplots}
\usepackage{filecontents}

\begin{document}

  \begin{filecontents}{data1.dat}
    X Y Y_error Label
    α -0.06 0.33 \\
    β -0.51 0.19 $\star$
    γ -0.80 0.19 $\star$
    δ 0.54 0.27 \\
  \end{filecontents}

  \begin{filecontents}{data2.dat}
    X Y Y_error Label
    α -0.04 0.33 \\
    β -0.85 0.17 $\star$
    γ 0.89 0.15 $\star$
    δ 0.36 0.31 $\star$ \\
  \end{filecontents}

  \begin{figure}
    \begin{tikzpicture} 
      \begin{axis}[
        enlargelimits=0.15, 
        symbolic x coords={α,β,γ,δ},
        xtick=data,
        ybar,
        point meta=explicit symbolic,
        nodes near coords,
        nodes near coords align={vertical},
        visualization depends on=abs(y)/y*(0.1+\thisrow{Y_error}) \as \myshift,
        every node near coord/.append style={
          anchor=center,shift={(axis direction cs:0,\myshift)},font={\bfseries},
        }]
        \addplot+[error bars/.cd, y dir=both, y explicit]
          table[x=X, y=Y, y error=Y_error, meta=Label] {data1.dat};
        \addplot+[error bars/.cd, y dir=both, y explicit]
          table[x=X, y=Y, y error=Y_error, meta=Label] {data2.dat};
      \end{axis} 
    \end{tikzpicture}
  \end{figure}

\end{document}

The heart of the problem seems to be with shift={(axis direction cs:0,\myshift)}. Having 0 for the x component doesn't make sense for symbolic coords, but I haven't found a way to express a "stand alone y shift". Am I missing something obvious?

It's not a show stopper, but I get tons of error messages and it's preventing me from tikzexternalizing those plots.

7

When only one component is required, the pgf math function transformdirectiony() should be used instead. So you can replace

shift={(axis direction cs:0,\myshift)}

with

shift={(0,transformdirectiony(\myshift))}

to apply a zero shift in the x-direction and the calculated shift in the y-direction. The complete code:

\documentclass{article} 
\usepackage[no-math]{fontspec}
\setmainfont{Linux Libertine O}
\usepackage[libertine]{newtxmath}
\usepackage{pgfplots}
\usepackage{filecontents}

\begin{document}

  \begin{filecontents}{data1.dat}
    X Y Y_error Label
    α -0.06 0.33 \\
    β -0.51 0.19 $\star$
    γ -0.80 0.19 $\star$
    δ 0.54 0.27 \\
  \end{filecontents}

  \begin{filecontents}{data2.dat}
    X Y Y_error Label
    α -0.04 0.33 \\
    β -0.85 0.17 $\star$
    γ 0.89 0.15 $\star$
    δ 0.36 0.31 $\star$ \\
  \end{filecontents}

  \begin{figure}
    \begin{tikzpicture} 
      \begin{axis}[
        enlargelimits=0.15, 
        symbolic x coords={α,β,γ,δ},
        xtick=data,
        ybar,
        point meta=explicit symbolic,
        nodes near coords,
        nodes near coords align={vertical},
        visualization depends on=abs(y)/y*(0.1+\thisrow{Y_error}) \as \myshift,
        every node near coord/.append style={
          anchor=center,shift={(0,transformdirectiony(\myshift))},font={\bfseries},
        }]
        \addplot+[error bars/.cd, y dir=both, y explicit]
          table[x=X, y=Y, y error=Y_error, meta=Label] {data1.dat};
        \addplot+[error bars/.cd, y dir=both, y explicit]
          table[x=X, y=Y, y error=Y_error, meta=Label] {data2.dat};
      \end{axis} 
    \end{tikzpicture}
  \end{figure}

\end{document}

And the output (same as your original, but with no pesky errors :-)):

enter image description here

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.