create transaction network diagram in r

To create a transaction network diagram in R using ggplot2, follow these steps:

  1. Install and load the required packages:
main.r
install.packages("ggplot2")
library(ggplot2)
45 chars
3 lines
  1. Create a data frame with at least two columns: a sender column and a receiver column. Each row represents a transaction from sender to receiver. In this example, the data frame is named transactions.
main.r
sender <- c("Alice", "Bob", "Charlie", "David")
receiver <- c("Bob", "Charlie", "David", "Alice")
amount <- c(100, 200, 50, 75)
transactions <- data.frame(sender, receiver, amount)
181 chars
5 lines
  1. Compute the total amount of transactions for each node. In this example, we use the dplyr package to group the data by sender and receiver and then sum the amount column. The result is stored in a new data frame named nodes.
main.r
library(dplyr)
nodes <- transactions %>%
  group_by(sender) %>%
  summarise(amount = sum(amount))

nodes <- rbind(nodes, transactions %>%
                group_by(receiver) %>%
                summarise(amount = sum(amount)))
nodes <- unique(nodes)
249 chars
10 lines
  1. Create a data frame with three columns: a sender column, a receiver column and a total amount column. The sender and receiver columns contain the same values as the transactions data frame, and the total amount column contains the total amount of transactions for each row. In this example, the data frame is named links.
main.r
links <- merge(transactions, nodes, by.x = "sender", by.y = "sender")
colnames(links) <- c("sender", "receiver", "amount_sent", "total_sent")
links <- merge(links, nodes, by.x = "receiver", by.y = "sender")
colnames(links) <- c("sender", "receiver", "amount_sent", "total_sent", "total_received")
links$total_amount <- links$total_sent + links$total_received
359 chars
6 lines
  1. Create the transaction network diagram using the ggplot2 package. The diagram uses nodes and edges to represent the transactions. The nodes represent the senders and receivers of the transactions, and their size is proportional to the total amount of transactions they are involved in. The edges represent the transactions themselves, and their width is proportional to the amount of the transaction. In this example, we use the geom_point() function to create the nodes and the geom_segment() function to create the edges.
main.r
ggplot(nodes, aes(x = sender, y = amount, size = amount)) +
  geom_point() +
  geom_segment(data = links, aes(x = sender, y = 0, xend = receiver, yend = amount_sent, size = total_amount), color = "black") +
  scale_size(range = c(5, 15)) +
  labs(title = "Transaction Network Diagram",
       x = "Node",
       y = "Number of Transactions",
       size = "Total Amount of Transactions",
       color = "Transaction Amount") +
  theme_bw()
440 chars
11 lines

The resulting diagram should look like this: Transaction Network Diagram

gistlibby LogSnag