Edin Mar
Edin Mar

Reputation: 121

How to make a star plot with igraph?

I am working with igraph for the first time and would like to do a "star plot"(make_star()) with the package igraph.

For this I have prepared a sample data set, it has two columns: name and wght.

I want "ME" to be in the center of the plot and all arrows should go out of it. It would be great if the arrow width corresponded to the values ​​from wght (maybe with edge.width) OR the weights on the arrows.

My code looks like this:

library(igraph)
wght <- runif(6, min = 1, max = 10)
name <- c("John", "Jim", "Jack", "Jesse", "Justin", "Peter")
data <- data.frame(name, wght)


st <- make_star(n = 6, mode = "out")
plot(st, vertex.label = data$name)

Output:

enter image description here

what I want:

enter image description here

Upvotes: 2

Views: 959

Answers (2)

Henrik
Henrik

Reputation: 67778

Create a graph where "Me" is included as a vertex. Add edge attribute "weight". Create star layout with "Me" as center. Set edge widths according to weights. Plot!

g <- graph_from_data_frame(data.frame(from = "Me", to = name))
E(g)$weights <- wght

plot(g, layout = layout_as_star(g, center = V(g)["Me"]), edge.width = E(g)$weights)

enter image description here


Data

set.seed(1)
wght <- runif(6, min = 1, max = 10)
name <- c("John", "Jim", "Jack", "Jesse", "Justin", "Peter")

Upvotes: 2

Ben G
Ben G

Reputation: 4328

Fun to learn about a new package. This should do it for you:

st <- make_star(n=6,mode = "out") %>%
      set_vertex_attr("label", index = 1, value = "ME") %>%
      set_vertex_attr("label", index = 2:6, value = name[2:6])

plot(st)

enter image description here

Upvotes: 1

Related Questions