3

Using this example for a scatter plot, I hacked together a working plot for my document. To perfectionize my plot, I would like to have each plotted point (circle or square) to also be a hyperlink to the rest of my text -- for user comfort. I.e. if I click on the blue circle 19 I get taken to the text section "Hypothesis 19".

Is there a way for pgfplotstable to read label references from the table and hyperlink to the referred part, such as I sketched in the commented out data table?

A scatter plot, that should have hyperlinks as points, linking to other parts of the document.

\documentclass{article}

\usepackage[utf8]{inputenc}
\usepackage{graphicx}
\usepackage{pgfplotstable}
\pgfplotsset{compat=1.17}


\colorlet{myred}{red!40!white}
\colorlet{myblue}{blue!30!white}


\begin{document}


\begin{tikzpicture}
\begin{axis}[
xtick={-1.5,-0.5,0.5,1.5},
x tick label style={align=center},
xticklabels={strongly\\disagree,disagree,agree,strongly\\agree},
xmin=-1.55,
xmax=1.55,
ymin=0.385,
ymax=0.715,
xlabel=Mean Agreement,
ytick={0.4,0.5,0.6,0.7},
y tick label style={align=center},
yticklabels={more\\controversial,0.5,0.6,more\\consensual},
ylabel=Consensus,
xmajorgrids=true,
ymajorgrids=true,
grid style={ultra thin, gray!20!white},
every node near coord/.append style={anchor=center,font=\footnotesize,color=black},
]

\addplot+[
    scatter, only marks, mark size=4.25pt,
    scatter/@pre marker code/.append code={%
        \let\pgfplotspointmeta=\mediansymbol
    },%
    scatter/classes*={
        CIRCLE={mark=*,myblue},%
        SQUARE={mark=square*,myred}},%
    nodes near coords*={\pgfmathprintnumber[int detect]{\hypothesisnumber}},
    visualization depends on={value \thisrow{mediansymbol} \as \mediansymbol},
    visualization depends on={\thisrow{hypothesisnumber} \as \hypothesisnumber},
]
    table[meta=mediansymbol]{
%what it currently looks like (column `empty' is not used)
x   y   empty   mediansymbol   hypothesisnumber
-0.2407407407   0.5826392204        0   SQUARE  11
0.7413793103    0.5908883524        0   CIRCLE  19
%what it should look like
%x   y   empty   mediansymbol   hypothesisnumber    hyperlinkto
%-0.2407407407  0.5826392204        0   SQUARE  11  hypo:oneone
%0.7413793103   0.5908883524        0   CIRCLE  19  hypo:onenine
    };
\coordinate (legend) at (axis description cs:0.05,0.11);
\end{axis}

%manually hacked legend:
\node[rectangle,minimum size=8.5pt,fill=myred,label=right:{median disagree},yshift=1.3em] at (legend) {};
\node[circle,minimum size=8.5pt,fill=myblue,label=right:{median agree},yshift=-0.5em] at (legend) {};
\end{tikzpicture}




\textit{Hypothesis 19. Perfecting a \textit{pgftablesplot} is not a $80\%-20\%$ ratio, try $99\%-1\%$.}
\label{hypo:onenine}
This hypothesis is a tautology, qed.


\end{document}

(note, that I could not get rid of the data column "empty", which serves no actual purpose -- I also had to add a legend, manually)

2 Answers 2

3

You can read the text value of the labels from the data using visualization depends on={value \thisrow{hyperlinkto}. Note the value keyword, which reads the value as text instead of trying to interpret it as a number (similar to the code for the median symbol). Then you can add the link to the node using \hyperref[label]{text} from the hyperref package in nodes near coords.

Relevant code snippet:

nodes near coords*={\hyperref[\hypothesislabel]{\pgfmathprintnumber[int detect]{\hypothesisnumber}}},
visualization depends on={value \thisrow{mediansymbol} \as \mediansymbol},
visualization depends on={\thisrow{hypothesisnumber} \as \hypothesisnumber},
visualization depends on={value \thisrow{hyperlinkto} \as \hypothesislabel},

Note that you need something that you can actually reference, like a section in the MWE below - of course in the real document you can use the amsthm package or something similar to define a hypothesis environment that has an automatic counter and that can be labeled, to look more like your current \textit output.

For showing the link the code below uses \usepackage[colorlinks]{hyperref}, which makes the text of the link a color (default red) instead of printing a link box around it, which does not look good in a plot.

MWE:

\documentclass{article}
\usepackage[colorlinks]{hyperref}
\usepackage[utf8]{inputenc}
\usepackage{graphicx}
\usepackage{pgfplotstable}
\pgfplotsset{compat=newest}


\colorlet{myred}{red!40!white}
\colorlet{myblue}{blue!30!white}


\begin{document}


\begin{tikzpicture}
\begin{axis}[
xtick={-1.5,-0.5,0.5,1.5},
x tick label style={align=center},
xticklabels={strongly\\disagree,disagree,agree,strongly\\agree},
xmin=-1.55,
xmax=1.55,
ymin=0.385,
ymax=0.715,
xlabel=Mean Agreement,
ytick={0.4,0.5,0.6,0.7},
y tick label style={align=center},
yticklabels={more\\controversial,0.5,0.6,more\\consensual},
ylabel=Consensus,
xmajorgrids=true,
ymajorgrids=true,
grid style={ultra thin, gray!20!white},
every node near coord/.append style={anchor=center,font=\footnotesize,color=black},
]

\addplot+[
    scatter, only marks, mark size=4.25pt,
    scatter/@pre marker code/.append code={%
        \let\pgfplotspointmeta=\mediansymbol
    },%
    scatter/classes*={
        CIRCLE={mark=*,myblue},%
        SQUARE={mark=square*,myred}},%
    nodes near coords*={\hyperref[\hypothesislabel]{\pgfmathprintnumber[int detect]{\hypothesisnumber}}},
    visualization depends on={value \thisrow{mediansymbol} \as \mediansymbol},
    visualization depends on={\thisrow{hypothesisnumber} \as \hypothesisnumber},
    visualization depends on={value \thisrow{hyperlinkto} \as \hypothesislabel},
]
    table[meta=mediansymbol]{
%what it currently looks like (column `empty' is not used)
%x   y   empty   mediansymbol   hypothesisnumber
%-0.2407407407   0.5826392204        0   SQUARE  11
%0.7413793103    0.5908883524        0   CIRCLE  19
%what it should look like
x   y   empty   mediansymbol   hypothesisnumber    hyperlinkto
-0.2407407407  0.5826392204        0   SQUARE  11  hypo:oneone
0.7413793103   0.5908883524        0   CIRCLE  19  hypo:onenine
    };
\coordinate (legend) at (axis description cs:0.05,0.11);
\end{axis}

%manually hacked legend:
\node[rectangle,minimum size=8.5pt,fill=myred,label=right:{median disagree},yshift=1.3em] at (legend) {};
\node[circle,minimum size=8.5pt,fill=myblue,label=right:{median agree},yshift=-0.5em] at (legend) {};
\end{tikzpicture}




\section{Hypothesis 19}
\label{hypo:onenine}
\textit{Perfecting a \textit{pgftablesplot} is not a $80\%-20\%$ ratio, try $99\%-1\%$.}
This hypothesis is a tautology, qed.


\end{document}

Result:

enter image description here

3

This is similar to @Marijn answer. I created a "plain text" macro with the hypothesis number with

 visualization depends on={value \thisrow{hypothesisnumber} \as \hypothesislink}

But, instead of using a section to link to, I created a target (see "additional user macros" in the hyperref manual) like this:

 \hyperdef{hypo}{19}{Hypothesis 19}

and then referenced it with

nodes near coords*={%
        \hyperref{}{hypo}{\hypothesislink}%
        {\pgfmathprintnumber[int detect]{\hypothesisnumber}}%
    },

That way you can more or less link wherever you want in the document. How precise will be the viewer when following the link will mainly depend on the viewer itself.

Full code:

\documentclass{article}

\usepackage[utf8]{inputenc}
\usepackage{graphicx}
\usepackage{pgfplotstable}
\pgfplotsset{compat=1.17}


\colorlet{myred}{red!40!white}
\colorlet{myblue}{blue!30!white}

\usepackage{lipsum}
\usepackage{hyperref}

\begin{document}


\begin{tikzpicture}
\begin{axis}[
xtick={-1.5,-0.5,0.5,1.5},
x tick label style={align=center},
xticklabels={strongly\\disagree,disagree,agree,strongly\\agree},
xmin=-1.55,
xmax=1.55,
ymin=0.385,
ymax=0.715,
xlabel=Mean Agreement,
ytick={0.4,0.5,0.6,0.7},
y tick label style={align=center},
yticklabels={more\\controversial,0.5,0.6,more\\consensual},
ylabel=Consensus,
xmajorgrids=true,
ymajorgrids=true,
grid style={ultra thin, gray!20!white},
every node near coord/.append style={anchor=center,font=\footnotesize,color=black},
]

\addplot+[
    scatter, only marks, mark size=4.25pt,
    scatter/@pre marker code/.append code={%
        \let\pgfplotspointmeta=\mediansymbol
    },%
    scatter/classes*={
        CIRCLE={mark=*,myblue},%
        SQUARE={mark=square*,myred}},%
    nodes near coords*={%
        \hyperref{}{hypo}{\hypothesislink}%
        {\pgfmathprintnumber[int detect]{\hypothesisnumber}}%
    },
    visualization depends on={value \thisrow{mediansymbol} \as \mediansymbol},
    visualization depends on={\thisrow{hypothesisnumber} \as \hypothesisnumber},
    visualization depends on={value \thisrow{hypothesisnumber} \as \hypothesislink}
]
    table[meta=mediansymbol]{
%what it currently looks like (column `empty' is not used)
x   y   empty   mediansymbol   hypothesisnumber
-0.2407407407   0.5826392204        0   SQUARE  11
0.7413793103    0.5908883524        0   CIRCLE  19
%what it should look like
%x   y   empty   mediansymbol   hypothesisnumber    hyperlinkto
%-0.2407407407  0.5826392204        0   SQUARE  11  hypo:oneone
%0.7413793103   0.5908883524        0   CIRCLE  19  hypo:onenine
    };
\coordinate (legend) at (axis description cs:0.05,0.11);
\end{axis}

%manually hacked legend:
\node[rectangle,minimum size=8.5pt,fill=myred,label=right:{median disagree},yshift=1.3em] at (legend) {};
\node[circle,minimum size=8.5pt,fill=myblue,label=right:{median agree},yshift=-0.5em] at (legend) {};
\end{tikzpicture}

\lipsum

\textit{%
\hyperdef{hypo}{19}{Hypothesis 19}. Perfecting a \textit{pgftablesplot} is not a $80\%-20\%$ ratio, try $99\%-1\%$.}
This hypothesis is a tautology, qed.

\lipsum

\hyperdef{hypo}{11}{Hypothesis 11}. Again Perfecting a \textit{pgftablesplot} is not a $80\%-20\%$ ratio, try $99\%-1\%$.
This hypothesis is a tautology, qed.

\end{document}

output, showing the internal link in Okular

3
  • You already went above and beyond with your solution! This solves a lot of editing and tiny errors! I am fascinated with all your guys' latex magic! May 24 at 15:58
  • Nice improvement of my approach, +1. Note that my name ends with rijn - but I don't blame you, outside of The Netherlands and Belgium nobody knows how to spell my name (let alone pronounce it...)
    – Marijn
    May 24 at 17:19
  • 1
    @Marijn oops, fixed. I sympathize, my (real) name is also often misspelled (double consonants are basically unknown outside Italy...)
    – Rmano
    May 24 at 19:37

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.