美文网首页
R_柱状图(加误差线,显著性差异)

R_柱状图(加误差线,显著性差异)

作者: 奔跑的Forrest | 来源:发表于2021-01-22 11:24 被阅读0次

    先放最终效果图


    结果图
    # bar_plot 2021/01/19
    # 导入所需的包
    library(ggplot2)
    library(ggthemr)
    library(ggsignif)
    library(tidyverse)
    library(dplyr)
    library(ggpubr)
    library(devEMF)
    
    # 导入并处理数据,需要两张表,一个是原始汇总表格,另外一些是每一个测量数据的均值和标准差
    data1 <- read.csv(file = "../all_enzymatic.txt", sep = "\t", header = T)
    data2 <- dplyr::select(data1,1,3,4,5,6)
    
    # SOD
    data_SOD <- dplyr::select(data2,1,5)
    SOD_mean <- data_SOD %>% 
      dplyr::group_by(group) %>% 
      dplyr::summarize(
        count=n(),
        mean = mean(SOD),
        sd = sd(SOD)
      )
    
    data_SOD
    SOD_mean
    ###################################################################
    # 分析数据,数据量比较少就可以不做了
    #正态分布检验:用shapiro.test()
    shapiro.test(data_SOD$SOD)    
    
    #方差齐性检验:用bartlett.test()或者leveneTest()
    bartlett.test(SOD~group,data = data_SOD)   #巴雷特检验
    
    library(car)  #leveneTest()属于car包
    leveneTest(data_SOD$SOD~data_SOD$group)  #列文检验
    
    compare_means(SOD~group,data = data_SOD,
                   method = "anova") #显著性计算
    ####################################################################
    
    #####################################################################
    plot_data1 <- SOD_mean
    plot_data2 <- data_SOD
    p4 <- ggplot()+ 
      geom_bar(data=plot_data1,mapping=aes(x=group,y=mean,fill=group), # fill填充
               position="dodge", # 柱状图格式
               stat="identity", # 数据格式
               width = 0.7)+  # 柱状图尺寸
      scale_fill_manual(values = c("#4E4E56", "#DA635D","#B1938B"))+ # 柱状图颜色
      geom_signif(data=plot_data2,mapping=aes(x=group,y=SOD), # 不同组别的显著性
                  comparisons = list(c("C", "HT"), # 哪些组进行比较
                                     c("HI", "HT")),
                  annotation=c("**"), # 显著性差异做标记
                  map_signif_level=T, # T为显著性,F为p value
                  tip_length=c(0.04,0.04,0.05,0.05), # 修改显著性那个线的长短
                  y_position = c(4100,3000), # 设置显著性线的位置高度
                  size=1, # 修改线的粗细
                  textsize = 10, # 修改*标记的大小
                  test = "t.test")+ # 检验的类型
      geom_errorbar(data=plot_data1,mapping=aes(x = group,ymin = mean-sd, ymax = mean+sd), # 误差线添加
                    width = 0.1, #误差线的宽度
                    color = 'black', #颜色
                    size=0.8)+ #粗细
      scale_y_continuous(limits =c(0, 4500) ,expand = c(0,0))+ # y轴的范围
      theme_classic(  # 主题设置,这个是无线条主题
        base_line_size = 1 # 坐标轴的粗细
      )+
      labs(title="",x="",y="")+ # 添加标题,x轴,y轴内容
      theme(plot.title = element_text(size = 20,
                                      colour = "red",
                                      hjust = 0.5),
            axis.title.y = element_text(size = 15, 
                                        # family = "myFont", 
                                        color = "black",
                                        face = "bold", 
                                        vjust = 1.9, 
                                        hjust = 0.5, 
                                        angle = 90),
            legend.title = element_text(color="black", # 修改图例的标题
                                        size=15, 
                                        face="bold"),
            legend.text = element_text(color="black", # 设置图例标签文字
                                       size = 10, 
                                       face = "bold"),
            axis.text.x = element_text(size = 13,  # 修改X轴上字体大小,
                                       # family = "myFont", # 类型
                                       color = "black", # 颜色
                                       face = "bold", #  face取值:plain普通,bold加粗,italic斜体,bold.italic斜体加粗
                                       vjust = 0.5, # 位置
                                       hjust = 0.5, 
                                       angle = 0), #角度
            axis.text.y = element_text(size = 13,  # 修改y轴上字体大小,
                                       # family = "myFont", # 类型
                                       color = "black", # 颜色
                                       face = "bold", #  face取值:plain普通,bold加粗,italic斜体,bold.italic斜体加粗
                                       vjust = 0.5, # 位置
                                       hjust = 0.5, 
                                       angle = 0) #角度
            ) 
    emf(file = "SOD.emf") # 打开一个矢量图画布,这种格式的图片放在word里不会失真
    print(p4) # 打印图片
    dev.off() #关闭画布
    

    相关文章

      网友评论

          本文标题:R_柱状图(加误差线,显著性差异)

          本文链接:https://www.haomeiwen.com/subject/jfsszktx.html