要在ggplot图上添加显著性符号和半封闭线段,可以使用ggpubr包中的stat_compare_means函数。

首先,将数据用ggplot函数绘制成图形:

library(ggplot2)

# 绘制散点图
ggplot(mtcars, aes(x = factor(cyl), y = mpg)) +
  geom_point()

然后,在图形上添加显著性符号和半封闭线段:

library(ggpubr)

# 绘制散点图,并添加显著性符号和半封闭线段
ggplot(mtcars, aes(x = factor(cyl), y = mpg)) +
  geom_point() +
  stat_compare_means(method = 't.test', label = 'p.format',
                     symnum.args = list(cutpoints = c(0, 0.001, 0.01, 0.05, 1),
                                        symbols = c('***', '**', '*', ' ')),
                     half = TRUE)

在这个例子中,使用了t.test方法进行比较,label参数指定了显著性符号的显示格式,symnum.args参数指定了显著性水平和符号,half参数指定了半封闭线段的显示。

结果如下图所示:

image.png

R语言 ggplot 图上添加显著性符号和半封闭线段

原文地址: https://www.cveoy.top/t/topic/nzTo 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录