• MATLAB | 绘图复刻(四) | 和弦图+颜色修改+标签旋转


    说明

    看到公众号iMeta使用R语言复刻了2022年10月13日刊登在iMeta上的Gut microbiota composition in the sympatric and diet-sharing Drosophila simulans and Dicranocephalus wallichii bowringi shaped largely by community assembly processes rather than regional species pool- iMeta | 扬州大学杜予州团队揭示同域内同食物的两种昆虫肠道微生物群落装配机制中的配图Figure 1E:

    我基于我自己写的MATLAB工具函数对其进行复刻(使用1.6.0版本)

    Zhaoxu Liu (2022). chord chart 弦图 (https://www.mathworks.com/matlabcentral/fileexchange/116550-chord-chart), MATLAB Central File Exchange. 检索来源 2022/11/10.近期我又对其进行了微调,请自行下载查看其中demo3该工具函数详细使用方法请见:

    https://slandarer.blog.csdn.net/article/details/126458181

    我的绘图效果:


    完整步骤

    0 数据准备

    这里没有获取其原始数据,直接随机生成了一些数据值:

    % 随机生成数据
    dataMat=randi([1,7],[11,5]);
    % 标签名称
    colName={'Fly','Beetle','Leaf','Soil','Waxberry'};
    rowName={'Bartomella','Bradyrhizobium','Dysgomonas','Enterococcus',...
             'Lactococcus','norank','others','Pseudomonas','uncultured',...
             'Vibrionimonas','Wolbachia'};
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    1 基础绘图

    准备好上述数据后,使用slandarer开发的工具函数两行代码绘制完成:

    CC=chordChart(dataMat,'rowName',rowName,'colName',colName);
    CC=CC.draw();
    
    • 1
    • 2

    2 调整间隙

    使用1.6.0版本进行绘图可使用Sep属性减小间隙,默认值为1/40这里设置为1/80:

    CC=chordChart(dataMat,'rowName',rowName,'colName',colName,'Sep',1/80);
    CC=CC.draw();
    
    • 1
    • 2

    3 显示刻度

    % 开启刻度
    CC.tickState('on')
    
    • 1
    • 2

    4 调整方块颜色

    颜色RGB值获取可以通过很多方法,例如PPT或者QQ自带的颜色提取器或者这篇所提到的图片颜色提取器《MATLAB | 自制色卡、更改绘图配色、图像修饰》

    就使用

    • setSquareT_N
    • setSquareF_N

    循环调整颜色即可,例如调整上方颜色:

    % 修改上方方块颜色
    CListT=[0.7765 0.8118 0.5216;0.4431 0.4706 0.3843;0.5804 0.2275 0.4549;
            0.4471 0.4039 0.6745;0.0157 0      0     ];
    for i=1:5
        CC.setSquareT_N(i,'FaceColor',CListT(i,:))
    end
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    调整下方颜色:

    % 修改下方方块颜色
    CListF=[0.5843 0.6863 0.7843;0.1098 0.1647 0.3255;0.0902 0.1608 0.5373;
            0.6314 0.7961 0.2118;0.0392 0.2078 0.1059;0.0157 0      0     ;
            0.8549 0.9294 0.8745;0.3882 0.3255 0.4078;0.5020 0.7216 0.3843;
            0.0902 0.1843 0.1804;0.8196 0.2314 0.0706];
    for i=1:11
        CC.setSquareF_N(i,'FaceColor',CListF(i,:))
    end
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    5 调整弦颜色

    使用

    • CC.setChordMN

    循环调整颜色:

    % 修改弦颜色
    for i=1:5
        for j=1:11
            CC.setChordMN(j,i,'FaceColor',CListT(i,:),'FaceAlpha',.5)
        end
    end
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    6 旋转标签并调整字体

    我们发现有的标签挤在一起,因此需要旋转90度,懒得将该功能集成进工具函数了,请将以下代码加入文末实现标签旋转:

    % 以下代码用来旋转标签
    % The following code is used to rotate the label
    textHdl=findobj(gca,'Type','Text');
    for i=1:length(textHdl)
        if textHdl(i).Rotation<-90
            textHdl(i).Rotation=textHdl(i).Rotation+180;
        end
        switch true
            case textHdl(i).Rotation<0&&textHdl(i).Position(2)>0
                textHdl(i).Rotation=textHdl(i).Rotation+90;
                textHdl(i).HorizontalAlignment='left';
            case textHdl(i).Rotation>0&&textHdl(i).Position(2)>0
                textHdl(i).Rotation=textHdl(i).Rotation-90;
                textHdl(i).HorizontalAlignment='right';
            case textHdl(i).Rotation<0&&textHdl(i).Position(2)<0
                textHdl(i).Rotation=textHdl(i).Rotation+90;
                textHdl(i).HorizontalAlignment='right';
            case textHdl(i).Rotation>0&&textHdl(i).Position(2)<0
                textHdl(i).Rotation=textHdl(i).Rotation-90;
                textHdl(i).HorizontalAlignment='left';
        end
    end
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22

    使用setFont调整字号和字体:

    CC.setFont('FontSize',17,'FontName','Cambria')
    
    • 1

    -1 完整代码

    % chord demo
    rng(2)
    
    dataMat=randi([1,7],[11,5]);
    colName={'Fly','Beetle','Leaf','Soil','Waxberry'};
    rowName={'Bartomella','Bradyrhizobium','Dysgomonas','Enterococcus',...
             'Lactococcus','norank','others','Pseudomonas','uncultured',...
             'Vibrionimonas','Wolbachia'};
    
    CC=chordChart(dataMat,'rowName',rowName,'colName',colName,'Sep',1/80);
    CC=CC.draw();
    
    % 开启刻度
    CC.tickState('on')
    
    % 修改上方方块颜色
    CListT=[0.7765 0.8118 0.5216;0.4431 0.4706 0.3843;0.5804 0.2275 0.4549;
            0.4471 0.4039 0.6745;0.0157 0      0     ];
    for i=1:5
        CC.setSquareT_N(i,'FaceColor',CListT(i,:))
    end
    
    % 修改下方方块颜色
    CListF=[0.5843 0.6863 0.7843;0.1098 0.1647 0.3255;0.0902 0.1608 0.5373;
            0.6314 0.7961 0.2118;0.0392 0.2078 0.1059;0.0157 0      0     ;
            0.8549 0.9294 0.8745;0.3882 0.3255 0.4078;0.5020 0.7216 0.3843;
            0.0902 0.1843 0.1804;0.8196 0.2314 0.0706];
    for i=1:11
        CC.setSquareF_N(i,'FaceColor',CListF(i,:))
    end
    
    % 修改弦颜色
    for i=1:5
        for j=1:11
            CC.setChordMN(j,i,'FaceColor',CListT(i,:),'FaceAlpha',.5)
        end
    end
    
    
    % 以下代码用来旋转标签
    % The following code is used to rotate the label
    textHdl=findobj(gca,'Type','Text');
    for i=1:length(textHdl)
        if textHdl(i).Rotation<-90
            textHdl(i).Rotation=textHdl(i).Rotation+180;
        end
        switch true
            case textHdl(i).Rotation<0&&textHdl(i).Position(2)>0
                textHdl(i).Rotation=textHdl(i).Rotation+90;
                textHdl(i).HorizontalAlignment='left';
            case textHdl(i).Rotation>0&&textHdl(i).Position(2)>0
                textHdl(i).Rotation=textHdl(i).Rotation-90;
                textHdl(i).HorizontalAlignment='right';
            case textHdl(i).Rotation<0&&textHdl(i).Position(2)<0
                textHdl(i).Rotation=textHdl(i).Rotation+90;
                textHdl(i).HorizontalAlignment='right';
            case textHdl(i).Rotation>0&&textHdl(i).Position(2)<0
                textHdl(i).Rotation=textHdl(i).Rotation-90;
                textHdl(i).HorizontalAlignment='left';
        end
    end
    
    CC.setFont('FontSize',17,'FontName','Cambria')
    
    • 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

    未经允许本代码请勿作商业用途,引用的话可以引用我file exchange上的链接,可使用如下格式:

    Zhaoxu Liu (2022). chord chart 弦图 (https://www.mathworks.com/matlabcentral/fileexchange/116550-chord-chart), MATLAB Central File Exchange. 检索来源 2022/11/10.

    若转载请保留以上file exchange链接及本文链接!!!

    本文中全部代码:

    链接:https://pan.baidu.com/s/1id3g6iosFWSyVlk6K0XPPA?pwd=slan
    提取码:slan

  • 相关阅读:
    算法——单调队列
    【Android 屏幕适配】屏幕适配通用解决方案 ④ ( 自定义组件解决方案 | 计算设计稿与实际布局的比例系数 )
    C# linq初探 使用linq查询数组中元素
    手把手教你安装python环境 Mac Windows
    2021年中青杯B题港珠澳大桥桥梁设计与安全策略数学建模全过程文档及程序
    人大金仓数据库,可以外连SQL Server 、MySql 数据库吗?
    uniapp写一个计算器用于记账(微信小程序,APP)
    解决Pytest UnknownMarkWarning: Unknown pytest.mark.single - is this a typo?
    Cookie和Session的各自作用、使用场景、java操作代码(创建、获取等操作)
    2.SSM之Spring整合、AOP及Spring事务
  • 原文地址:https://blog.csdn.net/slandarer/article/details/127789016