如何格式化 data.frame 适合来自 pairwise.t.test 输出的 ggplot

How to format data.frame suitable for ggplot from pairwise.t.test output

我的df

    df <- data.frame(
    measurement = runif(120, min=1, max=25),    
    group = rep(c("A","B","C","D"), each=3, times=10)
)

我将结果放入 data.frame

   ttest_results <- as.data.frame(pairwise.t.test(df$measurement, df$group)$p.value)

如何设置适合绘制 ggplot 矩阵的 df 格式?

这是使用 Hadley 宇宙重塑数据的一种方法,即 dplyrtidyr

library(dplyr)
library(tidyr)
library(ggplot2)
ttest_results %>% 
  add_rownames("Group.1") %>% 
  gather("Group.2", "value", -Group.1) %>%
  filter(!is.na(value)) %>% 
  ggplot(aes(Group.1, Group.2, fill = value)) +
  geom_tile(height = .7, width = .7)