Control which nodes to change size igraph

Viewed 226

This may look simple but I am not able to do this. I want to plot two types of nodes, small and big using a cutoff. The values are in the attribute degree.

Here is a small toy example

    g1 <- graph(edges=c(1,2, 2,3, 3, 1, 4,2), n=4, directed=F) %>%
    set_vertex_attr("names", value = LETTERS[1:4])
    g1_degree <- degree(g1, mode = "total")
     g1_degree
    [1] 2 3 2 1
    g1 <- set_vertex_attr(g1, "degree", value = g1_degree)
plot(g1, vertex.size=V(g1)$degree)

This gives me every node according to the degree, but I want nodes of degree 2 and 3 big and 1 small. So I tried to edit the values within V(g1)$degree

ifelse(V(g1)$degree < 2, yes = V(g1)$degree==1, no = V(g1)$degree==5)

FALSE FALSE FALSE TRUE

Ok, I checked my degree values, but how can I overwrite the TRUE or FALSE using the cutoffs I need?

1 Answers

Here are two solutions.

One with ifelse, like in the question.

g1 <- set_vertex_attr(g1, "degree", value = ifelse(V(g1)$degree < 2, 1, 5))

V(g1)$degree
#[1] 5 5 5 1

And another with findInterval. This has better performance than ifelse, which can be important in large data sets.

i <- findInterval(V(g1)$degree, c(0, 2, Inf))
g1 <- set_vertex_attr(g1, "degree", value = c(1, 5)[i])

V(g1)$degree
#[1] 5 5 5 1

With different new sizes set with the findInterval index, c(10, 50)[i], the graph would look like below.

g1 <- set_vertex_attr(g1, "degree", value = c(10, 50)[i])
plot(g1, vertex.size = V(g1)$degree)

enter image description here

Related