igraph - adding text to community plot

谁说胖子不能爱 提交于 2021-01-27 06:13:18

问题


Using the code below I can generate a plot of the communities in the 'karate club' example provided by igraph (see first image).

library(igraph)
karate <- make_graph("Zachary")
wc <- cluster_walktrap(karate)
plot(wc, karate)

How can I add a text label 'near' each of the communities? For this example, say I want to call the 5 different communities "Community X" (X = 1:5). Ideally, the code would place the text labels on the 'outside' of the colored community polygons to create a clear graph (by placing the labels on the furthest xy coord from the center of the graph for each polygon). (See second image)

I've looked at this post, but the only suggestion is to add the labels manually.

Any ideas on how to add the text labels with code? Is it possible to 'get' the xy coords of the community polygons and add the text labels that way? (Not sure if this should be a new question or a comment on the linked post's answer - let me know as I'm new to this).

Thanks a lot!

igraph community example

igraph community example labeled

EDIT: Based on the comment below, I identified the following code that seems to be drawing the polygons for each of the groups/communities:

params <- i.parse.plot.params(graph, list(...))
#...
layout <- params("plot", "layout")
#....
for (g in seq_along(mark.groups)) {
v <- V(graph)[mark.groups[[g]]]
#...
igraph.polygon(layout[v, , drop = FALSE], vertex.size = vs, 
               expand.by = mark.expand[g]/200, shape = mark.shape[g], 
               col = mark.col[g], border = mark.border[g])

}

However, I can't find any information about the igraph.polygon function. I also don't know what to do with the params and 'layout[v, ,]'. Further, even if I could figure all this out, how would I change igraph's plotting function and add a text label??

来源:https://stackoverflow.com/questions/47971179/igraph-adding-text-to-community-plot

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!