![]() ![]() social_net_tbls <- tbl_graph(nodes = nodes, edges = links, directed = FALSE) Here, we convert our separate tables into a single object called a tbl_graph. There is just one final step that we need to take before we can pass our tables to a function that creates the list.īefore we can pass these tables into our plotting function, we just need to ensure that they are in the correct format for the ggraph package to interpret. With our nodes and links tables prepared, we are almost ready to start plotting the network. In this example, we arbitrarily create twice as many links as there are nodes before removing redundant links. With regards to how many links we should draw, this will depends on how connected you would like your network to be. Links % filter(!(src = target)) links <- unique(links) In R: source <- sample(1:nrow(nodes), nrow(nodes)*2, replace = TRUE) target <- sample(1:nrow(nodes), nrow(nodes)*2, replace = TRUE) We repeat the same process for our target column. This time, we sample with replacement since each node can have multiple links attached to it. Starting with the source column, we get a random sample of numbers between one and nine (since there are only nine nodes). To create our links table, we will consider the source and target columns separately to begin with. Here’s how we might accomplish this in R: get_random_names <- function(n) nodes <- get_random_names(9) Creating the links table Return those rows - this will be our nodes table.From our filtered table, pull out the rows indexed by the random numbers that we selected.Select a random sample of n numbers between one and the number of rows in our filtered table (without replacement - meaning that once a number has been drawn, it can not be drawn at again).Filter the table to remove any duplicate names.Here’s an outline of the steps that we can take: To get a list of random names that are unique, it would be best to write a small function that we can reuse to generate a nodes list of size n (we may want to draw a larger network in future). This tells us that our solution will need to include something that ensures we don’t have duplicates in the final draw. While the chances of drawing the same name twice are quite low, it is still a possibility. A quick check shows us that there are 1,924,665 rows in the baby names table. We want to ensure that the names we draw at random are unique. This structure suggests that names are repeated each year with different values for prop (proportion) and n (number). Here we can see that we have a list of names, sex, and a few other variables that may not apply directly to our use case here. We use this to get a look at how the data is structured: This returns the top 6 rows of the baby names table. We start by taking a look at the structure of that data like so: head(babynames) We want to get nine random names from the baby names data that we pulled into our environment. Library(tidyverse) library(ggraph) library(tidygraph) library(babynames) Creating the nodes table We will use this to select the random names for the nodes in our network. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |