Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Plot labels for specific geom_node_text

I am trying to plot a network graph and show only the labels for the geom_node_text that have a centrality score above a specific threshold. My code is below:

rt_tbl %>% 
mutate(centrality = centrality_authority()) %>% 
ggraph(layout = 'kk') + 
geom_edge_link(aes(), colour = "gray", alpha = 0.5) + 
geom_node_point(aes(size = centrality, colour = centrality)) + 
geom_node_text(data=subset(centrality > 0.6), aes(centrality, label=name)) + 
theme_graph(base_family = "Roboto Condensed", base_size = 13)

I encounter the following error:

Error in subset(centrality > 100) : object 'centrality' not found

My data looks like this:

# A tbl_graph: 1589 nodes and 3992 edges
# A directed multigraph with 3 components
# Node Data: 1,589 x 3 (active)
        name degree centrality
        <chr>  <dbl>      <dbl>
1   Ashinlay1970     35 0.90053429
2     WinTunMin1     25 0.66408597
3 Yaminayeyaohn1      2 0.06080755
4  TUNOO00655880      3 0.07609831
5     inewagency      8 0.21569006
6         KSwe03      4 0.12416238
# ... with 1,583 more rows

# Edge Data: 3,992 x 2
from    to
<int> <int>
1     1    48
2     1    49
3     1     1
# ... with 3,989 more rows
like image 989
mundos Avatar asked Dec 02 '22 12:12

mundos


1 Answers

This is not an answer but a comment for any future visitor using the answer above. When I tried levels(name) in different networks it gave erroneous results. tidygraph has filter property which can be used in various geoms for filtering nodes, edges, etc.

So, the geom_node_text line of the answer above should work better if written as:

geom_node_text(aes(filter=centrality > .6, label = name))
like image 83
Alper Yilmaz Avatar answered Dec 19 '22 10:12

Alper Yilmaz