2

I'm using the pgfplots package to generate a scatter chart with variable bubble sizes. Please, check the fallowing image:

enter image description here

However, I'm having the following issues:

  • Node labels overlap. See for instance class1 and class2 labels.
  • Bigger bubbles obscure the smaller ones. See for instance class4 and class3 bubbles.
  • Also, class1's label remains behind class2's bubble.

Here I provide a minimal example of my code:

\documentclass[border = 1cm]{standalone}

\usepackage{tikz} % https://www.ctan.org/pkg/pgf
\usepackage{pgfplots} % https://www.ctan.org/pkg/pgfplots
   % -> 
   \pgfplotsset{compat = newest} 
   \usetikzlibrary{plotmarks, positioning}

\begin{filecontents}{data.dat}
c1  c2  c3      c4
2   2   class1  10
2.3 2   class2  40
6.3 1.5 class3  10
7   2   class4  30
3   4   class5  10
\end{filecontents}

\begin{document}

\begin{tikzpicture}
   \begin{axis}[
      scatter/classes = {
         class1={mark = *, green, opacity = .7},
         class2={mark = *, blue, opacity = .7},
         class3={mark = *, red, opacity = .7},
         class4={mark = *, yellow, opacity = .7},
         class5={mark = *, pink, opacity = .7}
      }]
      \addplot[                 
         scatter,
         only marks,
         scatter src = explicit symbolic,
         % Bubble sizes
         visualization depends on = {\thisrow{c4} \as \perpointmarksize},
         scatter/@pre marker code/.append style={/tikz/mark size = \perpointmarksize},
         % Node labels
         visualization depends on = {value \thisrow{c3} \as \perpointlabel},
         nodes near coords* = {\perpointlabel},
         every node near coord/.append style={
            font = \tiny,
            anchor = south
         }
      ]
      table[
         x = c1, 
         y = c2,
         meta = c3,
         col sep = space
      ]{data.dat};
   \end{axis}
\end{tikzpicture}

\end{document}

Is there a way to effectively solve the three issues mentioned above?

2
  • Can reduce the size so that they don't overlap?
    – nidhin
    Nov 1, 2019 at 15:05
  • I can't @nidhin, the chart is meant to be in a research paper and there's not much space available. Nov 1, 2019 at 15:40

1 Answer 1

3

I do not have a magical recipe that always works, but in this case changing the order and moving the nodes according to the sizes help quite a bit.

\documentclass[border = 1cm]{standalone}
\usepackage{pgfplots} % https://www.ctan.org/pkg/pgfplots
\pgfplotsset{compat = newest} 
\begin{filecontents}{data.dat}
c1  c2  c3      c4
2.3 2   class2  40
7   2   class4  30
2   2   class1  10
6.3 1.5 class3  10
3   4   class5  10
\end{filecontents}

\begin{document}

\begin{tikzpicture}
   \begin{axis}[
      scatter/classes = {
         class1={mark = *, green, opacity = .7},
         class2={mark = *, blue, opacity = .7},
         class3={mark = *, red, opacity = .7},
         class4={mark = *, yellow, opacity = .7},
         class5={mark = *, pink, opacity = .7}
      }]
      \addplot[                 
         scatter,
         only marks,
         scatter src = explicit symbolic,
         % Bubble sizes
         visualization depends on = {\thisrow{c4} \as \perpointmarksize},
         scatter/@pre marker code/.append style={/tikz/mark size = \perpointmarksize},
         % Node labels
         visualization depends on = {value \thisrow{c3} \as \perpointlabel},
         nodes near coords* = {\perpointlabel},
         every node near coord/.append style={
            font = \tiny,
            anchor = south,
            yshift=-5+0.012*\perpointmarksize*\perpointmarksize
         }
      ]
      table[
         x = c1, 
         y = c2,
         meta = c3,
         col sep = space
      ]{data.dat};
   \end{axis}
\end{tikzpicture}

\end{document}

enter image description here

If needed, the sorting could also be done by pgfplotstable.

1
  • Thank you Schrödinger's cat, the sorting helped a lot actually. Nov 1, 2019 at 18:44

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .