automaticly add p-values to facet plot

后端 未结 1 1380
天涯浪人
天涯浪人 2021-01-06 13:16

I have made a facet plot below using the following command:

    ggplot(data, aes(factor(Length),logFC)),
 + geom_boxplot(fill = \"grey90\"),
 +  coord_cartes         


        
相关标签:
1条回答
  • 2021-01-06 13:39

    You can do this by summarizing the data into a table of p-values. This can be done using dplyr:

    library(dplyr)
    pvalues <- data %>% group_by(Experiment, Modification, Length) %>%
        filter(n() > 1) %>%
        summarize(p.value = (t.test(logFC, mu = 0)$p.value))
    

    (The line filter(n() > 1) is to get rid of any groups of size 1, for which a p-value cannot be calculated). This produces a table that looks like:

    # Experiment Modification Length   p.value
    # 1       Daub          NTA     22 0.3980043
    # 2       Daub          NTA     23 0.3535590
    # 3       Daub          NTA     24 0.5831962
    # 4       Daub          NTA     25 0.9137644
    # 5       Daub          NTA     26 0.6254004
    # 6       Daub         t3-d     20 0.1493108
    

    Now you can add that text to your plot using a geom_text layer, choosing some y such as y = 3:

    library(ggplot2)
    
    ggplot(data, aes(factor(Length),logFC)) + geom_boxplot(fill = "grey90") +
        coord_cartesian(ylim=c(-5,5)) + facet_grid(Experiment~Modification) +
        geom_text(aes(y = 3, label = p.value), data = pvalues, size = 1)
    

    You will probably have to manipulate the size (and possibly angle) of your geom_text to make the plot readable. Note also that since you are performing many tests, you should probably look at the adjusted p-values rather than the raw p-values. You can compute that column with

    pvalues <- pvalues %>% mutate(p.adjusted = p.adjust(p.value, method = "bonferroni"))
    

    The function format.pval will also come in handy, especially if some of your p-values are close to 0.

    0 讨论(0)
提交回复
热议问题