Join new node attribute values in a left join using a data frame. The use of a left join in this function allows for no possibility that nodes in the graph might be removed after the join.
join_node_attrs(graph, df, by_graph = NULL, by_df = NULL)A graph object of class dgr_graph.
The data frame to use for joining.
Optional specification of the column in the graph's internal
node data frame for the left join. If both by_graph and by_df are not
provided, then a natural join will occur if there are columns in the
graph's ndf and in df with identical names.
Optional specification of the column in df for the left join.
If both by_graph and by_df are not provided, then a natural join will
occur if there are columns in the graph's ndf and in df with identical
names.
A graph object of class dgr_graph.
Other node creation and removal:
add_n_node_clones(),
add_n_nodes(),
add_n_nodes_ws(),
add_node(),
add_node_clones_ws(),
add_node_df(),
add_nodes_from_df_cols(),
add_nodes_from_table(),
colorize_node_attrs(),
copy_node_attrs(),
create_node_df(),
delete_node(),
delete_nodes_ws(),
drop_node_attrs(),
layout_nodes_w_string(),
mutate_node_attrs(),
mutate_node_attrs_ws(),
node_data(),
recode_node_attrs(),
rename_node_attrs(),
rescale_node_attrs(),
set_node_attr_to_display(),
set_node_attr_w_fcn(),
set_node_attrs(),
set_node_attrs_ws(),
set_node_position()
# Set a seed
suppressWarnings(RNGversion("3.5.0"))
set.seed(23)
# Create a simple graph
graph <-
create_graph() %>%
add_n_nodes(n = 5) %>%
add_edges_w_string(
edges = "1->2 1->3 2->4 2->5 3->5")
# Create a data frame with node ID values and a
# set of numeric values
df <-
data.frame(
values = round(rnorm(6, 5), 2),
id = 1:6)
# Join the values in the data frame to the
# graph's nodes; this works as a left join using
# identically-named columns in the graph and the df
# (in this case the `id` column is common to both)
graph <-
graph %>%
join_node_attrs(
df = df)
# Get the graph's internal ndf to show that the
# join has been made
graph %>% get_node_df()
#> id type label values
#> 1 1 <NA> <NA> 6.00
#> 2 2 <NA> <NA> 6.11
#> 3 3 <NA> <NA> 4.72
#> 4 4 <NA> <NA> 6.02
#> 5 5 <NA> <NA> 5.05
# Get betweenness values for each node and
# add them as a node attribute (Note the
# common column name `id` in the different
# tables results in a natural join)
graph <-
graph %>%
join_node_attrs(
df = get_betweenness(.))
# Get the graph's internal ndf to show that
# this join has been made
graph %>% get_node_df()
#> id type label values betweenness
#> 1 1 <NA> <NA> 6.00 0.0
#> 2 2 <NA> <NA> 6.11 1.5
#> 3 3 <NA> <NA> 4.72 0.5
#> 4 4 <NA> <NA> 6.02 0.0
#> 5 5 <NA> <NA> 5.05 0.0