how to plot Correlation matrix between several variable with circle?
10 views (last 30 days)
Show older comments
I need a Correlation matrix plot same as this
is it anyone can help me?
0 Comments
Answers (1)
Cris LaPierre
on 20 Feb 2022
There is no single function for creating this plot, so you would have to build it up using bits and pieces from several functions. Here is an example that should get you started. It uses
% create sample data set
X = randn(30,4);
Y = randn(30,4);
Y(:,4) = Y(:,4)+X(:,2);
[rho,pval] = corr(X,Y)
% gray squares via imagesc
c = double(pval>=0.05);
c(c==0)=0.8; % result would be white/black, so change black to gray
imagesc(c,[0 1]) % imagesc autoscales to the data range, so force range of [0 1]
colormap gray
xticks(1:size(c,2))
yticks(1:size(c,1))
axis equal
axis tight
xline(1.5:size(c,2)-0.5,'--','Color',[0.8 0.8 0.8]) % xgrid
yline(1.5:size(c,1)-0.5,'--','Color',[0.8 0.8 0.8]) % ygrid
% add bubbles
hold on
[X,Y] = meshgrid(1:size(c,2),1:size(c,1));
bubblechart(X(rho(:)<0),Y(rho(:)<0),rho(rho(:)<0),'MarkerFaceAlpha',0.20) % blue
bubblechart(X(rho(:)>=0),Y(rho(:)>=0),rho(rho(:)>=0),'MarkerFaceAlpha',0.20) % red
% set min/max bubble size
bubblesize([5 20])
bubblelegend('Location','eastoutside')
hold off
2 Comments
Cris LaPierre
on 23 Feb 2022
Edited: Cris LaPierre
on 23 Feb 2022
There is no colorbar. That is a legend for a bubblechart. You can explore colorbar options, but the problem is your plot does not have a range of colors that would make having a colorbar necessary. It has 2 hard-coded colors (red and blue), making a legend more appropriate. It is not from -1-1 because it is a legend of the data in your plot, not a colorbar.
Instead, it was my guess that it was the size of the bubble that indicated the strength of the correlation.
You should review the image you shared to determine what the grey boxes mean. I based this figure off that.
I did overlook the diagonal. Not promising this is the best apprach, but here's one way.
% create sample data set
X = randn(30,4);
Y = randn(30,4);
Y(:,4) = Y(:,4)+X(:,2);
[rho,pval] = corr(X,Y)
% gray squares via imagesc
c = double(pval>=0.05);
d = diag(ones(length(pval),1));
c(d==1)=1;
c(c==0)=0.8; % result would be white/black, so change black to gray
imagesc(c,[0 1]) % imagesc autoscales to the data range, so force range of [0 1]
colormap gray
xticks(1:size(c,2))
yticks(1:size(c,1))
axis equal
axis tight
xline(1.5:size(c,2)-0.5,'--','Color',[0.8 0.8 0.8]) % xgrid
yline(1.5:size(c,1)-0.5,'--','Color',[0.8 0.8 0.8]) % ygrid
% add bubbles
hold on
[X,Y] = meshgrid(1:size(c,2),1:size(c,1));
bubblechart(X(rho(:)<0 & d(:)==0),Y(rho(:)<0 & d(:)==0),rho(rho(:)<0 & d(:)==0),'MarkerFaceAlpha',0.20) % blue
bubblechart(X(rho(:)>=0 & d(:)==0),Y(rho(:)>=0 & d(:)==0),rho(rho(:)>=0 & d(:)==0),'MarkerFaceAlpha',0.20) % red
% set min/max bubble size
bubblesize([5 20])
hold off
See Also
Categories
Find more on Data Distribution Plots in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!