将自己公式中的 p 值添加到 ggplot2

Add p-values from own formula to ggplot2

我想从图中的特定公式添加不同的 p 值。我需要每个主题的不同 p 值。这是我使用的代码,它不起作用:

formula <- lme(scale(Inactive.freq)~ scale(Time.point), random=~ 1|Subject, data=Freq_df,  method='ML')

gggplot(Freq_df, aes(x=Time.point, y=Inactive.freq, group=Subject,colour=Subject)) +
        geom_line(size=2)+
        theme_minimal()+ 
        geom_point()+
        stat_smooth(method=lm, se = FALSE,linetype ="dashed")+
        geom_smooth(method = "lm", formula = formula)+
        stat_poly_eq(aes(label =  paste(stat(eq.label),
                                        stat(adj.rr.label), sep = "~~~~")), formula = formula, parse = TRUE) + 
        stat_fit_glance(label.x.npc = "right", label.y.npc = "bottom", geom = "text", 
                        aes(label = paste("P-value = ", signif(..p.value.., digits = 3), sep = "")))

如有任何帮助,我将不胜感激。谢谢!

更新 我的数据:

structure(list(Subject = structure(c(1L, 1L, 1L, 1L, 1L, 1L), .Label = 
c("Caesar", 
"DL", "Kyosti", "Paul", "Richards", "Taylor"), class = "factor"), 
Time.point = c(1, 3, 4, 5, 6, 7), Pacing.freq = c(0.644444444444444, 
0.562962962962963, 0.411111111111111, 0.122222222222222, 
0, 0), Affiliative.freq = c(0.0703125, 0.138576779026217, 
0.00760456273764259, 0.00617283950617284, 0.0634920634920635, 
0.0629370629370629), Inactive.freq = c(0, 0, 0.174904942965779, 
0.518518518518518, 0.290322580645161, 0.172661870503597), 
Not.alert.alone.freq = c(0, 0, 0.174904942965779, 0.518518518518518, 
0.279569892473118, 0.165467625899281), Not.alert.with.cagemate.freq = c(0, 
0, 0, 0, 0.0108695652173913, 0.00719424460431655), Alert.with.cagemate.freq = c(0.06640625, 
0.0262172284644195, 0, 0, 0, 0.00719424460431655), Non_visible = c(15L, 
3L, 7L, 18L, 84L, 131L), Visible = c(255L, 267L, 263L, 162L, 
186L, 139L)), row.names = c(NA, 6L), class = "data.frame")

这可以通过包 ggpmisc 中提供的 "stat_fit_glance" 方法使用另一层来完成(我相信您已经在使用它...)。这是一个很棒的包,具有更多用于注释 ggplot2 的功能。

解决方案是:

The modified data

Freq_df <- structure(list(Subject = as.factor(c(rep("Caesar", 3), rep("DL", 3))), 
                     Time.point = c(1, 3, 4, 5, 6, 7), 
                     Pacing.freq = c(0.644444444444444, 0.562962962962963, 
                     0.411111111111111, 0.122222222222222, 0, 0), 
                     Affiliative.freq = c(0.0703125, 0.138576779026217, 0.00760456273764259,
                     0.00617283950617284, 0.0634920634920635, 0.0629370629370629), 
                     Inactive.freq = c(0, 0, 0.174904942965779, 0.518518518518518, 
                     0.290322580645161, 0.172661870503597), 
                     Not.alert.alone.freq = c(0, 0, 0.174904942965779, 0.518518518518518, 
                     0.279569892473118, 0.165467625899281), 
                     Not.alert.with.cagemate.freq = c(0, 0, 0, 0,  
                     0.0108695652173913, 0.00719424460431655), 
                     Alert.with.cagemate.freq = c(0.06640625, 0.0262172284644195, 0, 0, 0,    
                     0.00719424460431655), 
                     Non_visible = c(15L, 3L, 7L, 18L, 84L, 131L),
                     Visible = c(255L, 267L, 263L, 162L, 186L, 139L)), 
                     row.names = c(NA, 6L), class = "data.frame")

需要更改数据,因为除非至少有两个数据点,否则无法拟合一条线,而您为每个受试者提供了一个数据点。所以我把它限制在两个科目,每个科目三分。但是你明白了:)

The plotting code

    ggplot(Freq_df, aes(x = Time.point, y = Pacing.freq)) + ylim(-0.5, 1.5) + 
    geom_line(size=2, alpha = 0.5) + geom_point(aes(group = "Subject"), size = 3) + 
    geom_smooth(method = "lm", formula = formula) + facet_wrap('Subject') +
    stat_poly_eq(aes(label =  paste(stat(eq.label), stat(adj.rr.label), 
                 sep = "~~~~")), formula = formula, parse = TRUE) + 
    stat_fit_glance(label.x.npc = "right", label.y.npc = "bottom", geom = "text", 
                    aes(label = paste("P-value = ", signif(..p.value.., digits = 15), 
                    sep = "")))

编辑 1:

#another way to use `stat_fit_glance` (not shown in the graph here)
stat_fit_glance(label.x = "right", label.y = "bottom", 
                aes(label = sprintf('r^2~"="~%.3f~~italic(p)~"="~%.2f',
                    stat(r.squared), stat(p.value))), parse = T)

如果您需要每组单独的 p 值(单独的线拟合)(我相信也不要太多组),`Facet-wrap' 就可以解决问题......必须限制小平面的数量允许,我不知道!)。

OUTPUT

使用选项来获得所需的输出,例如如果您使用 label.x.npc = "left" & label.y.npc = "bottom",则回归方程和 p 值标签可能重叠。