广告位联系
返回顶部
分享到

Matlab实现绘制高阶版本韦恩图(upset图)

C语言 来源:互联网 作者:佚名 发布时间:2023-01-30 09:55:31 人浏览
摘要

韦恩图随着阶数升高会越来越复杂,当阶数达到7或者以上时几乎没办法绘制: 但是使用upset图却可以比较轻易的绘制: 两种类型图的对应关系: 这期便教大家如何绘制这样的upset图:

韦恩图随着阶数升高会越来越复杂,当阶数达到7或者以上时几乎没办法绘制:

但是使用upset图却可以比较轻易的绘制:

两种类型图的对应关系:

这期便教大家如何绘制这样的upset图:

教程部分

数据准备

数据需要的是0,1矩阵,例如随机生成数据:

1

2

setName={'RB1','PIK3R1','EGFR','TP53','PTEN'};

Data=rand([200,5])>.85;

每一行代表一个对象,第一行是1,0,0,1,0说明它既属于第一类也属于第四类。

配色

配色可以是rgb数值,也可以是多行颜色,也可以用matlab自带的colormap数据:

1

2

3

4

% 设置两个柱状图及关系连线的颜色

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

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

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

在随后的部分会更详细的讲解颜色设置。

主要运算

接下来几行就是数据统计的详细代码,小技巧有点多,这部分技巧在韦恩图绘制那篇和数据统计那篇也写到过,可以去瞅瞅,这里不详细展开:

1

2

3

4

5

6

7

8

9

10

11

% 进行组合统计(一顿花里胡哨的操作)

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属性。

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

% 构造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)

图像绘制

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

% 相交关系统计图 -----------------------------------------------------------

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

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

1

2

3

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

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

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

修改其中一个柱状图颜色,另一个用多行颜色矩阵:

1

2

3

4

5

6

7

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数据:

1

2

3

bar1Color=[0,0,245;245,0,0]./255;

bar2Color=cool;

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

完整代码

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

% 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.

对于本例子的解读:


版权声明 : 本文内容来源于互联网或用户自行发布贡献,该文观点仅代表原作者本人。本站仅提供信息存储空间服务和不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权, 违法违规的内容, 请发送邮件至2530232025#qq.cn(#换@)举报,一经查实,本站将立刻删除。
原文链接 : https://blog.csdn.net/slandarer/article/details/128750453
相关文章
  • C语言通过二分查找实现猜数字游戏

    C语言通过二分查找实现猜数字游戏
    二分查找 题目: 在一个有序数组中查找具体的某个数字n。 首先我们先定义一个110的数组 ,如果7为我们要查找的数字,编写代码如下 1 2
  • Matlab实现绘制高阶版本韦恩图(upset图)

    Matlab实现绘制高阶版本韦恩图(upset图)
    韦恩图随着阶数升高会越来越复杂,当阶数达到7或者以上时几乎没办法绘制: 但是使用upset图却可以比较轻易的绘制: 两种类型图的对应关
  • 通俗易懂的C语言快速排序和归并排序的时间复杂
    今天面试的时候,被问到归并排序的时间复杂度,这个大家都知道是O(nlogn),但是面试官又继续问,怎么推导出来的。这我就有点懵了,
  • C语言学习基础知识分享

    C语言学习基础知识分享
    写在前面 我们正式开始接触到C语言,这是我在学习过C语言后重新写的博客,我把之前的稍微优化了一下,希望能用更加朴素的语言和大家分享
  • C语言基础知识分享续篇

    C语言基础知识分享续篇
    写在前面 好了,现在我们开始C语言的第二个部分.今天我们需要看下面几个知识点,都是非常简单的,我们主要认识一下. 数组 我们知道一个一
  • C语言实现求解素数的N种方法

    C语言实现求解素数的N种方法
    哈喽各位友友们,我今天又学到了很多有趣的知识,现在迫不及待的想和大家分享一下!我仅已此文,手把手带领大家探讨利用试除法、筛
  • C语言利用goto语句设计实现一个关机程序

    C语言利用goto语句设计实现一个关机程序
    goto语句其实在平常中我们 除了学习分支语句和循环语句时,介绍循环语句时,才会知道有goto语句这个用法,那读者可能会问:我们还有学
  • VS及Unity安装和使用Nuget包

    VS及Unity安装和使用Nuget包
    一、百科 Nuget是一个包(package)管理平台,确切的说是.net平台的包管理工具,它提供了一系列客户端用于生成,上传和使用包(package),
  • C/C++ Qt实现文章小说人物关系分析

    C/C++ Qt实现文章小说人物关系分析
    一、所需工具软件 1. Visual Stuido 2. C++ 二、使用步骤 1.引入库 代码如下(示例): 1 2 3 4 5 6 7 8 9 10 11 #include QtGuiApplication1.h #includeqDebug #incl
  • C语言实现三子棋的代码

    C语言实现三子棋的代码
    一、问题描述 用 c 语言实现三子棋。 二、基本流程 在写三子棋的代码之前,我们来看看实现这个游戏的逻辑: 1.菜单界面选择开始或者退
  • 本站所有内容来源于互联网或用户自行发布,本站仅提供信息存储空间服务,不拥有版权,不承担法律责任。如有侵犯您的权益,请您联系站长处理!
  • Copyright © 2017-2022 F11.CN All Rights Reserved. F11站长开发者网 版权所有 | 苏ICP备2022031554号-1 | 51LA统计