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

这篇具有很好参考价值的文章主要介绍了跟着Nature Communications学作图:纹理柱状图+添加显著性标签!。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。


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

复现图片

跟着Nature Communications学作图:纹理柱状图+添加显著性标签!,【R绘图】,r语言,开发语言,ggplot2,可视化

跟着Nature Communications学作图:纹理柱状图+添加显著性标签!,【R绘图】,r语言,开发语言,ggplot2,可视化
主要复现红框部分,右侧的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)
vl

for(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_col
  group_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

跟着Nature Communications学作图:纹理柱状图+添加显著性标签!,【R绘图】,r语言,开发语言,ggplot2,可视化

图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

跟着Nature Communications学作图:纹理柱状图+添加显著性标签!,【R绘图】,r语言,开发语言,ggplot2,可视化

合并图ab

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

跟着Nature Communications学作图:纹理柱状图+添加显著性标签!,【R绘图】,r语言,开发语言,ggplot2,可视化
使用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等所需的其他库)。

复现效果还是比较完美的。中间可视化代码细节比较多,大家可以自行学习,可以留言提问答疑。文章来源地址https://www.toymoban.com/news/detail-737483.html

到了这里,关于跟着Nature Communications学作图:纹理柱状图+添加显著性标签!的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • 【Midjourney】Midjourney 基本操作 ⑤ ( 垫图 - 以图作图 / 上传图片 / 引用链接 + 提示词 | 为提示词添加权重 - ::提示词::权重数字 )

    如果想要在一张图片的基础上 , 绘制另一张图片 , 进行如下操作 ; 将图片从 桌面目录 中拖动到 Discord 界面中 , 拖动完以后 , 会显示如下内容 , 点击回车 , 即可将图片上传到 Discord 中 ; 右键点击图片 , 选择 \\\" 复制链接 \\\" 选项 ; 输入如下指令 , 先复制图片链接 , 然后空格 , 再后

    2024年02月09日
    浏览(39)
  • R语言绘图:实现数据点的线性拟合,进行显著性分析(R-squared、p-value)、添加公式到图像

    最近在做关于数据点线性拟合相关的研究,感觉R语言在这方面很方便,而且生成的图片很漂亮,所以在这里和大家分享一下代码。 这是别人所绘制的拟合图像,很漂亮,自己也用iris鸢尾花数据集进行一个线性拟合看看: 拟合线性模型最基本的函数就是 lm() ,格式为:myfit

    2024年02月11日
    浏览(27)
  • 【EXCEL】给数据添加图表(数据条、柱状图、折线图等),快速分析功能图文详解

    目录 0.环境 1.背景+简介 2.具体实现 2.1 给数据添加数据条 实现效果: 具体操作:  2.2 给数据添加柱状图图表 实现效果: 具体操作: 2.3 给数据添加迷你图(在表格中的折线图) 实现效果: 具体操作: windows + excel 2021 希望给数字类的数据增加多种形式的可视化,让数据看起

    2024年02月12日
    浏览(26)
  • Nature:刘清华团队揭示调控睡眠时间的关键分子通路

    导读 你能做到一周不睡觉吗?良好的睡眠对我们保证生活质量十分重要。不过,有些人每天只需睡4-6个小时,有些人则需要8个小时(可能还不够),这是什么原因导致的? 其实,这也是很多科学家好奇的问题,最近,研究人员在小鼠身上发现了调控睡眠时间的通路机制,揭

    2024年02月03日
    浏览(25)
  • 显著图(Saliency map)

    在计算机视觉中, 显著图 (Saliency map)是一种突出人们眼睛首先关注的区域的图像。 显著图的目标是反映像素对人类视觉系统的重要程度 。 显著性是图像的突出部分,我们的大脑会特别关注这个部分。例如,大家有没有曾经在看广告的时候被一些特别的内容吸引,为此我们还

    2024年02月10日
    浏览(20)
  • 推断统计|显著性水平|无偏抽样

    推断统计则是研究如何利用样本数据来推断总体特征的统计学方法,其内容包括参数估计和假设检验两大类。其中, 参数估计是利用样本信息推断总体特征;假设检验是利用样本信息判断对总体的假设是否成立。 推断统计学是统计学的一个重要分支,其主要目标是通过分析

    2024年02月09日
    浏览(29)
  • 解决——》CommunicationsException:Communications link failure

    推荐链接:     总结——》【Java】     总结——》【Mysql】     总结——》【Redis】     总结——》【Kafka】     总结——》【Spring】     总结——》【SpringBoot】     总结——》【MyBatis、MyBatis-Plus】     总结——》【Linux】     总结——》【MongoDB】    

    2024年02月09日
    浏览(24)
  • 【代码复现】BriVL:人大在Nature上发布的多模态图文认知基础模型

    Towards artificial general intelligence via a multimodal foundation model论文 Towards artificial general intelligence via a multimodal foundation model官方代码 The fundamental goal of artificial intelligence (AI) is to mimic the core cognitive activities of human. Despite tremendous success in the AI research, most of existing methods have only single-

    2024年04月28日
    浏览(40)
  • NCCL (NVIDIA Collective Communications Library)

    NCCL : NVIDIA Collective Communications Library 英伟达集体通信库 提供集合通信和点对点通信的 发送/接收原语。不是个成熟的并行编程框架;而是一个加速GPU内通信的库 NCCL 提供如下集体通信原语(collective communication primitives): AllReduce Broadcast Reduce AllGather ReduceScatter 他也允许点到点

    2024年02月13日
    浏览(22)
  • MATLAB | 如何使用MATLAB获取顶刊《Nature》全部绘图(附带近3年全部图像)

    我出了如何使用MATLAB获取期刊《Cell》全部绘图,立马就有粉丝问《Nature》、《Sience》、《PNAS》啥的会不会安排,这期就给大家安排《Nature》全部绘图获取,之后其他期刊也会慢慢安排,但是不会一次性全出完(毕竟不能抓住一个主题就狠更)。 由于《Nature》绘图获取需要科学

    2024年02月09日
    浏览(30)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包