云雨图(Raincloud plots)其实是可以看成核密度估计曲线图、箱形图和抖动散点图的组合图,清晰、完整、美观地展示了所有数据信息。
本质上是一个混合图,可同时将原始数据、数据分布和关键汇总统计表现出来,由对分的小提琴图(Violin plot)、箱线图(boxplot)和作为某种散点的原始数据组成。
clc
close all
clear
%% 函数说明-绘制云雨图
pathFigure= '.\Figures\' ;
figureUnits = 'centimeters';
figureWidth = 20;
figureHeight = 10;
% 导入数据
X1=[normrnd(8,4,1,120),normrnd(5,2,1,25)];
X2=[normrnd(2.5,3,1,75),normrnd(6,4,1,25),normrnd(15,1,1,100)];
X3=[normrnd(4,3,1,40),normrnd(3,4,1,25)];
X4=[normrnd(4,3,1,40),normrnd(2,4,1,75)];
dataCell={X1,X2,X3,X4}; % 把数据放到元胞数组,要是数据太多可写循环放入
dataName={'A','B','C','D'}; % 各个数据类的名称,可空着
%% 绘制横向云雨图
rainCloudsH(dataCell, dataName)
str= strcat(pathFigure, "图1 "+"横向云雨图", '.tiff');
print(gcf, '-dtiff', '-r600', str);
%% 调用函数
function rainCloudsH(dataCell, dataName)
% 颜色列表
colorList=[0.9294 0.7569 0.5059
0.9176 0.5569 0.4627
0.7020 0.4784 0.5451
0.4863 0.4314 0.5490];
% =========================================================================
classNum=length(dataCell);
if size(colorList,1)==0
colorList=repmat([130,170,172]./255,[classNum,1]);
else
colorList=repmat(colorList,[ceil(classNum/size(colorList,1)),1]);
end
if isempty(dataName)
for i=1:classNum
dataName{i}=['class',num2str(i)];
end
end
figure(1)
% 坐标区域修饰
hold on; box on;
ax=gca;
ax.YLim=[1/2,classNum+2/3];
ax.YTick=1:classNum;
ax.LineWidth=1.2;
ax.YTickLabels=dataName(end:-1:1);
ax.FontSize=14;
rate=3.5;
% 绘制雨云图
for i=1:classNum
tX=dataCell{i};tX=tX(:);
[F,Xi]=ksdensity(tX);
% 绘制山脊图
patchCell(i)=fill([Xi(1),Xi,Xi(end)],0.2+[0,F,0].*rate+(classNum+1-i).*ones(1,length(F)+2),...
colorList(i,:),'EdgeColor',[0,0,0],'FaceAlpha',0.8,'LineWidth',1.2);
% 其他数据获取
qt25=quantile(tX,0.25); % 下四分位数
qt75=quantile(tX,0.75); % 上四分位数
med=median(tX); % 中位数
outliBool=isoutlier(tX,'quartiles'); % 离群值点
nX=tX(~outliBool); % 95%置信内的数
% 绘制箱线图
plot([min(nX),max(nX)],[(classNum+1-i),(classNum+1-i)],'k','lineWidth',1.2);
fill([qt25,qt25,qt75,qt75],(classNum+1-i)+[-1 1 1 -1].*0.12,colorList(i,:),'EdgeColor',[0 0 0]);
plot([med,med],[(classNum+1-i)-0.12,(classNum+1-i)+0.12],'Color',[0,0,0],'LineWidth',2.5)
% 绘制散点
tY=(rand(length(tX),1)-0.5).*0.24+ones(length(tX),1).*(classNum+1-i);
scatter(tX,tY,15,'CData',colorList(i,:),'MarkerEdgeAlpha',0.15,...
'MarkerFaceColor',colorList(i,:),'MarkerFaceAlpha',0.1)
end
set(gca,'FontName','Times New Roman','FontSize',14, 'Layer','top','LineWidth',1);
% 绘制图例
hl = legend(patchCell,dataName);
set(hl,'Box','off','location','northOutside','NumColumns',4,'FontSize',16,'FontName','Times New Roman');
end
MATLAB代码如下:
clc
close all
clear
%% 函数说明-绘制云雨图
pathFigure= '.\Figures\' ;
figureUnits = 'centimeters';
figureWidth = 20;
figureHeight = 10;
% 导入数据
X1=[normrnd(8,4,1,120),normrnd(5,2,1,25)];
X2=[normrnd(2.5,3,1,75),normrnd(6,4,1,25),normrnd(15,1,1,100)];
X3=[normrnd(4,3,1,40),normrnd(3,4,1,25)];
X4=[normrnd(4,3,1,40),normrnd(2,4,1,75)];
dataCell={X1,X2,X3,X4}; % 把数据放到元胞数组,要是数据太多可写循环放入
dataName={'A','B','C','D'}; % 各个数据类的名称,可空着
%% 绘制竖向云雨图
rainCloudsV(dataCell, dataName)
str= strcat(pathFigure, "图2 "+"竖向云雨图", '.tiff');
print(gcf, '-dtiff', '-r600', str);
function rainCloudsV(dataCell, dataName)
% 颜色列表
colorList=[0.9294 0.7569 0.5059
0.9176 0.5569 0.4627
0.7020 0.4784 0.5451
0.4863 0.4314 0.5490];
% =========================================================================
classNum=length(dataCell);
if size(colorList,1)==0
colorList=repmat([130,170,172]./255,[classNum,1]);
else
colorList=repmat(colorList,[ceil(classNum/size(colorList,1)),1]);
end
if isempty(dataName)
for i=1:classNum
dataName{i}=['class',num2str(i)];
end
end
figure(2)
% 坐标区域修饰
hold on; box on;
ax=gca;
ax.XLim=[1/2,classNum+2/3];
ax.XTick=1:classNum;
ax.LineWidth=1.2;
ax.XTickLabels=dataName(end:-1:1);
ax.FontSize=14;
rate=3.5;
% 绘制雨云图
for i=1:classNum
tX=dataCell{i};tX=tX(:);
[F,Xi]=ksdensity(tX);
% 绘制山脊图
patchCell(i)=fill(0.2+[0,F,0].*rate+(classNum+1-i).*ones(1,length(F)+2),[Xi(1),Xi,Xi(end)],...
colorList(i,:),'EdgeColor',[0,0,0],'FaceAlpha',0.8,'LineWidth',1.2);
% 其他数据获取
qt25=quantile(tX,0.25); % 下四分位数
qt75=quantile(tX,0.75); % 上四分位数
med=median(tX); % 中位数
outliBool=isoutlier(tX,'quartiles'); % 离群值点
nX=tX(~outliBool); % 95%置信内的数
% 绘制箱线图
plot([(classNum+1-i),(classNum+1-i)],[min(nX),max(nX)],'k','lineWidth',1.2);
fill((classNum+1-i)+[-1 1 1 -1].*0.12,[qt25,qt25,qt75,qt75],colorList(i,:),'EdgeColor',[0 0 0]);
plot([(classNum+1-i)-0.12,(classNum+1-i)+0.12],[med,med],'Color',[0,0,0],'LineWidth',2.5)
% 绘制散点
tY=(rand(length(tX),1)-0.5).*0.24+ones(length(tX),1).*(classNum+1-i);
scatter(tY,tX,15,'CData',colorList(i,:),'MarkerEdgeAlpha',0.15,...
'MarkerFaceColor',colorList(i,:),'MarkerFaceAlpha',0.1)
end
set(gca,'FontName','Times New Roman','FontSize',14, 'Layer','top','LineWidth',1);
% 绘制图例
hl = legend(patchCell,dataName);
set(hl,'Box','off','location','northOutside','NumColumns',4,'FontSize',16,'FontName','Times New Roman');
end
1、微信公众号-MATLAB | 如何使用MATLAB绘制雨云图