2

I have the following MWE:

\documentclass[12pt]{article}
\usepackage{pgfplots}
\usepackage{pgfplotstable}
\pgfplotsset{compat=newest}

\usepackage{filecontents}% write data file to make make MWE self contained 
\begin{filecontents*}{data.dat}
    type,   value,  text
    a,      4.8,    4.8\textsuperscript{\textdagger}
    b,      9.7,    9.7\textsuperscript{\textdagger\textdagger}
    c,      -1.4,   -1.4    
\end{filecontents*}

\begin{document}

\pgfplotstableread[col sep=comma]{data.dat}\datatable

\begin{tikzpicture}
\begin{axis}[
    enlarge x limits={abs=1cm},
    xbar,
    nodes near coords,
    y axis line style = { opacity = 0 },
    axis x line       = none,
    tickwidth         = 0pt,
    y dir = reverse,
    ytick=data,
    yticklabels from table={\datatable}{type},
    ]
    \addplot table [y expr=\coordindex,x=value]{\datatable};
\end{axis}
\end{tikzpicture}

\end{document}

which gives:

Now imagine I want display the content of the column text as label just right of the bars. (As you might guess my objective is to add significance levels to some numbers, and that is the easiest way I have found to do so.)

So, I simply need to add point meta=explicit symbolic in the axis options and declare meta=text in the plot. For completeness, this is the code with these two things added:

\documentclass[12pt]{article}
\usepackage{pgfplots}
\usepackage{pgfplotstable}
\pgfplotsset{compat=newest}

\usepackage{filecontents}% write data file to make make MWE self contained 
\begin{filecontents*}{data.dat}
    type,   value,  text
    a,      4.8,    4.8\textsuperscript{\textdagger}
    b,      9.7,    9.7\textsuperscript{\textdagger\textdagger}
    c,      -1.4,   -1.4    
\end{filecontents*}

\begin{document}

\pgfplotstableread[col sep=comma]{data.dat}\datatable

\begin{tikzpicture}
\begin{axis}[
    enlarge x limits={abs=1cm},
    xbar,
    nodes near coords,
    point meta=explicit symbolic,
    y axis line style = { opacity = 0 },
    axis x line       = none,
    tickwidth         = 0pt,
    y dir = reverse,
    ytick=data,
    yticklabels from table={\datatable}{type},
    ]
    \addplot table [y expr=\coordindex,x=value,meta=text]{\datatable};
\end{axis}
\end{tikzpicture}

\end{document}

But here is the result:

As you can see the anchor of the label changes.

I have tried to play with nodes near coords align, for example specifying horizontal (which should work?), but it doesn't do anything. Specifying anchor=west makes it a bit better, but the negative value is still inside the bar, not outside it:

Ideally I would like to have the labels of the positive values right of the bars, and the labels of the negative values, left.

In the first case it seems that pgfplots knows when the label would be inside or outside the bar, and so having positive or negative values is not a problem. However when the label is pulled from textit loses this capability.

I haven't found any questions related to this problem. This one is the closest one as it displays a similar issue, but there simply adding nodes near coords align = horizontal did the trick.

  • Would something like tex.stackexchange.com/questions/258194/… be an acceptable workaround? – Torbjørn T. Oct 10 '17 at 20:12
  • A workaround would definitely work, but for this particular one I would run into more problems. To use visualization depends on I would need to rely on \thisrow{} and it does not work when the table is read through \pgfplotstableread (see here or here). I could use the (rather complex) fix proposed in one of these links, or pass the table in the \addplot directly. – darpich Oct 11 '17 at 9:29
1

Similar to my answer in How to make particular nodes near coords labels bold in a pgfplots bar chart?

Instead of having a specific text as meta column, write a number (0,1 or 2 in the example below), and use some \ifnums in nodes near coords to decide what is printed based on that number.

output of code

\documentclass[12pt,border=5mm]{standalone}
\usepackage{pgfplotstable}
\pgfplotsset{compat=newest}

\usepackage{filecontents}% write data file to make make MWE self contained 
\begin{filecontents*}{data.dat}
    type,   value,  text
    a,      4.8,    1
    b,      9.7,    2
    c,      -1.4,   0    
\end{filecontents*}

\begin{document}

\pgfplotstableread[col sep=comma]{data.dat}\datatable

\begin{tikzpicture}
\begin{axis}[
    enlarge x limits={abs=1cm},
    xbar,
    nodes near coords={
     \pgfplotstablegetelem{\coordindex}{text}\of\datatable
       \ifnum \pgfplotsretval=2
         \pgfmathprintnumber{\pgfplotspointmeta}$^{\dagger\dagger}$
       \else
         \ifnum \pgfplotsretval=1
           \pgfmathprintnumber{\pgfplotspointmeta}$^{\dagger}$
         \else 
           \pgfmathprintnumber{\pgfplotspointmeta}
       \fi\fi
    },
    node near coord style={font=\strut}, % for more consistent alignment
    y axis line style = { opacity = 0 },
    axis x line       = none,
    tickwidth         = 0pt,
    y dir = reverse,
    ytick=data,
    yticklabels from table={\datatable}{type},
    ]
    \addplot table [y expr=\coordindex,x=value]{\datatable};
\end{axis}
\end{tikzpicture}

\end{document}
  • If this isn't a viable workaround let me know, I can always delete the answer. – Torbjørn T. Oct 11 '17 at 19:17
  • That's the best workaround I've seen so far! – darpich Oct 12 '17 at 14:27

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.