跟着Nature Communications学作图:纹理柱状图+添加显著性标签!

📋文章目录

  • 复现图片
  • 设置工作路径和加载相关R包
  • 读取数据集
  • 数据可视化
    • 计算均值和标准差
    • 方差分析
    • 组间t-test
  • 图a可视化过程
  • 图b可视化过程
  • 合并图ab

   跟着「Nature Communications」学作图,今天主要通过复刻NC文章中的一张主图来巩固先前分享过的知识点,比如纹理柱状图、 添加显著性标签、拼图等,其中还会涉及数据处理的相关细节和具体过程。

复现图片

在这里插入图片描述

在这里插入图片描述
主要复现红框部分,右侧的cd图与框中的图是同类型的,只不过需要构建更多数据相对麻烦,所以选择以左侧红框图进行学习和展示。

设置工作路径和加载相关R包

rm(list = ls()) # 清空当前环境变量
setwd("C:/Users/Zz/Desktop/公众号 SES") # 设置工作路径
# 加载R包
library(ggplot2)
library(agricolae)
library(ggpattern)
library(ggpubr)

读取数据集

cData1 <- read.csv("cData1.csv", header = T, row.names = 1)
head(cData1)
#   Type   Deep ctValue ftValue Stripe_Angle
# 1   BT    Top      55      73          135
# 2   BT    Top      61      78          135
# 3   BT    Top      69      80          135
# 4   BT Center      35      50          135
# 5   BT Center      42      41          135
# 6   BT Center      43      57          135

数据包括以下指标:2个分类变量、2个数值变量、和1个整数变量。

数据可视化

在可视化前,我们需要先思考图中构成的元素,由哪些组成。

  • 计算每个分组或处理下的均值和标准差;
  • 进行组内的方差分析及多重比较;
  • 进行组间的t检验;

计算均值和标准差

cData1_mean <- cData1 %>% gather(key = "var_type", value = "value",3:4) %>% group_by(Type, Deep, var_type, Stripe_Angle) %>%  summarise(mean = mean(value),sd = sd(value))
cData1_mean  
# A tibble: 12 × 6
# Groups:   Type, Deep, var_type [12]
# Type  Deep   var_type Stripe_Angle  mean    sd
# <fct> <chr>  <chr>           <int> <dbl> <dbl>
# 1 BT    Bottom ctValue           135  47.7  1.53
# 2 BT    Bottom ftValue           135  48    1   
# 3 BT    Center ctValue           135  40    4.36
# 4 BT    Center ftValue           135  49.3  8.02
# 5 BT    Top    ctValue           135  61.7  7.02
# 6 BT    Top    ftValue           135  77    3.61
# 7 CK    Bottom ctValue           135  42    7.21
# 8 CK    Bottom ftValue           135  48    4.36
# 9 CK    Center ctValue           135  38.3  2.08
# 10 CK    Center ftValue           135  47.7  5.13
# 11 CK    Top    ctValue           135  46.7  7.57
# 12 CK    Top    ftValue           135  53.7 12.3 

方差分析

# 方差分析
groups <- NULL
vl <- unique((cData1 %>% gather(key = "var_type", value = "value", 3:4) %>% unite("unique_col", c(Type, var_type), sep = "-"))$unique_col)
vlfor(i in 1:length(vl)){df <- cData1 %>% gather(key = "var_type", value = "value", 3:4) %>% unite("unique_col", c(Type, var_type), sep = "-") %>% filter(unique_col == vl[i])aov <- aov(value ~ Deep, df)lsd <- LSD.test(aov, "Deep", p.adj = "bonferroni") %>%.$groups %>% mutate(Deep = rownames(.),unique_col = vl[i]) %>%dplyr::select(-value) %>% as.data.frame()groups <- rbind(groups, lsd)
}
groups <- groups %>% separate(unique_col, c("Type", "var_type"))
groups
#         groups   Deep Type var_type
# Top          a    Top   BT  ctValue
# Bottom       b Bottom   BT  ctValue
# Center       b Center   BT  ctValue
# Top1         a    Top   CK  ctValue
# Bottom1      a Bottom   CK  ctValue
# Center1      a Center   CK  ctValue
# Top2         a    Top   BT  ftValue
# Center2      b Center   BT  ftValue
# Bottom2      b Bottom   BT  ftValue
# Top3         a    Top   CK  ftValue
# Bottom3      a Bottom   CK  ftValue
# Center3      a Center   CK  ftValue

使用aov函数和LSD.test函数实现方差分析及对应的多重比较,并提取显著性字母标签。

然后将多重比较的结果与原均值标准差的数据进行合并:

cData1_mean1 <- left_join(cData1_mean, groups, by = c("Deep", "Type", "var_type")) %>% arrange(var_type) %>% group_by(Type, var_type) %>% mutate(label_to_show = n_distinct(groups))
cData1_mean1
# A tibble: 12 × 8
# Groups:   Type, var_type [4]
# Type  Deep   var_type Stripe_Angle  mean    sd groups label_to_show
# <chr> <chr>  <chr>           <int> <dbl> <dbl> <chr>          <int>
# 1 BT    Bottom ctValue           135  47.7  1.53 b                  2
# 2 BT    Center ctValue           135  40    4.36 b                  2
# 3 BT    Top    ctValue           135  61.7  7.02 a                  2
# 4 CK    Bottom ctValue           135  42    7.21 a                  1
# 5 CK    Center ctValue           135  38.3  2.08 a                  1
# 6 CK    Top    ctValue           135  46.7  7.57 a                  1
# 7 BT    Bottom ftValue           135  48    1    b                  2
# 8 BT    Center ftValue           135  49.3  8.02 b                  2
# 9 BT    Top    ftValue           135  77    3.61 a                  2
# 10 CK    Bottom ftValue           135  48    4.36 a                  1
# 11 CK    Center ftValue           135  47.7  5.13 a                  1
# 12 CK    Top    ftValue           135  53.7 12.3  a                  1
  • 需要注意的是:这里添加了label_to_show一列,目的是为了后续再进行字母标签添加时可以识别没有显著性的结果。

组间t-test

cData1_summary <- cData1 %>%gather(key = "var_type", value = "value", 3:4) %>% # unite("unique_col", c(Type, Deep), sep = "-") %>% unique_colgroup_by(Deep, var_type) %>%summarize(p_value = round(t.test(value ~ Type)$p.value, 2)) %>%mutate(label = ifelse(p_value <= 0.001, "***",ifelse(p_value <= 0.01, "**", ifelse(p_value <= 0.05, "*", ifelse(p_value <= 0.1, "●", NA)))))
cData1_summary
# Deep   var_type p_value label
# <chr>  <chr>      <dbl> <chr>
# 1 Bottom ctValue     0.31 NA   
# 2 Bottom ftValue     1    NA   
# 3 Center ctValue     0.59 NA   
# 4 Center ftValue     0.78 NA   
# 5 Top    ctValue     0.07 ●    
# 6 Top    ftValue     0.07 ● 

我们将计算出来的p值,并用* 或者 ●进行了赋值。然后合并相关结果:

cData1_summary1 <- left_join(cData1_mean1, cData1_summary, by = c("Deep", "var_type"))
cData1_summary1
# Type  Deep   var_type Stripe_Angle  mean    sd groups label_to_show p_value label
# <chr> <chr>  <chr>           <int> <dbl> <dbl> <chr>          <int>   <dbl> <chr>
# 1 BT    Bottom ctValue           135  47.7  1.53 b                  2    0.31 NA   
# 2 BT    Center ctValue           135  40    4.36 b                  2    0.59 NA   
# 3 BT    Top    ctValue           135  61.7  7.02 a                  2    0.07 ●    
# 4 CK    Bottom ctValue           135  42    7.21 a                  1    0.31 NA   
# 5 CK    Center ctValue           135  38.3  2.08 a                  1    0.59 NA   
# 6 CK    Top    ctValue           135  46.7  7.57 a                  1    0.07 ●    
# 7 BT    Bottom ftValue           135  48    1    b                  2    1    NA   
# 8 BT    Center ftValue           135  49.3  8.02 b                  2    0.78 NA   
# 9 BT    Top    ftValue           135  77    3.61 a                  2    0.07 ●    
# 10 CK    Bottom ftValue           135  48    4.36 a                  1    1    NA   
# 11 CK    Center ftValue           135  47.7  5.13 a                  1    0.78 NA   
# 12 CK    Top    ftValue           135  53.7 12.3  a                  1    0.07 ● 
  • 需要注意的是:添加的label也是为了后续筛选掉没有显著性结果做准备。

图a可视化过程

ctValue <- ggplot(data = cData1_mean1 %>% filter(var_type == "ctValue") %>% mutate(Deep = factor(Deep, levels = c("Top", "Center", "Bottom"))), aes(x = Type, y = mean, fill = Deep, pattern = Type, width = 0.75)) +geom_bar_pattern(position = position_dodge(preserve = "single"),stat = "identity",pattern_fill = "white", pattern_color = "white", pattern_angle = -50,pattern_spacing = 0.05,color = "grey",width = 0.75) +scale_pattern_manual(values = c(CK = "stripe", BT = "none")) +geom_errorbar(data = cData1_mean %>% filter(var_type == "ctValue") %>% mutate(Deep = factor(Deep, levels = c("Top", "Center", "Bottom"))), aes(x = Type, y = mean, ymin = mean - sd, ymax = mean + sd, width = 0.2),position = position_dodge(0.75),)+geom_point(data = cData1 %>% mutate(Deep = factor(Deep, levels = c("Top", "Center", "Bottom"))),aes(x = Type, y = ctValue, group = Deep), color = "black", fill = "#D2D2D2", shape = 21,position = position_dodge(0.75), size = 3)+geom_text(data = cData1_mean1 %>% filter(var_type == "ctValue",label_to_show > 1) %>% mutate(Deep = factor(Deep, levels = c("Top", "Center", "Bottom"))),aes(x = Type, y = mean + sd, label = groups), position = position_dodge(0.75), vjust = -0.5, size = 5) +geom_segment(data = cData1_summary1 %>% filter(p_value <= 0.1 & var_type == "ctValue"),aes(x = 0.75, xend = 0.75, y = 73, yend = 76))+geom_segment(data = cData1_summary1 %>% filter(p_value <= 0.1 & var_type == "ctValue"),aes(x = 0.75, xend = 1.75, y = 76, yend = 76))+geom_segment(data = cData1_summary1 %>% filter(p_value <= 0.1 & var_type == "ctValue"),aes(x = 1.75, xend = 1.75, y = 73, yend = 76))+geom_text(data = cData1_summary1 %>% filter(p_value <= 0.1 & var_type == "ctValue"),aes(x = 1.25, y = 76, label = paste0("p = ", p_value)),vjust = -0.5, size = 5)+geom_text(data = cData1_summary1 %>% filter(p_value <= 0.1 & var_type == "ctValue"),aes(x = 1.25, y = 78, label = label),vjust = -1, size = 5)+scale_fill_manual(values = c("#393939", "#A2A2A2", "#CCCCCC")) +scale_y_continuous(expand = c(0, 0), limits = c(0, 100), breaks = seq(0, 100, 50)) +theme_classic()+theme(legend.position = "top",axis.ticks.length.y = unit(0.2, "cm"),axis.text.y = element_text(color = "black", size = 12),axis.title.y = element_text(color = "black", size = 12, face = "bold"),axis.title.x = element_blank(),axis.text.x = element_blank(),axis.line.x = element_blank(),axis.ticks.x = element_blank(),plot.margin = margin(t = 0, r = 0, b = 1, l = 0, "lines"))+labs(y = "CTvalue", fill = "", pattern = "");ctValue

在这里插入图片描述

图b可视化过程

ftValue <- ggplot(data = cData1_mean1 %>% filter(var_type == "ftValue") %>% mutate(Deep = factor(Deep, levels = c("Top", "Center", "Bottom"))), aes(x = Type, y = mean, fill = Deep, pattern = Type, width = 0.75)
) +geom_bar_pattern(position = position_dodge(preserve = "single"),stat = "identity",pattern_fill = "white", pattern_color = "white", pattern_angle = -50,pattern_spacing = 0.05,color = "grey",width = 0.75) +scale_pattern_manual(values = c(CK = "stripe", BT = "none")) +geom_errorbar(data = cData1_mean %>% filter(var_type == "ftValue") %>% mutate(Deep = factor(Deep, levels = c("Top", "Center", "Bottom"))), aes(x = Type, y = mean, ymin = mean - sd, ymax = mean + sd, width = 0.2),position = position_dodge(0.75),)+geom_point(data = cData1 %>% mutate(Deep = factor(Deep, levels = c("Top", "Center", "Bottom"))),aes(x = Type, y = ftValue, group = Deep), color = "black", fill = "#D2D2D2", shape = 21,position = position_dodge(0.75), size = 3)+geom_text(data = cData1_mean1 %>% filter(var_type == "ftValue",label_to_show > 1) %>% mutate(Deep = factor(Deep, levels = c("Top", "Center", "Bottom"))),aes(x = Type, y = mean + sd, label = groups), position = position_dodge(0.75), vjust = -0.5, size = 5) +geom_segment(data = cData1_summary1 %>% filter(p_value <= 0.1 & var_type == "ftValue"),aes(x = 0.75, xend = 0.75, y = 85, yend = 88))+geom_segment(data = cData1_summary1 %>% filter(p_value <= 0.1 & var_type == "ftValue"),aes(x = 0.75, xend = 1.75, y = 88, yend = 88))+geom_segment(data = cData1_summary1 %>% filter(p_value <= 0.1 & var_type == "ftValue"),aes(x = 1.75, xend = 1.75, y = 85, yend = 88))+geom_text(data = cData1_summary1 %>% filter(p_value <= 0.1 & var_type == "ftValue"),aes(x = 1.25, y = 88, label = paste0("p = ", p_value)),vjust = -0.5, size = 5)+geom_text(data = cData1_summary1 %>% filter(p_value <= 0.1 & var_type == "ftValue"),aes(x = 1.25, y = 90, label = label),vjust = -1, size = 5)+scale_fill_manual(values = c("#393939", "#A2A2A2", "#CCCCCC")) +scale_y_continuous(expand = c(0, 0), limits = c(0, 100), breaks = seq(0, 100, 50)) +theme_classic()+theme(legend.position = "top",axis.ticks.length.y = unit(0.2, "cm"),axis.text.y = element_text(color = "black", size = 12),axis.title.y = element_text(color = "black", size = 12, face = "bold"),axis.title.x = element_blank(),axis.text.x = element_blank(),axis.line.x = element_blank(),axis.ticks.x = element_blank())+labs(y = "FTvalue", fill = "", pattern = "");ftValue

在这里插入图片描述

合并图ab

ggarrange(ctValue, ftValue, nrow = 2, ncol = 1, labels = c ("A", "B"),align = "hv", common.legend = T)

在这里插入图片描述
使用ggpubr包中的ggarrange函数完成拼图。

这个图展示了基于不同深度(Top、Center、Bottom)和类型(CK、BT)的ctValue。以下是一个简短的解读:
柱状图:使用geom_bar_pattern函数创建柱状图。柱子的高度代表每种类型和深度的平均ctValue。柱子的颜色是根据深度填充的,而模式则是基于类型填充的。
误差条:使用geom_errorbar函数添加误差条,表示平均值上下的标准差。
点:使用geom_point函数绘制ctValue的单个数据点。

注释:
geom_text函数向图表添加文本注释。似乎有某些群组和p值的注释。
使用geom_segment函数绘制的线条表示显著性的比较。

美学和主题:
scale_fill_manual函数用于手动设置柱子的颜色。
使用theme_classic和theme函数定制图表的外观。
使用labs函数将图的y轴标记为"CTvalue"。
要可视化数据,您需要相应的数据框(cData1_mean1、cData1_mean、cData1和cData1_summary1),并确保加载了所需的库(ggplot2以及geom_bar_pattern等所需的其他库)。

复现效果还是比较完美的。中间可视化代码细节比较多,大家可以自行学习,可以留言提问答疑。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/131920.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Git同时配置Gitee和GitHub

Git同时配置Gitee和GitHub 一、删除原先ssh密钥二、生成密钥 这里的同时配置是针对于之前配置过单个gitee或者github而言的&#xff0c;如果需要看git从安装开始的配置&#xff0c;则可以看这一篇文章 git安装配置教程 一、删除原先ssh密钥 在C盘下用户/用户名/.ssh文件下找到…

ESP32S3入手体验测试

ESP32S3入手体验测试 &#x1f516;所入手的型号是YD-ESP32-S3 N16R8,该款和乐鑫官方推出的ESP32-S3-DevKitC-1配置差不多。 &#x1f388;乐鑫官方介绍&#xff1a;ESP32-S3-DevKitC-1 v1.1 &#x1f530;两者采用的模组&#xff1a;ESP32-S3-WROOM-1 和ESP32-S3-WROOM-1U模组…

“Java与Redis的默契舞曲:优雅地连接与存储数据“

文章目录 引言1. Java连接上Redis2. Java对Redis进行存储数据2.1 存储set类型数据2.2 存储hash类型数据2.3 存储list类型数据 总结 引言 在现代软件开发中&#xff0c;数据存储和处理是至关重要的一环。Java作为一门强大的编程语言&#xff0c;与Redis这个高性能的内存数据库相…

2023-11 | 短视频批量下载/爬取某个用户的所有视频 | Python

这里以鞠婧祎的个人主页为demo https://www.douyin.com/user/MS4wLjABAAAACV5Em110SiusElwKlIpUd-MRSi8rBYyg0NfpPrqZmykHY8wLPQ8O4pv3wPL6A-oz 【2023-11-4 23:02:52 星期六】可能后面随着XX的调整, 方法不再适用, 请注意 找到接口 找到https://www.douyin.com/aweme/v1/web/…

C++ map 的使用

下面的是关于 map 的介绍。来自 map - C Reference (cplusplus.com) 的翻译&#xff0c;您可以看也可以不看哈&#xff01; map 是关联容器&#xff0c;它按照特定的次序(按照 key 来比较)存储由键值 key 和值 value组合而成的元素。在 map 中&#xff0c;键值 key 通常用于排序…

P2824 [HEOI2016/TJOI2016] 排序

P2824 [HEOI2016/TJOI2016] 排序 [HEOI2016/TJOI2016] 排序 题目描述 在 2016 2016 2016 年&#xff0c;佳媛姐姐喜欢上了数字序列。因而她经常研究关于序列的一些奇奇怪怪的问题&#xff0c;现在她在研究一个难题&#xff0c;需要你来帮助她。 这个难题是这样子的&#x…

linux 创建git项目并提交到gitee(保姆式教程)

01、git安装与初始化设置 mhzzjmhzzj-virtual-machine:~/work/skynetStudy$ apt install mhzzjmhzzj-virtual-machine:~/work/skynetStudy$ git config --global user.name "用户名" mhzzjmhzzj-virtual-machine:~/work/skynetStudy$ git config --global user.ema…

Python自定义函数练习(持续更新中~)

1.计算矩阵的面积和周长&#xff1a; class Rectangle:def __init__(self, width, height):self.width widthself.height heightdef area(self):return self.width * self.heightdef perimeter(self):return 2 * (self.width self.height)if __name__ "__main__"…

深入理解JS中的this

1、浅谈this 1.1、调用位置 在学习this的绑定过程之前&#xff0c;首先要理解调用位置&#xff0c;即函数在代码中被调用的位置&#xff0c;因此我们需要分析调用栈&#xff0c;看以下代码 function baz(){// 当前调用栈是baz// 因此调用位置就是全局作用域console。log(&qu…

财务RPA机器人如何使用

随着科技的不断发展&#xff0c;自动化技术在各个领域得到了广泛应用&#xff0c;在财务领域&#xff0c;RPA机器人已经成为一种新兴的技术手段&#xff0c;帮助众多企业实现了财务流程的自动化&#xff0c;大大提高了工作效率&#xff0c;降低人力成本。 本文将详细介绍财务RP…

AtCoder Beginner Contest 327 G. Many Good Tuple Problems(带标号二分图计数+有区别小球放入有区别盒子)

题目 一个长为n(n<30)的原始序列x&#xff0c;x[i]可以取值0或1 一个长为m(m<1e9)的点对序列(s,t)&#xff0c; s序列第i项和t的第i项&#xff0c;均可以取值[1,n]&#xff0c; 如果构造好s和t后&#xff0c;对任意都存在01序列x使得&#xff0c; 则称这个序列是合法…

【RabbitMQ】 RabbitMQ 消息的延迟 —— 深入探索 RabbitMQ 的死信交换机,消息的 TTL 以及延迟队列

文章目录 一、死信交换机1.1 什么是死信和死信交换机1.2 死信交换机和死信队列的创建方式 二、消息的 TTL2.1 什么是消息的 TTL2.2 基于死信交换机和 TTL 实现消息的延迟 三、基于 DelayExchang 插件实现延迟队列3.1 安装 DelayExchang 插件3.2 DelayExchang 实现消息延迟的原理…

MySQL5.7小版本升级-In-Place方式

在 Centos7.9上升级二进制方式安装的MySQL 。升级方式有就地升级和逻辑升级2种方法&#xff0c;本实验演示In-Place Upgrade就地升级的方式将mysql-5.7.17升级至5.7.44 In-Place Upgrade就地升级Logical Upgrade逻辑升级 升级方式介绍 In-Place Upgrade就地升级 In-Place U…

perl列表创建、追加、删除

简介 perl 列表追加元素 主要是通过push和unshift函数来实现。其中&#xff0c;push是追加到列表尾&#xff0c;unshift是追加到列表头。 perl列表删除元素 主要是通过pop和shift函数来实现。其中&#xff0c;pop是从列表尾删除一个元素&#xff0c; shift是从列表头删除一…

6大场景,玩转ChatGPT!

文章目录 一、故事叙述提问举例 二、产品描述提问举例 三、报告撰写提问举例 四、邮件和信件撰写提问举例 五、新间稿和公告撰写提问举例 六、学术论文和专业文章撰写提问举例 本文是在GPT3.5版本下演示的 我们知道AI技术不仅能够自动生成文章和内容&#xff0c;还可以根据我们…

【大数据】NiFi 中的重要术语

NiFi 中的重要术语 1.Flow Controller2.Processor3.Connection4.Controller Service5.Process Group6.FlowFile 那些一个个黑匣子称为 Processor&#xff0c;它们通过称为 Connection 的队列交换名为 FlowFile 的信息块。最后&#xff0c;FlowFile Controller 负责管理这些组件…

软考高级系统架构设计师系列之:软考高级系统架构设计师论文专题

软考高级系统架构设计师系列之:软考高级系统架构设计师论文专题 一、论文相关内容二、论文专题大纲三、论文考试方式四、历年真题汇总分析五、论文常见问题六、论文评分标准七、搭建论文万能模版八、论文万能模版公式九、搭建论文万能模版—摘要十、搭建论文万能模版—背景十一…

XSAN数据恢复-存储空间架构迁移时误格式化存储系统的XSAN数据恢复案例

XSAN数据恢复环境&#xff1a; 昆腾存储&#xff0c;MAC OS操作系统&#xff0c;存放视频类数据&#xff08;MXF、MOV等格式文件&#xff09;。 XSAN故障&检测&#xff1a; 将存储空间从XSAN架构迁移到STORNEXT架构后&#xff0c;存储空间中数据全部丢失。 故障存储中一共…

Oracle常用运维SQL-SQL执行性能及锁表等查询分析

oracle相关系列文章: docker–在Anaconda jupyter 容器中使用oracle数据源时,Oracle客户端安装配置及使用示例 Oracle常用运维SQL–用户管理、数据导入、导出的实用脚本 Oracle TEMPORARY TABLE 临时表的使用及删除报ORA-14452错误的解决办法 Oracle常用运维SQL-SQL执行性能及…

R 复习 菜鸟教程

R语言老师说R好就业&#xff0c;学就完了 基础语法 cat()可以拼接函数&#xff1a; > cat(1, "加", 1, "等于", 2, \n) 1 加 1 等于 2sink()&#xff1a;重定向 sink("r_test.txt", splitTRUE) # 控制台同样输出 for (i in 1:5) print(i…