R图形饼图形状填充?

时间:2015-12-02 18:22:10

标签: r graph network-programming nodes igraph

我在R中使用igraph包,并想修改饼节点形状。而不是多种颜色,我希望圆形节点是一种颜色,但部分填充图案,如下例所示。

我还想在方形节点中复制它。当使用2种以上的颜色/图案时,我希望不同的颜色/图案在方形中径向显示,而不是在象限中显示(参见下面的第二个例子)。

谢谢!

pie node with pattern fill square node with one color and pattern fill square node with multiple colors/pattern fill

根据@EricFail的评论,这是一个可重现的R代码示例及其生成的双色饼节点:

library(igraph)

g <- graph.ring(1)
values <- lapply(1, function(x) sample(1, 2, replace=TRUE))
plot(g, vertex.shape="pie", vertex.pie=values,
 vertex.pie.color=list(heat.colors(2)),
 vertex.size=100, vertex.label=NA)

pie node

我想用图案填充替换其中一种颜色。理想情况下,我希望水平分割而不是垂直分割。

Gabor Csardi对这篇文章R iGraph Heatmap in Vertex的解决方案展示了如何将热图图像用作igraph中的节点,但我一直无法理解一些语法和重新定位它符合我的需要。这是Csardi的代码:

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)
}

### this is where i get a bit lost...
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)

0 个答案:

没有答案