user430997
user430997

Reputation: 317

Matching a visualization style in R

I have a network graph in R:

library(dplyr)
library(igraph)

set.seed(123)

edges <- data.frame(
    SOURCE = sample(1:30, 100, replace = TRUE),
    TARGET = sample(1:30, 100, replace = TRUE)
)

edges <- edges[edges$SOURCE != edges$TARGET, ]

edges <- edges %>%
    mutate(
        min_node = pmin(SOURCE, TARGET),
        max_node = pmax(SOURCE, TARGET)
    ) %>%
    distinct(min_node, max_node) %>%
    transmute(
        SOURCE = min_node,
        TARGET = max_node
    )

legend <- data.frame(
    NODE = 1:30,
    COLOR = sample(c("red", "blue", "green", "purple", "yellow"), 
                  30, replace = TRUE)
)

g <- graph_from_data_frame(edges, directed = FALSE)

V(g)$color <- legend$COLOR[match(V(g)$name, legend$NODE)]

plot(g)

enter image description here

I want to make this network graph in the same style as the following graph (https://yunranchen.github.io/intro-net-r/advanced-network-visualization.html):

enter image description here

I tried to rewrite my code based on the code in the tutorial:

library(statnet)
library(GGally)
library(ggnetwork)
devtools::install_github("sctyner/geomnet")
library(geomnet)
library(statnet)

set.seed(123)

edges <- data.frame(
   SOURCE = sample(1:30, 100, replace = TRUE),
   TARGET = sample(1:30, 100, replace = TRUE)
)

edges <- edges %>%
   filter(SOURCE != TARGET) %>%
   mutate(
       min_node = pmin(SOURCE, TARGET),
       max_node = pmax(SOURCE, TARGET)
   ) %>%
   distinct(min_node, max_node) %>%
   transmute(
       SOURCE = min_node,
       TARGET = max_node
   )

vertices <- data.frame(
   label = 1:30,
   group = sample(c("Group A", "Group B", "Group C", "Group D", "Group E"), 
                 30, replace = TRUE)
)

net <- network::network(edges, vertex.attr = vertices, directed = FALSE)

net %v% "group" <- vertices$group[match(network.vertex.names(net), vertices$label)]

edge_same_group <- matrix(0, nrow = network.size(net), ncol = network.size(net))
for(i in 1:nrow(edges)) {
   source_group <- vertices$group[edges$SOURCE[i]]
   target_group <- vertices$group[edges$TARGET[i]]
   edge_same_group[edges$SOURCE[i], edges$TARGET[i]] <- 
       ifelse(source_group == target_group, 1, 0)
}
set.edge.attribute(net, "same.group", as.vector(edge_same_group[edge_same_group != 0]))

set.edge.attribute(net, "lty", 
                 ifelse(net %e% "same.group" == 1, 1, 2))

network_plot <- ggnet2(net,
   mode = "fruchtermanreingold",
   layout.par = list(cell.jitter = 0.75),
   node.color = "group",
   palette = "Paired",
   node.size = 5,
   node.alpha = 0.75,
   edge.color = c("color", "grey50"),
   edge.alpha = 0.5,
   edge.size = 0.3,
   edge.lty = "lty",
   color.legend = "Group",
   legend.size = 9
) +
   geom_point(aes(color = color), size = 3)

print(network_plot)

enter image description here

I am unable to see the numbers on each node and the dotted lines. How can I match the visualization style to the tutorial as close as possible?. Can someone please show me how to do this?


After a bit of work, I got the numbers to show up!

set.seed(123)

edges <- data.frame(
    SOURCE = sample(1:30, 100, replace = TRUE),
    TARGET = sample(1:30, 100, replace = TRUE)
)

edges <- edges %>%
    filter(SOURCE != TARGET) %>%
    mutate(
        min_node = pmin(SOURCE, TARGET),
        max_node = pmax(SOURCE, TARGET)
    ) %>%
    distinct(min_node, max_node) %>%
    transmute(
        SOURCE = min_node,
        TARGET = max_node
    )

vertices <- data.frame(
    label = 1:30,
    group = sample(c("Group A", "Group B", "Group C", "Group D", "Group E"), 
                   30, replace = TRUE)
)

net <- network::network(edges, vertex.attr = vertices, directed = FALSE)

net %v% "group" <- vertices$group[match(network.vertex.names(net), vertices$label)]

edge_same_group <- matrix(0, nrow = network.size(net), ncol = network.size(net))
for(i in 1:nrow(edges)) {
    source_group <- vertices$group[edges$SOURCE[i]]
    target_group <- vertices$group[edges$TARGET[i]]
    edge_same_group[edges$SOURCE[i], edges$TARGET[i]] <- 
        ifelse(source_group == target_group, 1, 0)
}

set.edge.attribute(net, "same.group", as.vector(edge_same_group[edge_same_group != 0]))
set.edge.attribute(net, "lty", 
                   ifelse(net %e% "same.group" == 1, 1, 2))

network_plot <- ggnet2(net,
                       mode = "fruchtermanreingold",
                       layout.par = list(cell.jitter = 0.75),
                       node.color = "group",
                       palette = "Paired",
                       node.size = 9,
                       node.alpha = 0.75,
                       edge.color = c("color", "grey50"),
                       edge.alpha = 0.5,
                       edge.size = 0.3,
                       edge.lty = "lty",
                       node.label = TRUE,
                       node.label.color = "black",
                       node.label.size = 3,
                       color.legend = "Group",
                       legend.size = 9
)

print(network_plot)

enter image description here

Upvotes: 1

Views: 61

Answers (0)

Related Questions