5

I would like to code something like this:

if \thecitation is {This work} then
    nodes near coords={\thenumber{} [This work]} and plot color is Red
else
    nodes near coords={\thenumber{} \cite{\thecitation}} and plot color is Blue

This is the code I used:

\begin{filecontents*}{RR.dat}
y   x   Label   Cite
1   0.613   PSB Jung:2015
2   -1.700  MRB Jung:2015
3   0.998   BC700   {This work}
4   0.903   BC600   {This work}
5   0.891   BC500   {This work}
6   0.759   BC400   {This work}
7   0.030   BG450   Yao:2012
8   -0.050  BB450   Yao:2012
9   0.381   TC600   Zeng:2013
10  0.018   PC700   Zeng:2013
11  0.667   SBT Zhang:2012
\end{filecontents*}

    \begin{tikzpicture}
    \begin{axis}[
        axis lines*=middle,
        yticklabels from table={RR.dat}{Label},
        ytick=data,
        yticklabel style={xshift=-4cm},
        nodes near coords={\thenumber{} \cite{\thecitation}},
        nodes near coords align={horizontal},
        visualization depends on={value 2.1cm*(-\thisrow{x}+max(0,\thisrow{x})) \as \myshift},
        every node near coord/.append style={xshift=\myshift},
        visualization depends on={value \thisrow{Cite} \as \thecitation},
        visualization depends on={value \thisrow{x} \as \thenumber},
        ]
        \addplot+[xcomb] table [x=x, y=y] {RR.dat};
    \end{axis}
    \end{tikzpicture}

And this the result: enter image description here

SOLVED:

\begin{filecontents*}{RR.dat}
y   x   Label   Cite
1   0.613   PSB Jung:2015
2   -1.700  MRB Jung:2015
3   0.998   BC700   {This work}
4   0.903   BC600   {This work}
5   0.891   BC500   {This work}
6   0.759   BC400   {This work}
7   0.030   BG450   Yao:2012
8   -0.050  BB450   Yao:2012
9   0.381   TC600   Zeng:2013
10  0.018   PC700   Zeng:2013
11  0.667   SBT Zhang:2012
\end{filecontents*}

\pgfplotsset{
    discard if/.style 2 args={
        x filter/.code={
            \edef\tempa{\thisrow{#1}}
            \edef\tempb{#2}
            \ifx\tempa\tempb
                \def\pgfmathresult{inf}
            \fi
        }
    },
    discard if not/.style 2 args={
        x filter/.code={
            \edef\tempa{\thisrow{#1}}
            \edef\tempb{#2}
            \ifx\tempa\tempb
            \else
                \def\pgfmathresult{inf}
            \fi
        }
    }
}


    \begin{tikzpicture}
    \begin{axis}[
        axis lines*=middle,
        yticklabels from table={RR.dat}{Label},
        ytick=data,
        yticklabel style={xshift=-4cm},
        nodes near coords={\ifthenelse{\equal{\thecitation}{This work}}{\pgfmathparse{\thenumber*100}\pgfmathprintnumber{\pgfmathresult}\% [This work]}{\pgfmathparse{\thenumber*100}\pgfmathprintnumber{\pgfmathresult}\% \cite{\thecitation}}},
        nodes near coords align={horizontal},
        visualization depends on={value \thisrow{Cite} \as \thecitation},
        visualization depends on={value \thisrow{x} \as \thenumber},
        visualization depends on={value 2.2cm*(-\thisrow{x}+max(0,\thisrow{x})) \as \myshift},
        every node near coord/.append style={xshift=\myshift},
        ]
        \addplot+[xcomb,discard if={Cite}{This work}] table [x=x, y=y] {RR.dat};
        \addplot+[xcomb,discard if not={Cite}{This work}] table [x=x, y=y] {RR.dat};
    \end{axis}
    \end{tikzpicture}
6
  • 1
    The conditional nodes near coords you can do with the xifthen package: nodes near coords={\ifthenelse{\equal{\thecitation}{This work}}{\thenumber{} [This work]}{\thenumber{} \cite{\thecitation}}}, – Tom Bombadil Oct 27 '15 at 12:46
  • @TomBombadil Would you like to turn your comment into an answer? – Gonzalo Medina Oct 27 '15 at 14:15
  • @GonzaloMedina: It's only half an answer so far, as it does not change the color as well. – Tom Bombadil Oct 27 '15 at 14:18
  • @TomBombadil I found the second half. If you turn your comment into an answer you can include my second half in order to complete this post. – Stefano Oct 27 '15 at 15:11
  • @Stefano: OK, I will do that. Give me a moment... – Tom Bombadil Oct 27 '15 at 15:24
3

Here's the collaboration that Stefano and I put together. However, it also drops the labels of the This work entries, and I don't know if that is wanted.

Hint: If one removes the discard if style from the first plot, the labels show correctly. However, the red entries then draw over the previous blue entries, which might lead to bad printing results, so it is less than optimal.

Code

\documentclass[tikz, border=2mm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.12}

\usepackage{xifthen}

\begin{filecontents*}{RR.dat}
y   x   Label   Cite
1   0.613   PSB Jung:2015
2   -1.700  MRB Jung:2015
3   0.998   BC700   {This work}
4   0.903   BC600   {This work}
5   0.891   BC500   {This work}
6   0.759   BC400   {This work}
7   0.030   BG450   Yao:2012
8   -0.050  BB450   Yao:2012
9   0.381   TC600   Zeng:2013
10  0.018   PC700   Zeng:2013
11  0.667   SBT Zhang:2012
\end{filecontents*}

\begin{document}

\pgfplotsset%
{   discard if/.style 2 args=%
    {   x filter/.code=%
        {   \edef\tempa{\thisrow{#1}}
      \edef\tempb{#2}
      \ifx\tempa\tempb
        \def\pgfmathresult{inf}
      \fi
    }
  },
  discard if not/.style 2 args=%
  { x filter/.code=%
    {   \edef\tempa{\thisrow{#1}}
      \edef\tempb{#2}
      \ifx\tempa\tempb
      \else
        \def\pgfmathresult{inf}
      \fi
    }
  }
}


\begin{tikzpicture}
  \begin{axis}
  [ axis lines*=middle,
    yticklabels from table={RR.dat}{Label},
    ytick=data,
    yticklabel style={xshift=-4cm},
    nodes near coords={\ifthenelse{\equal{\thecitation}{This work}} % if
                                        {\thenumber{} [This work]}                                      % then
                                    {\thenumber{} \cite{\thecitation}}},                    % else
    nodes near coords align={horizontal},
    every node near coord/.append style={xshift=\myshift},
    visualization depends on={value \thisrow{Cite} \as \thecitation},
    visualization depends on={value \thisrow{x} \as \thenumber},
    visualization depends on={value 2.2cm*(-\thisrow{x}+max(0,\thisrow{x})) \as \myshift},
  ]
    \addplot+[xcomb, discard if={Cite}{This work}] table [x=x, y=y] {RR.dat};
    \addplot+[xcomb, discard if not={Cite}{This work}] table [x=x, y=y] {RR.dat};
    \end{axis}
\end{tikzpicture}

\end{document}

Output

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.