43

I have 42 variables and I have calculated the correlation matrix for them in Matlab. Now I would like to visualize it with a schemaball. Does anyone have any suggestions / experiences how this could be done in Matlab? The following pictures will explain my point better:

SCHEMABALL

enter image description here

In the pictures each parabola between variables would mean the strength of correlation between them. The thicker the line is, the more correlation. I prefer the style of picture 1 more than the style in picture 2 where I have used different colors to highlight the strength of correlation.

Amro
  • 123,847
  • 25
  • 243
  • 454
jjepsuomi
  • 4,223
  • 8
  • 46
  • 74
  • You assume the matrix is symmetric? what about begative values? – Shai Jun 11 '13 at 08:25
  • 1
    you might be able to hack this http://www.mathworks.com/matlabcentral/fileexchange/19580-plot-a-directed-graph-v2-0 to get what you want – Shai Jun 11 '13 at 08:28
  • Yes, the matrix is symmetric and no negative values Shai =) – jjepsuomi Jun 11 '13 at 10:05
  • Do you guys think that to draw those parabolas the directrix passes through the center and the focal point is the midpoint of the chord that links the two correlated pairs (points on the circle)? I can give it a shot and probably will post on the FEX. – Oleg Jun 11 '13 at 11:19
  • @OlegKomarov post here, why FEX? – Shai Jun 11 '13 at 11:58
  • @jjepsuomi how about drawing negative correlations with different colors. – Shai Jun 11 '13 at 11:58
  • @Shai No problem with that =) Thank you for your help! =) – jjepsuomi Jun 11 '13 at 11:59
  • 1
    you can find the source of OP's images [here](http://mkweb.bcgsc.ca/schemaball/?Perl_code). I reverse-engineered it a bit and conclude that the connecting lines are bezier curves with three control points: two at the border of the circle and a third at configurable distance from the center of the circle (default `0.1*R`). If @OlegKomarov doesn't come through, I'll try to look at it myself later this day – Gunther Struyf Jun 11 '13 at 12:06
  • 1
    I will try with the parabolas but currently busy and might post something tomorrow. – Oleg Jun 11 '13 at 12:19
  • 2
    Possible duplicate of [Drawing a network of nodes in circular formation with links between nodes](https://stackoverflow.com/questions/5804468/drawing-a-network-of-nodes-in-circular-formation-with-links-between-nodes) – Owen Pauling Oct 22 '17 at 20:25

5 Answers5

25

Kinda finished I guess.. code can be found here at github. Documentation is included in the file.

The yellow/magenta color (for positive/negative correlation) is configurable, as well as the fontsize of the labels and the angles at which the labels are plotted, so you can get fancy if you want and not distribute them evenly along the perimeter/group some/...

If you want to actually print these graphs or use them outside matlab, I suggest using vector formats (eg eps). It's also annoying that the text resizes when you zoom in/out, but I don't know of any way to fix that without hacking the zoom function :/

schemaball % demo

enter image description here

schemaball(arrayfun(@num2str,1:10,'uni',false), rand(10).^8,11,[0.1587 0.8750],[0.8333 1],2*pi*sin(linspace(0,pi/2-pi/20,10)))

enter image description here

schemaball(arrayfun(@num2str,1:50,'uni',false), rand(50).^50,9)

enter image description here

Gunther Struyf
  • 11,158
  • 2
  • 34
  • 58
  • 1
    do add them! what toolboxes are you using? – Shai Jun 12 '13 at 05:49
  • 1
    is it too much to ask for supporting negative weights as well? i.e., positive correlations in yellow and negative in magenta? – Shai Jun 12 '13 at 05:50
  • 1
    no toolbox, just plain old matlab (plot and scatter). I'll put a switch in for negative correlations when I continue work on this (later this day).. – Gunther Struyf Jun 12 '13 at 07:31
  • +1 Thank you for your excellent answer @GuntherStruyf ! =D Appreciate the help! – jjepsuomi Jun 12 '13 at 20:12
  • Superb. Unfortunately Matlab's non-antialiansed graphics and text from the 1990s really hurt the aesthetics. How about options to use LaTeX interpreted text and anti-aliased lines via the undocumented `'LineSmoothing'`,`'on'` property? These might be tricky though, as both are can be buggy, especially LaTeX. – horchler Jun 13 '13 at 16:23
  • 2
    @horchler I just tested that `LineSmoothing` flag, but it didn't change that much, couldn't see a difference to be honest. That's why I advised to use vector formats if you export it. My experience with using such methods are quite good: [link](http://stackoverflow.com/questions/13788168/matlab-export-plot-to-vector-format/13788467#13788467) – Gunther Struyf Jun 13 '13 at 20:49
  • @horchler Yes, vector formats are much better. To get good-quality rasterized version just use ghostscript to export EPS to, say, PNG. – anandr Jun 14 '13 at 15:00
  • 1
    @GuntherStruyf Really nice job! Have you thought about 3D version? ;o) – anandr Jun 14 '13 at 15:05
  • @anandr Nice idea and I definitely think that would be possible (just a 3d version of the bezier curve, control points are also well-defined imo) and I also have some code lying around to evenly distribute points over a sphere. But I think the 2d printouts of the 3d graph gets messy quickly. It would only be useful for in-matlab 3d manipulation. But who knows... if your comment gets enough upvotes for interest, I might just implement it ;) – Gunther Struyf Jun 14 '13 at 15:23
  • @GuntherStruyf I agree that 2D printout of the 3D graph is not so informative. Actually I was thinking about animated/rotated 3D graph (possibly with some sort of 3D fog) for some video or presentation -- it may look quite good in some cases. – anandr Jun 14 '13 at 17:01
  • @GuntherStruyf and @anandr: Yes, of course for exporting. But we'd all like our visualizations to look nice and not hurt our eyes when working live in Matlab as we interact with them. Not all plots and graphics have the goal of being exported for static media. And I'm surprised that you didn't see any difference with `'LineSmoothing'` -perhaps you set the `'Renderer'` to `'painters'`? – horchler Jun 14 '13 at 17:24
  • @horchler mmz, yes I did, got some annoying warnings, I'll give it another go – Gunther Struyf Jun 14 '13 at 20:35
  • It improves the curves yes, but doesn't work for improving the text. So part of the plot is still aliased. I'd also leave it out because [it breaks exporting](http://undocumentedmatlab.com/blog/plot-linesmoothing-property/#comment-211246) and gets deprecated when matlab by default ships with that new renderer – Gunther Struyf Jun 14 '13 at 21:23
25

I finished and submitted my version to the FEX: schemaball and will update the link asap.

There are a some differences with Gunther Struyf's contribution:

  1. You can return the handles to the graphic object for full manual customization
  2. Labels are oriented to allow maximum left-to-rigth readability
  3. The figure stretches to fit labels in, leaving the axes unchanged
  4. Syntax requires only correlations matrix (but allows optional inputs)
  5. Optimized for performance.

Follow examples of demo, custom labels and creative customization. Note: the first figure was exported with saveas(), all others with export_fig.

schemaball

enter image description here enter image description here

x      = rand(10).^3;
x(:,3) = 1.3*mean(x,2);
schemaball(x, {'Hi','how','is','your','day?', 'Do','you','like','schemaballs?','NO!!'})

enter image description here

h = schemaball;
set(h.l(~isnan(h.l)), 'LineWidth',1.2)
set(h.s, 'MarkerEdgeColor','red','LineWidth',2,'SizeData',100)
set(h.t, 'EdgeColor','white','LineWidth',1)

enter image description here

The default colormap:

enter image description here

To improve on screen rendering you can launch MATLAB with the experimental -hgVersion 2 switch which produces anti/aliased graphics by default now (source: HG2 update | Undocumented Matlab). However, if you try to save the figure, the file will have the usual old anti-aliased rendering, so here's a printscreen image of Gunther's schemaball:

enter image description here

Community
  • 1
  • 1
Oleg
  • 10,406
  • 3
  • 29
  • 57
  • I am on the way of writing a function as well, requests for functionalities can be posted here. Once done I will update my answer.Also, I couldn-t post an image in a comment. – Oleg Jun 14 '13 at 16:01
  • +1 for trying HG2 - it's starting to look *really* nice. Which version of MATLAB did you do that in? Might be interesting to see what it looks like in the 13b prerelease. – Sam Roberts Jun 15 '13 at 07:10
  • @SamRoberts I did that in R2013a, but not planning to explore the pre-release. On a different note, I have produced my version of schemaball but I am currently unhappy with its design since it works quite well but for high dimensional correlation matrices. For instance, a 1e3 x 1e3 matrix could take quite a while to plot, if at all, and the visual perception of differences in colors can offer necessary performance improvements. Also, positioning of nodes, although minor, should be handled such that the markers do not overlap. Therefore, it will take me some more time to release it. – Oleg Jun 17 '13 at 14:08
  • 1
    very nice. taking back my initial comments +1! – Shai Jun 18 '13 at 14:39
  • nice, you apparently took it a step further :p – Gunther Struyf Jun 26 '13 at 08:44
  • Oh my god, this is more Matlab that I would ever expect to see. Amazing. Really amazing. You deserve a prize. – Ander Biguri Jun 26 '14 at 12:06
  • is it possible to display the colorbar for the connections? – shamalaia Jan 12 '17 at 14:19
5

Important update:

You can do this in Matlab now with the FileExchange submission:

http://www.mathworks.com/matlabcentral/fileexchange/48576-circulargraph

There is an exmample by Matlab in here:

http://uk.mathworks.com/examples/matlab/3859-circular-graph-examples

Which gives this kind of beautiful plots:

enter image description here

enter image description here

Ander Biguri
  • 35,140
  • 11
  • 74
  • 120
3

Coincidentally, Cleve Moler (MathWorks Chief Mathematician) showed an example of just this sort of plot on his most recent blog post (not nearly as beautiful as the ones in your example, and the connecting lines are straight rather than parabolic, but it looks functional). Unfortunately he didn't include the code directly, but if you leave him a comment on the post he's usually very willing to share things.

What might be even nicer for you is that he also applies (and this time includes) code to permute the rows/columns of the array in order to maximize the spatial proximity of highly connected nodes, rather than randomly ordering them around the circumference. You end up with a 'crescent'-shaped envelope of connecting lines, with the thick bit of the crescent representing the most highly connected nodes.

Unfortunately however, I suspect that if you need to enhance his code to get the very narrow, high-resolution lines in your example plots, then MATLAB's currently non-anti-aliased graphics aren't quite up to it yet.

Sam Roberts
  • 23,951
  • 1
  • 40
  • 64
  • 1
    @SamRoberts: the code is available on that Xerox PARC ftp site mentioned in the article. In fact I just posted an [answer](http://stackoverflow.com/a/17094448/97160) based on it before I saw this question :) – Amro Jun 14 '13 at 00:46
2

I've recently been experimenting with MATLAB data and the D3 visualization library for similar graphs - there are several related types of circular visualizations you may be interested in and many of them are interactive. Another helpful, well-baked, and freely available option is Circos which is probably responsible for most of the prettier versions of these graphs you've seen in popular press.

MSommer
  • 21
  • 1
  • And how can you take advantage of the packages you listed with MATLAB? An exaple would be great. – Oleg Jun 24 '13 at 20:49
  • Sure, i'll try to provide some code examples in the next day or so, but in general using D3 is just a matter of exporting your matlab data into a JSON format that matches your visualization choice using something like [JSONLab](http://iso2mesh.sourceforge.net/cgi-bin/index.cgi?jsonlab) This is of course visualized outside of the matlab environment, but per Sam's answer above, it's easy to format/create JSON for a sparse co-occurrence matrix and load it in directly to something like this [D3 viz](http://mbostock.github.io/d3/talk/20111116/bundle.html) Haven't used Circos yet. – MSommer Jun 24 '13 at 22:17