I am using the igraph package in R, and would like to modify the pie node shape. Rather than multiple colors, I would like circular nodes that are one color, but partially filled with a pattern, as in the example below.
I would also like to replicate this in square nodes. When using more than 2 colors/patterns, I would like the different colors/patterns to be displayed radially in the square, rather than in quadrants (see second example below).
Thanks!
Per the comment from @EricFail, here is a reproducible example of R code and the two-color pie node that it generates:
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)
I would like to replace one of the colors with a pattern fill. I would also ideally like the node to be divided horizontally rather than vertically.
Gabor Csardi's solution to this post R iGraph Heatmap in Vertex shows how to use a heat map image as a node in igraph, but I've been having trouble understanding some of the syntax and re-purposing it for my needs. Here is Csardi's code:
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)