【问题标题】:R iGraph Heatmap in Vertex顶点中的 R iGraph 热图
【发布时间】:2013-01-21 13:09:05
【问题描述】:

我对 R 很陌生,并且遇到了一个问题。 是否可以在iGraph 的顶点上打印热图? 我知道我可以做一个彩色的正方形或圆形。但是一个小的热图是可能的吗? 这是绘制我当前图表的代码:

    # create graph
graph <- graph.data.frame(network[,1:2])
vertex_names <- get.vertex.attribute(graph,"name")


# define node attributes
V(graph)$label.font <- 1
V(graph)$label.font[which(element_types[vertex_names,"type"]=="PRIMARIES")] <- 2
V(graph)$label.font[which(element_types[vertex_names,"type"]=="REACTION")] <- 2

V(graph)$label <- element_types[vertex_names,"label"]
V(graph)$color <- element_types[vertex_names,"color"]
V(graph)$size <- as.integer(element_types[vertex_names,"size"]*20)
V(graph)$label.cex <- element_types[vertex_names,"weight"]

V(graph)$frame.color <- "gray"
V(graph)$frame.color[which(element_types[vertex_names,"type"]=="PRIMARIES")] <- "white"
V(graph)$frame.color[which(element_types[vertex_names,"type"]=="PATHWAY")] <- "white"
V(graph)$frame.color[which(element_types[vertex_names,"type"]=="PRODUCTS")] <- "white"
V(graph)$frame.width <- 10

V(graph)$shape <- "square"
V(graph)$shape[which(element_types[vertex_names,"type"]=="REACTION")] <- "circle"

V(graph)$label.color <- "black"
V(graph)$label.color[which(element_types[vertex_names,"type"]=="PRIMARIES")] <- "darkred"
V(graph)$label.color[which(element_types[vertex_names,"type"]=="PATHWAY")] <- "darkgreen"
V(graph)$label.color[which(element_types[vertex_names,"type"]=="REACTION")] <- "darkorange3"

E(graph)$color <- "red"
E(graph)$color[which(network[,3]=="out")] <- "blue"
E(graph)$color[which(network[,3]=="external")] <- "darkgreen"
E(graph)$arrow.size <- 0.5

layout <- layout.auto(graph)
plot.igraph(graph,layout=layout,main=pathways[pathway_id,"COMMON-NAME"])

此外,我的列表中有矩阵,可以绘制成热图。 这些矩阵如下所示:

[[1]]
     TotalSNP HighSNP ModerateSNP PromotorSNP
[1,]        1       0           0           1
[2,]        3       0           2           1
[3,]        5       0           2           3
[4,]        1       0           0           1
[5,]        7       0           4           3
[6,]        3       0           3           0
[7,]        4       0           1           3
[8,]        3       0           2           1

[[2]]
     TotalSNP HighSNP ModerateSNP PromotorSNP
[1,]        3       0           1           2
[2,]        0       0           0           0

[[3]]
     TotalSNP HighSNP ModerateSNP PromotorSNP
[1,]        0       0           0           0
[2,]        0       0           0           0

有谁知道是否可以将这些矩阵绘制为顶点上的热图?

样本数据:

    FinalList <- list(structure(c(1, 3, 5, 1, 7, 3, 4, 3, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 2, 2, 0, 4, 3, 1, 2, 1, 1, 3, 1, 3, 0, 3, 1), .Dim = c(8L, 
4L), .Dimnames = list(NULL, c("TotalSNP", "HighSNP", "ModerateSNP", 
"PromotorSNP"))), structure(c(3, 0, 0, 0, 1, 0, 2, 0), .Dim = c(2L, 
4L), .Dimnames = list(NULL, c("TotalSNP", "HighSNP", "ModerateSNP", 
"PromotorSNP"))), structure(c(0, 0, 0, 0, 0, 0, 0, 0), .Dim = c(2L, 
4L), .Dimnames = list(NULL, c("TotalSNP", "HighSNP", "ModerateSNP", 
"PromotorSNP"))))

矩阵的大小可以变化,它总是 4 列,但行数可以从 0 行到 10 行不等。

【问题讨论】:

  • 使用 0.6.4 版中的新顶点形状 API 实际上相对容易(正式发布,但 R 包已经在 CRAN 上)。如果您可以使您的示例具有可重复性并且我不需要自己提供一些示例数据,那么我在这里提供帮助会更容易。
  • 在这最后一步之前有很多数据处理,使这个可重现是相当困难的,但不是真的必要。这只是另一个graph &lt;- graph.data.frame(network[,1:2]) 0.6.4 版本(我已经安装)中的顶点形状是否有此功能?我还没找到呢。
  • 我不太关心数据处理,你也可以包含玩具数据,使用dput()textConnection(),见*.com/questions/5963269/… 这对你来说似乎是一个小问题,但是在许多情况下,想出一些有意义的玩具数据需要 5-10 分钟,而回答问题需要 1 分钟。如果您有一个可重复的示例并包含数据,则您获得答案的机会会大大增加。

标签: r vector heatmap igraph vertex


【解决方案1】:

因此,正如我在上面的评论中所说,使用新的顶点形状 API 相对容易,尽管您需要了解一些技巧。 igraph 中有一些示例形状,我猜对这些形状有帮助。

所以你需要定义一个函数来绘制你的顶点。它必须具有三个参数,矩阵中的坐标、要绘制的顶点(NULL 表示所有顶点)和一个可用于查询 igraph 图形参数的函数对象。如果您有兴趣,请查看代码中的详细信息。如果您想更改热图的外观,只需将image() 更改为您喜欢的任何内容。

myheat <- function(coords, v=NULL, params) {
  colbar <- heat.colors(50)
  colbreaks <- seq(0, 1, length=length(colbar)+1)
  vertex.size <- 1/200 * params("vertex", "size")
  if (length(vertex.size) != 1 && !is.null(v)) {
    vertex.size <- vertex.size[v]
  }
  heat <- params("vertex", "heat")
  if (is.list(heat) && !is.null(v)) {
    heat <- heat[v]
  } else if (!is.null(v)) {
    heat <- list(heat)
  }
  mapply(coords[,1], coords[,2], vertex.size*2, heat,
         FUN=function(x, y, size, int) {
           stopifnot(is.matrix(int))
           nc <- ncol(int); nr <- nrow(int)
           xc <- seq(x, x+size/nc*(nc-1), length=nc)-size/nc*(nc-1)/2
           yc <- seq(y, y+size/nr*(nr-1), length=nr)-size/nr*(nr-1)/2
           image(xc, yc, int, add=TRUE, col=colbar, breaks=colbreaks)
         })
}

# OK, we add the new shape now, it will be called "heat", 
# and will have an extra vertex parameter, also called "heat". 
# This parameter gives the heatmap intensities. The shape will 
# clip as a square, ie. the edges will be cut at the boundary 
# of the heatmap.

add.vertex.shape("heat", clip=vertex.shapes("square")$clip, plot=myheat,
                 parameters=list(vertex.heat=matrix(0,3,3)))

# Some example data and random heatmaps

g <- graph.formula(A:B -+ C:D +- E)
randheat <- function() matrix(runif(9), 3)
heats <- lapply(1:vcount(g), function(x) randheat())

# Plot them

plot(g, vertex.shape="heat", vertex.heat=heats, vertex.size=50)

# You can mix various vertex shapes

par(mar=c(0,0,0,0)+.1)
plot(g, vertex.shape=c("heat", "heat", "sphere", "heat", "heat"),
     vertex.heat=heats, vertex.size=50)

【讨论】:

  • 哈,这对 iGraph 的开发人员来说可能很容易是的! :P 非常感谢您的详细解决方案!我会尝试整合这是我的代码!
  • 您好,很抱歉再次打扰您,该功能与您的 randheat 功能完美配合。然而,我自己的数据没有标准矩阵大小。总是有 4 个列,但行数可能不同。也可以有 0 行。我已经尝试了很多来修改您的功能,但没有成功运行它。
  • 这是我遇到的错误Error in image.default(xc, yc, int, add = TRUE, col = colbar, breaks = colbreaks) : dimensions of z are not length(x)(-1) times length(y)(-1) In addition: Warning message: In vattrs[[name]][index] &lt;- value : number of items to replace is not a multiple of replacement length 我还用一些玩具数据编辑了我的问题。如果你能帮助我,那就太好了!!干杯,
  • 我最终通过在我的数据中添加一列或一行 0 来强制我的矩阵成为 x : x 维度。当它处于正方形 x:x 维度时,它工作得很好(4:4、5:5 等)。这确实添加了一些无用的数据,这些数据从来都不好。但这是一个解决方案。如果有人找到强制 img() 拉伸或允许顶点为矩形的方法,请告诉我。现在,我将其标记为已解决。再次感谢 Gabor Csardi!
最近更新 更多