4

Is there a straightforward way to create a correlation plot matrix similar to the figure below?

R corrplot with circle method

The image is an example of R's corrplot package using circles, which also offers other shapes and colour schemes to draw these plots. Ideally I'd like to do this with tikz/pgfplots.

An example of the kind of data I want to plot in a table format. The first two columns are the X and Y labels, the third contains the values (which, for example, would be represented as the circles' radii):

X   Y   value

0.7 0   0.80
1.5 0   0.80
3.8 0   0.80
9.9 0   0.80
19.7    0   0.80

0.7 1   0.61
1.5 1   0.61
3.8 1   0.64
9.9 1   0.62
19.7    1   0.61

0.7 2   0.53
1.5 2   0.54
3.8 2   0.57
9.9 2   0.53
19.7    2   0.52

0.7 5   0.52
1.5 5   0.52
3.8 5   0.55
9.9 5   0.49
19.7    5   0.49

0.7 10  0.52
1.5 10  0.53
3.8 10  0.56
9.9 10  0.50
19.7    10  0.50
  • What does your input data look like? Do you have a table of pre-calculated correlation coefficients (then it's easy), or do you want PGFPlots to calculate the coefficients (much harder and slower)? – Jake Feb 25 '16 at 9:51
  • The data is already calculated. – freieschaf Feb 25 '16 at 9:55
  • Could you include a sample of what your data format looks like (is it a matrix or a list)? – Jake Feb 25 '16 at 10:00
  • I've edited the question to include it. – freieschaf Feb 25 '16 at 10:21
  • 1
    Here is another related way to present a matrix-data diagram: tex.stackexchange.com/questions/135892/… – Steven B. Segletes Feb 26 '16 at 0:23
4

Here's a way to achieve this:

\documentclass[border=5mm]{standalone}
\usepackage{pgfplots}
\usepgfplotslibrary{colorbrewer}
\begin{document}

\begin{tikzpicture}
\begin{axis}[
    axis equal image, % We want a square grid, not a rectangular one
    scatter, % This activates the color mapping
    colormap/viridis, % Choose the colormap
    colorbar, % Add a legend
    point meta min=0,
    point meta max=1,
    grid=minor, % Grid lines between the intervals
    minor tick num=1, % One minor tick per interval
    tickwidth=0pt, % Don't draw the major tick marks
    y dir=reverse, % Y increases downward
    xticklabel pos=right, % X axis labels go to the top
    enlargelimits={abs=0.5}, % Add half a unit on all sides
    scatter/@pre marker code/.append code={% A bit of magic for scaling the circles in axis units
      \pgfplotstransformcoordinatex{sqrt(abs(\pgfplotspointmeta))}% Area scales with the square of the radius
      \scope[mark size=\pgfplotsunitxlength*\pgfmathresult/2, fill=mapped color]
    },
    scatter/@post marker code/.append code={%
      \endscope%
    }
]
\addplot +[
    point meta=explicit, % We'll provide values for the color and size
    only marks, % No lines between the points
    ] table [
    x expr={int(mod(\coordindex+0.01,5))}, % The position depends on the coordinate index, not the X or Y values
    y expr={int((\coordindex+0.01)/5))},
    meta=value
] {
X   Y   value

0.7 0   0.80
1.5 0   0.80
3.8 0   0.80
9.9 0   0.5
19.7    0   0.08

0.7 1   0.61
1.5 1   0.61
3.8 1   0.64
9.9 1   0.62
19.7    1   0.61

0.7 2   0.53
1.5 2   0.54
3.8 2   0.57
9.9 2   0.53
19.7    2   0.52

0.7 5   0.52
1.5 5   0.52
3.8 5   0.55
9.9 5   0.49
19.7    5   0.49

0.7 10  0.52
1.5 10  0.53
3.8 10  0.56
9.9 10  0.50
19.7    10  0.50
};
\end{axis}
\end{tikzpicture}
\end{document}
  • That looks very nice! I'd have to do a bit of tweaking to get the labels to show the coordinates in the data while not modifying the positions, but otherwise it seems straightforward. Thanks! – freieschaf Feb 26 '16 at 18:21

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.