ggpubr:在标签中显示显着性水平(*** 或 n.s.)而不是 p 值
Posted
技术标签:
【中文标题】ggpubr:在标签中显示显着性水平(*** 或 n.s.)而不是 p 值【英文标题】:ggpubr: Show significance levels (*** or n.s.) instead of p-value in the label 【发布时间】:2019-05-05 19:39:39 【问题描述】:我想在我的线性回归中使用 R 中的ggpubr
将显着性水平(***
或n.s.
)显示为标签。这似乎是通过使用aes(label = ..p.signif..)
来完成的,如下所示:https://www.r-bloggers.com/add-p-values-and-significance-levels-to-ggplots/
但是,当我在stat_cor(aes(label = paste(..rr.label.., ..p.label.., sep = "~
,~"))
中将..p.label..
替换为..p.signif..
时,即。 stat_cor(aes(label = paste(..rr.label.., ..p.signif.., sep = "~,
~"))` 我的情节没有任何变化,只是我得到一个错误:
Error in paste(rr.label, p.signif, sep = "~`,`~") :
object 'p.signif' not found
请问,我怎样才能绘制星星(*、、*)或 ns。值而不是我的情节上的精确 p 值?非常感谢。
我的虚拟数据:(借自http://www.sthda.com/english/articles/24-ggpubr-publication-ready-plots/78-perfect-scatter-plots-with-correlation-and-marginal-histograms/)
library(ggpubr)
data("mtcars")
df <- mtcars
df$cyl <- as.factor(df$cyl)
ggscatter(df, x = "wt", y = "mpg",
add = "reg.line", # Add regression line
conf.int = TRUE, # Add confidence interval
color = "cyl", palette = "jco", # Color by groups "cyl"
shape = "cyl" # Change point shape by groups "cyl"
)+
stat_cor(aes(color = cyl,
label =paste(..rr.label.., ..p.label.., sep = "~`,`~")), # HOW TO CHANGE p.label to show stars???
label.x = 3) # Add correlation coefficient
【问题讨论】:
【参考方案1】:你可以使用cut
:
ggscatter(df, x = "wt", y = "mpg",
add = "reg.line", # Add regression line
conf.int = TRUE, # Add confidence interval
color = "cyl", palette = "jco", # Color by groups "cyl"
shape = "cyl" # Change point shape by groups "cyl"
)+
stat_cor(aes(color = cyl,
label =paste(..rr.label.., cut(..p..,
breaks = c(-Inf, 0.0001, 0.001, 0.01, 0.05, Inf),
labels = c("'****'", "'***'", "'**'", "'*'", "'ns'")),
sep = "~")),
label.x = 3)
不用说,显示 p 值(或者甚至更好的是置信区间)要好得多。
【讨论】:
非常感谢@Roland。但是,当我运行您的解决方案时,我返回错误:Error in
levels(*tmp*, value = if (nl == nL) as.character(labels) else paste0(labels, : factor level [3] is duplicated
你知道为什么会发生此错误吗?以上是关于ggpubr:在标签中显示显着性水平(*** 或 n.s.)而不是 p 值的主要内容,如果未能解决你的问题,请参考以下文章