Search code examples
rggplot2p-value

Add p-values from own formula to ggplot2


I would like to add different p-values from an specific formula in a plot. I need different p-values from each of the subjects. Here is the code I used, which did not work:

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 = "")))

I would appreciate any help. Thank you!

UPDATE My data:

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")

Solution

  • This can be done using another layer with the "stat_fit_glance" method provided with the package ggpmisc (which you are already using, I believe...). It's a great package with lot more capabilities for annotating ggplot2.

    The solution would be:

    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 data needed to be changed, as a line cannot be fitted unless at least two data points are there, whereas you provided one data point per subject. So I limited it to two subjects with three points per subject. But you get the idea :)

    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 = "")))
    

    EDIT 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)
    

    `Facet-wrap' will do the trick if you need seperate p-values (seperate line-fitting) per group (and also not too many groups I believe... there must be a limit to number of facets allowed, which I don't know!).

    OUTPUT

    P-value, ggpmisc, facet_wrap

    Play with the options to get desired output, e.g. if you use label.x.npc = "left" & label.y.npc = "bottom", then the regression equation & the p value labels might overlap.