首页 文章

在R中生成矩阵中某些相关性的图形

提问于
浏览
12

我想在变量(列)之间生成图形,这些图形具有高于和低于某个点的相关性以及p值<0.01 . 图表将是绘制相关的两列(变量)的ggplot2(线或条)图 .

到目前为止,这是我的方法的要点,有一些虚拟数据,我会喜欢指向下一步的指针 .

# Create some dummy data
df <- data.frame(sample(1:50), sample(1:50), sample(1:50), sample(1:50))
colnames(df) <- c("var1", "var2", "var3", "var4")

# Find correlations in the dummy data
df.cor <- cor(df)

# Make up some random pvalues for this example
x <- 0:1000
df.cor.pvals <- data.frame(sample(x/1000, 4), sample(x/1000, 4), sample(x/1000, 4), sample(x/1000,4))
colnames(df.cor.pvals) <- c("var1", "var2", "var3", "var4")

# Find the significant correlations
df.cor.extreme <- ((df.cor < -0.01 | df.cor > 0.01) & df.cor.pvals < 0.5)

# Ready data to for plotting
df$rownames <- rownames(df)
df.melt <- melt(df, id="rownames")

# I want to plot the combinations of variables that have a TRUE value
# in the df.cor.extreme matrix

如果var1和var2的值为TRUE,则下面是硬编码示例 . 我假设这是我需要某种循环来生成多个图的地方,其中varA和varB是相关的 .

ggplot(df.melt[(df.melt$variable=="var1" | df.melt$variable=="var2"),], aes(x=rownames, y=value, group=variable, colour=variable)) +
  geom_line()

Example plot

2 回答

  • 8

    如@DrewSteen的评论中所述,p-avlue必须与cor的形状相同 .

    在这里,我提供了一个计算p值矩阵的函数(它应该存在一个内置函数,在stats包中)

    pvalue.matrix <- function(x,...){
      ncx <- ncol(x)
      r <- matrix(0, nrow = ncx, ncol = ncx)
      for (i in seq_len(ncx)) {
        for (j in seq_len(i)) {
          x2 <- x[, i]
          y2 <- x[, j]
          r[i, j] <-  cor.test(x2,y2,...)$p.value
        }
      }
      r <- r + t(r) - diag(diag(r))
      rownames(r) <- colnames(x)
      colnames(r) <- colnames(x)
      r
    }
    

    然后使用|的矢量化版本并且像这样

    df.cor.sig <- (df.cor > 0.01 | df.cor < -0.01) & pvalue.matrix(df) < 0.5
    

    情节是经典的geom_tile

    library(reshape2) ## melt
    library(plyr)     ## round_any
     library(ggplot2) 
    dat <- expand.grid(var1=1:4, var2=1:4)
    dat$value <- melt(df.cor.sig)$value
    dat$labels <- paste(round_any(df.cor,0.01) ,'(', round_any(pvalue.matrix(df),0.01),')',sep='')
    ggplot(dat, aes(x=var1,y=var2,label=labels))+ 
      geom_tile(aes(fill = value),colour='white')+
     geom_text()
    

    enter image description here

    OP澄清后编辑

    plots <- apply(dat,1,function(x){
        plot.grob <- nullGrob()
        if(length(grep(pattern='TRUE',x[3])) >0 ){
          gg <- paste('var',c(x[1],x[2]),sep='')
          p <- ggplot(subset(df.melt,variable %in% gg ), 
                aes(x=rownames, y=value, group=variable, colour=variable)) +
                geom_line()
          plot.grob <- ggplotGrob(p)
        }
        plot.grob
    
    })
    
    
    library(gridExtra)
    do.call(grid.arrange,  plots)
    

    enter image description here

  • 1

    如果您自己这样做,只是想在@agstudy的答案中添加一个补充 .

    如果您使用生成矩阵索引表的函数的结果,您可以将重要性应用于 . 即这一行:

    dat <- expand.grid(var1=1:4, var2=1:4)
    

    还要记住,上面一行中的硬编码4是你(正方形)网格的长度 . 无论如何,您可以通过执行以下代码来忽略任何重复图形的生成:

    # Find redunant pairs
    dat <- data.frame(t(apply(dat, 1, function(x){
      if(x[1]-x[2] <= 0) {    # If > zero than pair has come before.
        -x                    # If = zero than pair is same 
      } else x
    })))
    
    # Remove redundant pairs
    dat <- dat[dat$var1>0,]
    

    请享用!

相关问题