箱型图

比较不同数据的平均数和p-value值

library(ggplot2)
library(ggpubr)
# 读取文件
dataA <- read.table("A2_At_conserveRate.txt")
dataD <- read.table("D5_Dt_conserveRate.txt")
dataA2 <- read.table("D5_A2_conserveRate.txt")
dataAt <- read.table("At_Dt_conserveRate.txt")
# 增加分组
dataA2$V3 <- "A2_vs_D5"
dataAt$V3 <- "At_vs_Dt"
dataA$V3 <- "At_vs_A2"
dataD$V3 <- "Dt_vs_D5"
# 合并数据
mergeData <- rbind(dataA, dataD, dataA2, dataAt)
# 过滤数据
filteData <- mergeData[mergeData$V2 > 0, ]
## 修改因子水平
filteData$V3 <- factor(filteData$V3, levels = c("At_vs_A2", "Dt_vs_D5", "A2_vs_D5", "At_vs_Dt"))
ggplot(data = filteData, aes(x = V3, y = V2, fill = V3)) +
  geom_violin() +
  #自定义填充色
  scale_fill_manual(values = c(
    "#f6e58d", "#ffbe76", "#686de0", "#4834d4"
    )) +
  geom_boxplot(width = 0.2,
  fill="#ecf0f1", #填充色
  ) +
  theme(
    panel.grid = element_blank(), #网格线为空
    panel.background = element_blank(), #背景色为空
    #坐标轴
    axis.line = element_line(size = 0.5,color="black"),
    axis.text.x = element_text(size = "15"),
    axis.text.y = element_text(size = "10"),
    axis.title.y = element_text(size = "15"),
    legend.position = "none",#隐藏所有图例
  ) +
   #修改坐标轴名称
   ylab("conserve rate")+
   xlab("")+
  stat_compare_means(comparisons = list(
    c("At_vs_A2", "Dt_vs_D5"),
    c("A2_vs_D5", "At_vs_Dt")
  ))

修改显著性水平的高度

  stat_compare_means(comparisons =list(
    c("evm.TU.","Ghir_A1"),
    c("Gorai.0","Ghir_D1")
    ),
    label.y = 15.5 ##修改高度
    )

修改显著性标记和统计方法

  stat_compare_means(
    comparisons = list(
    c("high", "mild"),
    c("mild", "None")
  ),
  method="wilcox.test",
  label.y = c(12,13),
  label="p.signif"  ##显示星星
  )

修改显著性星星的数目和大小

  stat_compare_means(
    comparisons = list(
    c("high", "mild"),
    c("mild", "None")
  ),
  method="wilcox.test",
  label.y = c(12,13),
  label="p.signif",
  symnum.args = list(cutpoints = c(0, 0.0001, 0.001, 0.01, 0.05, 1), symbols = c("***", "***", "**", "*", "ns"),
size=6,  ##修改标记字体大小 
bracket.size=0.8,##修改线条粗细                        
  )

隐藏离群

  geom_boxplot(
    width = 0.2,
    fill="#ecf0f1", #柱子填充色
    outlier.color = NA, ##隐藏离群值
  )

修改箱型图内颜色

  geom_boxplot(
    width = 0.2,
    fill="#ecf0f1", #填充色
    outlier.color = NA,
    color="red" ##修改边框和中间线条颜色
  )

修改箱型图内边框粗细

  geom_boxplot(
    width = 0.2,
    fill="#f38181", #填充色
    outlier.color = NA,
    colour="#0d7377",
    size=2 ##修改线条粗细
  )

分组箱型图

geom_boxplot内部使用分组变量,使用position=position_dodge(0.8)调整组内间的间距

ggplot(data = mergeData, aes(x = V2, y =log2(V3+1))) +
  geom_boxplot(
    aes(fill=V1),
    position=position_dodge(0.8),
    width = 0.5,
    outlier.color = NA,
    colour="#0d7377",
    size=1
  )

分组箱型图添加显著性标记

  • tip_length 显著性括号的长度

geom_signif(y_position=c(6.2), xmin=c(2.8), xmax=c(3.2), 
              annotation=c("ns"), tip_length=0, size=0.8, textsize = 5, vjust = 0)

小提琴图

修改外边框

  geom_violin(
    trim=T ,##过滤尾巴
    color=NA ##不显示外边框
  )

分组箱型图并添加折线

安装ggpubr的时候出错

由于在R3.x中安装时nloptr编译不通过,指定适用于R3的版本进行安装即可

packageurl<-"https://cran.r-project.org/src/contrib/Archive/nloptr/nloptr_1.2.1.tar.gz"
install.packages(packageurl, repos=NULL, type="source")

参考

图例

https://blog.csdn.net/bone_ace/article/details/47284805#%E9%9A%90%E8%97%8F%E6%A0%87%E9%A2%98

https://www.jianshu.com/p/9ae6e06d4947

Last updated