首頁 > 軟體

Matlab實現繪製高階版本韋恩圖(upset圖)

2023-01-28 18:01:25

韋恩圖隨著階數升高會越來越複雜,當階數達到7或者以上時幾乎沒辦法繪製:

但是使用upset圖卻可以比較輕易的繪製:

兩種型別圖的對應關係:

這期便教大家如何繪製這樣的upset圖:

教學部分

資料準備

資料需要的是0,1矩陣,例如隨機生成資料:

setName={'RB1','PIK3R1','EGFR','TP53','PTEN'};
Data=rand([200,5])>.85;

每一行代表一個物件,第一行是1,0,0,1,0說明它既屬於第一類也屬於第四類。

配色

配色可以是rgb數值,也可以是多行顏色,也可以用matlab自帶的colormap資料:

% 設定兩個柱狀圖及關係連線的顏色
bar1Color=[61,58,61]./255;
bar2Color=[61,58,61]./255;
lineColor=[61,58,61]./255;

在隨後的部分會更詳細的講解顏色設定。

主要運算

接下來幾行就是資料統計的詳細程式碼,小技巧有點多,這部分技巧在韋恩圖繪製那篇和資料統計那篇也寫到過,可以去瞅瞅,這裡不詳細展開:

% 進行組合統計(一頓花裡胡哨的操作)
pBool=abs(dec2bin((1:(2^size(Data,2)-1))'))-48;
[pPos,~]=find(((pBool*(1-Data'))|((1-pBool)*Data'))==0);
sPPos=sort(pPos);dPPos=find([diff(sPPos);1]);
pType=sPPos(dPPos);pCount=diff([0;dPPos]);
[pCount,pInd]=sort(pCount,'descend');
pType=pType(pInd);
sCount=sum(Data,1);
[sCount,sInd]=sort(sCount,'descend');
sType=1:size(Data,2);
sType=sType(sInd);

建立佈局並修飾

若集合名稱為中文亂碼,請將字型改為宋體,若後面繪圖部分顯示不全請調整各個axes的XLim及YLim屬性。

% 構造figure及axes
fig=figure('Units','normalized','Position',[.3,.2,.5,.63],'Color',[1,1,1]);
axI=axes('Parent',fig);hold on;
set(axI,'Position',[.33,.35,.655,.61],'LineWidth',1.2,'Box','off','TickDir','out',...
    'FontName','Times New Roman','FontSize',12,'XTick',[],'XLim',[0,length(pType)+1])
axI.YLabel.String='Intersection Size';
axI.YLabel.FontSize=16;
%
axS=axes('Parent',fig);hold on;
set(axS,'Position',[.01,.08,.245,.26],'LineWidth',1.2,'Box','off','TickDir','out',...
    'FontName','Times New Roman','FontSize',12,'YColor','none','YLim',[.5,size(Data,2)+.5],...
    'YAxisLocation','right','XDir','reverse','YTick',[])
axS.XLabel.String='Set Size';
axS.XLabel.FontSize=16;
%
axL=axes('Parent',fig);hold on;
set(axL,'Position',[.33,.08,.655,.26],'YColor','none','YLim',[.5,size(Data,2)+.5],'XColor','none','XLim',axI.XLim)

影象繪製

% 相交關係統計圖 -----------------------------------------------------------
barHdlI=bar(axI,pCount);
barHdlI.EdgeColor='none';
if size(bar1Color,1)==1
    bar1Color=[bar1Color;bar1Color];
end
tx=linspace(0,1,size(bar1Color,1))';
ty1=bar1Color(:,1);ty2=bar1Color(:,2);ty3=bar1Color(:,3);
tX=linspace(0,1,length(pType))';
bar1Color=[interp1(tx,ty1,tX,'pchip'),interp1(tx,ty2,tX,'pchip'),interp1(tx,ty3,tX,'pchip')];
barHdlI.FaceColor='flat';
for i=1:length(pType)
    barHdlI.CData(i,:)=bar1Color(i,:);
end
text(axI,1:length(pType),pCount,string(pCount),'HorizontalAlignment','center',...
    'VerticalAlignment','bottom','FontName','Times New Roman','FontSize',12,'Color',[61,58,61]./255)
% 集合統計圖 ---------------------------------------------------------------
barHdlS=barh(axS,sCount,'BarWidth',.6);
barHdlS.EdgeColor='none';
barHdlS.BaseLine.Color='none';
for i=1:size(Data,2)
    annotation('textbox',[(axS.Position(1)+axS.Position(3)+axI.Position(1))/2-.02,...
        axS.Position(2)+axS.Position(4)./size(Data,2).*(i-.5)-.02,.04,.04],...
        'String',setName{sInd(i)},'HorizontalAlignment','center','VerticalAlignment','middle',...
        'FitBoxToText','on','LineStyle','none','FontName','Times New Roman','FontSize',13)
end
if size(bar2Color,1)==1
    bar2Color=[bar2Color;bar2Color];
end
tx=linspace(0,1,size(bar2Color,1))';
ty1=bar2Color(:,1);ty2=bar2Color(:,2);ty3=bar2Color(:,3);
tX=linspace(0,1,size(Data,2))';
bar2Color=[interp1(tx,ty1,tX,'pchip'),interp1(tx,ty2,tX,'pchip'),interp1(tx,ty3,tX,'pchip')];
barHdlS.FaceColor='flat';
sstr{size(Data,2)}='';
for i=1:size(Data,2)
    barHdlS.CData(i,:)=bar2Color(i,:);
    sstr{i}=[num2str(sCount(i)),' '];
end
text(axS,sCount,1:size(Data,2),sstr,'HorizontalAlignment','right',...
    'VerticalAlignment','middle','FontName','Times New Roman','FontSize',12,'Color',[61,58,61]./255)
% 繪製關係連線 ---------------------------------------------------------------
patchColor=[248,246,249;255,254,255]./255;
for i=1:size(Data,2)
    fill(axL,axI.XLim([1,2,2,1]),[-.5,-.5,.5,.5]+i,patchColor(mod(i+1,2)+1,:),'EdgeColor','none')
end
[tX,tY]=meshgrid(1:length(pType),1:size(Data,2));
plot(axL,tX(:),tY(:),'o','Color',[233,233,233]./255,...
    'MarkerFaceColor',[233,233,233]./255,'MarkerSize',10);
for i=1:length(pType)
    tY=find(pBool(pType(i),:));
    oY=zeros(size(tY));
    for j=1:length(tY)
        oY(j)=find(sType==tY(j));
    end
    tX=i.*ones(size(tY));
    plot(axL,tX(:),oY(:),'-o','Color',lineColor(1,:),'MarkerEdgeColor','none',...
        'MarkerFaceColor',lineColor(1,:),'MarkerSize',10,'LineWidth',2);
end

若是前面配色部分使用的是:

bar1Color=[61,58,61]./255;
bar2Color=[61,58,61]./255;
lineColor=[61,58,61]./255;

修改其中一個柱狀圖顏色,另一個用多行顏色矩陣:

bar1Color=[66,182,195]./255;
bar2Color=[253,255,228;
          164,218,183;
          68,181,197;
          44,126,185;
          35,51,154]./255;
lineColor=[61,58,61]./255;

一個用多行顏色矩陣,一個使用自帶colormap資料:

bar1Color=[0,0,245;245,0,0]./255;
bar2Color=cool;
lineColor=[61,58,61]./255;

完整程式碼

% upSetMDemo
% @author : slandarer
% Zhaoxu Liu / slandarer (2023). upset plot 
% (https://www.mathworks.com/matlabcentral/fileexchange/123695-upset-plot), 
% MATLAB Central File Exchange. 檢索來源 2023/1/22.

rng(2)
clc;clear;
setName={'RB1','PIK3R1','EGFR','TP53','PTEN'};
Data=rand([200,5])>.85;
% setName={'A','B','C','D','E','F','G'};
% Data=rand([200,7])>.9;

% 設定兩個柱狀圖及關係連線的顏色
bar1Color=[61,58,61]./255;
bar2Color=[61,58,61]./255;
lineColor=[61,58,61]./255;

% bar1Color=[66,182,195]./255;
% bar2Color=[253,255,228;
%           164,218,183;
%           68,181,197;
%           44,126,185;
%           35,51,154]./255;
% lineColor=[61,58,61]./255;

bar1Color=[0,0,245;245,0,0]./255;
bar2Color=cool;
lineColor=[61,58,61]./255;

%% =========================================================================
% 進行組合統計(一頓花裡胡哨的操作)
pBool=abs(dec2bin((1:(2^size(Data,2)-1))'))-48;
[pPos,~]=find(((pBool*(1-Data'))|((1-pBool)*Data'))==0);
sPPos=sort(pPos);dPPos=find([diff(sPPos);1]);
pType=sPPos(dPPos);pCount=diff([0;dPPos]);
[pCount,pInd]=sort(pCount,'descend');
pType=pType(pInd);
sCount=sum(Data,1);
[sCount,sInd]=sort(sCount,'descend');
sType=1:size(Data,2);
sType=sType(sInd);
%% ========================================================================
% 構造figure及axes
fig=figure('Units','normalized','Position',[.3,.2,.5,.63],'Color',[1,1,1]);
axI=axes('Parent',fig);hold on;
set(axI,'Position',[.33,.35,.655,.61],'LineWidth',1.2,'Box','off','TickDir','out',...
    'FontName','Times New Roman','FontSize',12,'XTick',[],'XLim',[0,length(pType)+1])
axI.YLabel.String='Intersection Size';
axI.YLabel.FontSize=16;
%
axS=axes('Parent',fig);hold on;
set(axS,'Position',[.01,.08,.245,.26],'LineWidth',1.2,'Box','off','TickDir','out',...
    'FontName','Times New Roman','FontSize',12,'YColor','none','YLim',[.5,size(Data,2)+.5],...
    'YAxisLocation','right','XDir','reverse','YTick',[])
axS.XLabel.String='Set Size';
axS.XLabel.FontSize=16;
%
axL=axes('Parent',fig);hold on;
set(axL,'Position',[.33,.08,.655,.26],'YColor','none','YLim',[.5,size(Data,2)+.5],'XColor','none','XLim',axI.XLim)
%% ========================================================================
% 相交關係統計圖 -----------------------------------------------------------
barHdlI=bar(axI,pCount);
barHdlI.EdgeColor='none';
if size(bar1Color,1)==1
    bar1Color=[bar1Color;bar1Color];
end
tx=linspace(0,1,size(bar1Color,1))';
ty1=bar1Color(:,1);ty2=bar1Color(:,2);ty3=bar1Color(:,3);
tX=linspace(0,1,length(pType))';
bar1Color=[interp1(tx,ty1,tX,'pchip'),interp1(tx,ty2,tX,'pchip'),interp1(tx,ty3,tX,'pchip')];
barHdlI.FaceColor='flat';
for i=1:length(pType)
    barHdlI.CData(i,:)=bar1Color(i,:);
end
text(axI,1:length(pType),pCount,string(pCount),'HorizontalAlignment','center',...
    'VerticalAlignment','bottom','FontName','Times New Roman','FontSize',12,'Color',[61,58,61]./255)
% 集合統計圖 ---------------------------------------------------------------
barHdlS=barh(axS,sCount,'BarWidth',.6);
barHdlS.EdgeColor='none';
barHdlS.BaseLine.Color='none';
for i=1:size(Data,2)
    annotation('textbox',[(axS.Position(1)+axS.Position(3)+axI.Position(1))/2-.02,...
        axS.Position(2)+axS.Position(4)./size(Data,2).*(i-.5)-.02,.04,.04],...
        'String',setName{sInd(i)},'HorizontalAlignment','center','VerticalAlignment','middle',...
        'FitBoxToText','on','LineStyle','none','FontName','Times New Roman','FontSize',13)
end
if size(bar2Color,1)==1
    bar2Color=[bar2Color;bar2Color];
end
tx=linspace(0,1,size(bar2Color,1))';
ty1=bar2Color(:,1);ty2=bar2Color(:,2);ty3=bar2Color(:,3);
tX=linspace(0,1,size(Data,2))';
bar2Color=[interp1(tx,ty1,tX,'pchip'),interp1(tx,ty2,tX,'pchip'),interp1(tx,ty3,tX,'pchip')];
barHdlS.FaceColor='flat';
sstr{size(Data,2)}='';
for i=1:size(Data,2)
    barHdlS.CData(i,:)=bar2Color(i,:);
    sstr{i}=[num2str(sCount(i)),' '];
end
text(axS,sCount,1:size(Data,2),sstr,'HorizontalAlignment','right',...
    'VerticalAlignment','middle','FontName','Times New Roman','FontSize',12,'Color',[61,58,61]./255)
% 繪製關係連線 ---------------------------------------------------------------
patchColor=[248,246,249;255,254,255]./255;
for i=1:size(Data,2)
    fill(axL,axI.XLim([1,2,2,1]),[-.5,-.5,.5,.5]+i,patchColor(mod(i+1,2)+1,:),'EdgeColor','none')
end
[tX,tY]=meshgrid(1:length(pType),1:size(Data,2));
plot(axL,tX(:),tY(:),'o','Color',[233,233,233]./255,...
    'MarkerFaceColor',[233,233,233]./255,'MarkerSize',10);
for i=1:length(pType)
    tY=find(pBool(pType(i),:));
    oY=zeros(size(tY));
    for j=1:length(tY)
        oY(j)=find(sType==tY(j));
    end
    tX=i.*ones(size(tY));
    plot(axL,tX(:),oY(:),'-o','Color',lineColor(1,:),'MarkerEdgeColor','none',...
        'MarkerFaceColor',lineColor(1,:),'MarkerSize',10,'LineWidth',2);
end
% Zhaoxu Liu / slandarer (2023). upset plot 
% (https://www.mathworks.com/matlabcentral/fileexchange/123695-upset-plot), 
% MATLAB Central File Exchange. 檢索來源 2023/1/22.

對於本例子的解讀:

以上就是Matlab實現繪製高階版本韋恩圖(upset圖)的詳細內容,更多關於Matlab繪製韋恩圖的資料請關注it145.com其它相關文章!


IT145.com E-mail:sddin#qq.com