automaticly add p-values to facet plot

橙三吉。 提交于 2019-12-01 10:29:14

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.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!