4

New to R; did my best with the rendering my question with the help of with reprex.

I have a network with nodes sized by degree, using the ggraph package.

The plotted network doesn't look great because some of the nodes are quite small.

I would like to increase the relative size of the nodes.

In igraph, I would increase the relative size of the nodes with something like:

plot(df, vertex.cex=degree*5) 

I've tried something similar in ggraph (in the rerpex below) but the result is a multiplication of the degree value rather than an increase in the relative size of the node.

I would like to stick to the ggraph package if only because of the tidy/grammar approach and to manage the (oh so very steep) learning curve (although I could be convinced otherwise if anyone has some thoughts on the two packages).

The examples below don't have attached plots because my reputation isn't high enough to post images. But if I've done this right, the reprex should do what it's suppose to do.

#load libraries
library(tidygraph)
#> 
#> Attaching package: 'tidygraph'
#> The following object is masked from 'package:stats':
#> 
#>     filter
library(ggraph)
#> Loading required package: ggplot2
# Creating a data frame
df <- rbind(c(0,1,1,0,0,1,1,0,1),
            c(0,0,1,1,0,0,1,1,0),
            c(0,1,0,0,0,0,1,0,0),
            c(0,0,0,0,0,1,0,1,0),
            c(0,0,1,0,0,1,0,1,0),
            c(0,0,1,0,0,1,1,1,0),
            c(1,0,1,1,0,1,0,1,0),
            c(0,1,0,0,0,0,0,0,1),
            c(0,1,0,0,0,1,1,0,1))
# convert to matrix
df <- as.matrix(df) #convert to matrix df; columns as headings is part of the function
# convert to tbl_graph
df <- as_tbl_graph(df)

# plot network; nodes sized by degree; nodes too small
df %>% 
  mutate(degree = centrality_degree()) %>% 
  ggraph(layout = 'kk') + 
  geom_node_point(aes(size = degree))+
  geom_edge_link()

# tried multiplying degree by a value as below; changes the 
# value of the degrees and leaves node size unchanged.
df %>% 
  mutate(degree = centrality_degree()) %>% 
  ggraph(layout = 'kk') + 
  geom_node_point(aes(size = 2*degree))+
  geom_edge_link()

Created on 2020-07-18 by the reprex package (v0.3.0)

avgoustisw
  • 213
  • 1
  • 7

1 Answers1

3

I have found a solution having a look to this link: Network Visualizations in R

The key is to add scale_size_continuous in the pipeline for your plot. I have tried this option:

df %>% 
  mutate(degree = centrality_degree()) %>% 
  ggraph(layout = 'kk') + 
  geom_node_point(aes(size = degree)) +
  scale_size_continuous(range = c(2, 5)) +
  geom_edge_link()
carlo_sguera
  • 395
  • 2
  • 14