3

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!

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

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)

pie node

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)
Community
  • 1
  • 1
A R
  • 41
  • 3
  • 1
    It's more likely that we will be able to help you if you make a minimal reproducible example to go along with your question. Something we can work from and use to show you how it might be possible to solve your problem. You can have a look at [this SO post](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) on how to make a great reproducible example in R. It could also show what you have already tried. – Eric Fail Dec 02 '15 at 18:41
  • You can take and modify the pie code and add it as a new vertex shape. It is here: https://github.com/igraph/rigraph/blob/ff9745f0f48ef8c33a3453b44cf23f7620a60660/R/plot.shapes.R#L806 – Gabor Csardi Dec 07 '15 at 15:08

0 Answers0