Designing Complex Relationships in R (feat. DiagrammeR)

Want to understand complex relationships at a glance? R's DiagrammeR package lets you design professional-looking relationship diagrams. In this article, you'll learn how to use R to create Legal dispute situationsas an example, let's take a closer look at the process of designing a relationship diagram like the one below.

관계도 디자인 결과 그림
(The result of designing a relationship diagram in R)

With this guide, you'll experience the power of R programming and the flexibility of DiagrammeR. With step-by-step explanations and real-world code examples, you'll discover new dimensions of data visualization. Learn how to clearly represent complex relationships, code optimization tips, and the secrets to creating professional-looking graphs.

Organizing complex relationships in legal cases

First, let's take a look at what situation we're trying to design a relationship diagram for, which you can read more about here.

  • Plaintiff (KEPCO): The party who rented an apartment from DefendantSecondary Participant (the landlord) and is suing for the return of their security deposit.
  • Defendant (Seoul Guaranty Insurance): A party that entered into a rent guarantee credit insurance contract with the plaintiff, but failed to pay the insurance claim when the plaintiff failed to return the rent deposit.
  • Defendant Secondary Participant (Landlord): The party that leased the apartment to the plaintiff and subsequently sold the apartment to Alienate 1.
  • Alienator 1 (Buyer): The party who bought the apartment from the defendant-subparticipant and took over as landlord.

Getting Started with DiagrammeR for Designing Relationship Diagrams

Install and load the DiagrammeR package

The first step to drawing a relationship diagram in R is to install and load the DiagrammeR package. Let's get started by running the following code

Install the # DiagrammeR package.
install.packages("DiagrammeR")
Load the # package.
library(DiagrammeR)

Setting up a relationship chart structure

Define the basic graph structure

DiagrammeR uses the DOT language to define graph structure. Let's start with the following code, which sets the default graph structure in the top-to-bottom (TB) direction.

grViz("
  digraph hierarchy {

    graph [rankdir = TB]

    node [shape = rectangle]
    # We're going to add nodes and edges here.
  }
")

Defining a node

Now let's add nodes to the graph, each representing an element of the relationship diagram.

Add each element of the # relationship diagram
Korean_Electric_Power_Corporation [label = 'Plaintiff (Tenant)\nKEPCO']
Seoul_Guarantee_Insurance [label = 'Defendant\nSeoul_Guarantee_Insurance']
Landlord [label = 'Defendant Secondary Participant\nLandlord']
Buyer [label = 'Alienation 1\nBuyer']
Lease_Contract [label = 'Lease Agreement\nNonreturn of Security Deposit']
Lawsuit [label = 'Lawsuit filed against defendant']
Contract_Succession [label = 'Alien 1 to Alienate 1\nSuccession of Lease']
Insurance_Contract [label = 'Contract for surety insurance']
Insurance_Denial [label = 'Refusal to pay insurance claim']
Sale_Contract [label = 'Sale contract']

Define an edge (connecting line)

Add edges to represent relationships between nodes.

#Setting relationships between nodes
Korean_Electric_Power_Corporation -> Seoul_Guarantee_Insurance [label = 'Lease_Contract']
Korean_Electric_Power_Corporation -> Lease_Contract
Seoul_Guarantee_Insurance -> Insurance_Denial
Korean_Electric_Power_Corporation -> Lawsuit
Landlord -> Sale_Contract
Buyer -> Contract_Succession

Lease_Contract -> Lawsuit
Lawsuit -> Seoul_Guarantee_Insurance
Sale_Contract -> Buyer
Contract_Succession -> Landlord

Optimize your layout

Add invisible edges to improve the layout of the graph.

#Adjusting placement with invisible edges
edge [style = invis]
Lease_Contract -> Insurance_Contract -> Insurance_Denial -> Lawsuit -> Contract_Succession -> Sale_Contract

Final code and results

Here's the finished code that puts all the pieces together. When you run this code, it generates a relationship diagram that clearly shows the complex legal dispute relationships.

grViz("
  digraph hierarchy {

    graph [rankdir = TB]

    node [shape = rectangle]
    Korean_Electric_Power_Corporation [label = 'Plaintiff (Lessee)\nKorea Electric Power Corporation']
    Seoul_Guarantee_Insurance [label = 'Defendant\nSeoul_Guarantee_Insurance']
    Landlord [label = 'Defendant Secondary Participant\nLandlord']
    Buyer [label = 'Alienation 1\nBuyer']
    Lease_Contract [label = 'Lease Agreement\nNonreturn of Security Deposit']
    Lawsuit [label = 'Lawsuit filed against defendant']
    Contract_Succession [label = 'Alien 1 to Alienate 1\nSuccession of Lease']
    Insurance_Contract [label = 'Contract for surety insurance']
    Insurance_Denial [label = 'Refusal to pay insurance claim']
    Sale_Contract [label = 'Sale contract']

    Korean_Electric_Power_Corporation -> Seoul_Guarantee_Insurance [label = 'Charterparty Guarantee\nCredit Insurance Contract']
    Korean_Electric_Power_Corporation -> Lease_Contract
    Seoul_Guarantee_Insurance -> Insurance_Denial
    Korean_Electric_Power_Corporation -> Lawsuit
    Landlord -> Sale_Contract
    Buyer -> Contract_Succession

    Lease_Contract -> Lawsuit
    Lawsuit -> Seoul_Guarantee_Insurance
    Sale_Contract -> Buyer
    Contract_Succession -> Landlord

    edge [style = invis]
    Lease_Contract -> Insurance_Contract -> Insurance_Denial -> Lawsuit -> Contract_Succession -> Sale_Contract

  }
")

Tips for using DiagrammeR

Customizing your code

  • Change the color, font, and style of nodes and edges.
  • You can apply conditional styling to emphasize certain relationships.

Explore advanced features

  • Subgraphs can be used to simplify complex structures.
  • Utilize dynamic data to create graphs that update automatically.

We'll write a separate post on how to utilize them.

Organize

I used R and the DiagrammeR package to create complex relationship diagrams easily and effectively. This guide took me from basic graph creation to layout optimization.

Now it's time to start creating your own unique relationship diagrams. What projects would you like to try DiagrammeR for? For more information, check out the Graph/Network Visualization - DiagrammeRfor more information.

#Full source code
Installing R, RStudio - Windows post to get a taste of the R world. Then, paste the code below to see the source code in action.

Load the # DiagrammeR package.
library(DiagrammeR)

# Generate a graph using the grViz function.
# This function takes a graph specification written in DOT language and visualizes it.
grViz("
  # digraph means a graph with a directed edge.
  # hierarchy is the name of the graph.
  digraph hierarchy {
  
    # Set the graph properties.
    # rankdir = TB sets the orientation of the graph from top to bottom.
    graph [rankdir = TB]
  
    # sets the default shape of the node to rectangle.
    node [shape = rectangle]
    
    # Define a label for each node.
    # [label = '...'] specifies the text to be displayed on the node.
    # \n means newline.
    Korean_Electric_Power_Corporation [label = 'Plaintiff (Lessee)\nKorea Electric Power Corporation']
    Seoul_Guarantee_Insurance [label = 'Defendant\n서울보증보험']
    Landlord [label = 'Defendant Secondary Participant\nLandlord']
    Buyer [label = 'Alienation 1\nBuyer']
    Lease_Contract [label = 'Lease Agreement\nNonreturn of Security Deposit']
    Lawsuit [label = 'Lawsuit filed against defendant']
    Contract_Succession [label = 'Alien 1 to Alienate 1\nSuccession of Lease']
    Insurance_Contract [label = 'Contract for surety insurance']
    Insurance_Denial [label = 'Refusal to pay insurance claim']
    Sale_Contract [label = 'Sale contract']
    
    Define relationships (edges) between # nodes.
    # A -> B draws an arrow from A to B.
    # [label = '...'] specifies the text to be displayed on the arrow.
    Korean_Electric_Power_Corporation -> Seoul_Guarantee_Insurance [label = 'Charterparty Guarantee\nCredit Insurance Contract']
    Korean_Electric_Power_Corporation -> Lease_Contract
    Seoul_Guarantee_Insurance -> Insurance_Denial
    Korean_Electric_Power_Corporation -> Lawsuit
    Landlord -> Sale_Contract
    Buyer -> Contract_Succession
    
    Lease_Contract -> Lawsuit
    Lawsuit -> Seoul_Guarantee_Insurance
    Sale_Contract -> Buyer
    Contract_Succession -> Landlord
    
    # Add an invisible edge to adjust the layout.
    # style = invis makes the edge invisible.
    edge [style = invis]
    Lease_Contract -> Insurance_Contract -> Insurance_Denial -> Lawsuit -> Contract_Succession -> Sale_Contract
    
  }
")

Similar Posts