简体   繁体   中英

How to change vertex.frame.color in igraph for R for a customized shape?

Could you please help me?

I'm plotting a.network in igraph using a customized diamond shape . The shape is created with the following function, which is a modification of the triangle shape function provided in the igraph manual :

MyDiamond <- function(coords, v=NULL, params) {
  vertex.color <- params("vertex", "color")
  if (length(vertex.color) != 1 && !is.null(v)) {
    vertex.color <- vertex.color[v]
  }
  vertex.frame.color <- params("vertex", "frame.color")
  if (length(vertex.frame.color) != 1 && !is.null(v)) {
    vertex.frame.color <- vertex.frame.color[v]
  }
  vertex.size <- 1/200 * params("vertex", "size")
  if (length(vertex.size) != 1 && !is.null(v)) {
    vertex.size <- vertex.size[v]
  }
  
  symbols(x=coords[,1], y=coords[,2], bg=vertex.color,
          stars=cbind(vertex.size, vertex.size, vertex.size, vertex.size),
          add=TRUE, inches=FALSE)
}
add_shape("diamond", clip=shapes("circle")$clip,
          plot=MyDiamond, parameters=list(vertex.frame.color="white",
                                           vertex.frame.width=1))

After calling this function, I plot the graph, like in the igraph manual:

shapes <- setdiff(shapes(), "")

g <- make_ring(length(shapes))

plot(g, vertex.shape="diamond", 
     vertex.color=rainbow(vcount(g)),
     vertex.frame.color = "white",
     vertex.size=seq(10,20,length=vcount(g)))

As you see, the argument vertex.frame.color is not working with this customized diamond shape. How can I make it work?

Thank you very much!

I found a solution!

It was just a matter of adding the fg argument to the end of the diamond shape function, in the call for symbols .

MyDiamond <- function(coords, v=NULL, params) {
  vertex.color <- params("vertex", "color")
  if (length(vertex.color) != 1 && !is.null(v)) {
    vertex.color <- vertex.color[v]
  }
  vertex.frame.color <- params("vertex", "frame.color")
  if (length(vertex.frame.color) != 1 && !is.null(v)) {
    vertex.frame.color <- vertex.frame.color[v]
  }
  vertex.size <- 1/200 * params("vertex", "size")
  if (length(vertex.size) != 1 && !is.null(v)) {
    vertex.size <- vertex.size[v]
  }
  
  symbols(x=coords[,1], y=coords[,2], bg=vertex.color, fg=vertex.frame.color,
          stars=cbind(vertex.size, vertex.size, vertex.size, vertex.size),
          add=TRUE, inches=FALSE)
}
add_shape("diamond", clip=shapes("circle")$clip,
          plot=MyDiamond, parameters=list(vertex.frame.color="white",
                                           vertex.frame.width=1))

Hope it helps other people.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM