Open In App

Create Diagrams (PDF) with DiagrammeR in R

Last Updated : 22 Apr, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

R Programming Language is an open-source language, that has extensive applications for statistical and data analysis. For proposing useful information and making essential decisions about any available data, data visualization presents itself as an effective graphical technique. Graphs, maps, and charts are considered major elements for data visualization and data analysis.

DiagrammeR Package

R has an extensive range of packages and libraries which are used for data visualization. DiagrammeR is a package in R language that is used for data visualization by creating multiple data elements such as graphs, flowcharts, network diagrams, and decision trees. Additionally, because of its flexible framework, DiagrammeR can facilitate the customization and modification of generated diagrams as well.

DiagrammeR package is capable of generating, modifying, analyzing, and visualizing the network graph diagrams. Network graph diagrams as output from the DiagrammeR can be extracted in the form of documents, integrated into the website applications, and exported into images as well as PDF formats. Easy creation and modification of network diagrams, visualization of it and later obtaining in multiple formats are the key features which makes DiagrammeR an effective package.

Installation of DiagrammeR in R

In order to install and load DiagrammeR, there are following steps:

  1. Open R studio
  2. Write the command of install.packages(“DiagrammeR”) and run.
  3. A message will be delivered in the console section of R studio about the installation of the package.
  4. Use the command of library(DiagrammeR) to load the package before the writing the code for utilizing it.

DiagrammeR can also be installed in an environment of R where it can be obtained from Comprehensive R Archive Network (CRAN). To install DiagrammeR package from CRAN, install.packages(“DiagrammeR”) command is used as discussed above.

R
install.packages(DiagrammeR)

library(DiagrammeR)

After the successful installation of the package, its library can be loaded by using library(DiagrammeR) command.

Create Diagrams with DiagrammeR

DiagrammeR is able to create the diagrams such as flow charts and network diagrams. Additionally, the package also provides the flexibility of incorporating different attributes such as colors and dimensions along with the flow charts and network graph diagrams. These are classified into nodes and edges which provides capability to create flow chart or network graph. In the end as a result, diagram can be saved into different formats such as pdf. Creation and export of graph is majorly comprised of three steps and as follow:

  1. Defining nodes, edges and relevant data attributes under the command of create_graph(). This command provides the flexibility of creating the diagrams such as flow charts and network graphs.
  2. Visualization of graph from the command of render_graph().
  3. Save the created diagram by using the command of export_graph().

Diagram is constituted of nodes and edges where nodes describe the data elements and edges represent the connections between the nodes. create_node_df() and create_edge_df() are the command of R which are used for the creation of nodes and edges respectively.

R
library(DiagrammeR)
nodes_GFG = create_node_df(n = 4, type = "empty")
edges_GFG = create_edge_df(from = c(1, 2), to = c(3, 4))

After defining nodes and edges of a diagram, create_graph() creates graph which can be visualized from render_gragh(). The generalized code for creating the graph is:

R
create_graph(
  nodes_df = NULL,
  edges_df = NULL,
  directed = TRUE,
  graph_name = NULL,
  attr_theme = "default",
  write_backups = FALSE,
  display_msgs = FALSE
)

An example for the creation of graph from DiagrammeR is:

R
library(DiagrammeR)
# Defining and creating Nodes and Edges for a Graph
nodes_GFG = create_node_df(n = 8, type = "empty")
edges_GFG = create_edge_df(from = c(1, 2, 3, 4), to = c(5, 6, 7, 8))
# Creating the Graph 
graph_GFG = create_graph(
  nodes_df = nodes_GFG, 
  edges_df = edges_GFG,
  directed = TRUE,
  graph_name = "Graphforgeeks",
)

# Visualization of the Graph
render_graph(graph_GFG)

Output:

gh

Create Diagrams (PDF) with DiagrammeR in R

Diagram as PDF

DiagrammeR has an ability to save the generated graphs and diagrams into different formats for data visualization. There are many formats such as PNG, PDF, SVG, and PostScript in which the created diagrams and graphs can be exported. Among them, diagrams and graphs can be exported in PDF format by using DiagrammeR. For this purpose, export_graph() is used. Diagrams from export_graph() as PDF can be exported. However, for this purpose packages of DiagrammeRsvg and rsvp must be installed.

R
install.packages("DiagrammeRsvg")
install.packages("rsvg")

General code for exporting the diagram in a specific format is:

R
export_graph(
  graph_GFG,
  file_name = NULL,
  file_type = NULL,
  title = NULL,
  width = NULL,
  height = NULL
)

It is important to note that the same command can be used in order to export the diagrams into other formats as well. In the continuation of previous example, the graph can be exported in PDF as:

R
export_graph(
  graph_GFG,
  file_name = "Graphforgeeks.pdf",
  title = "GFG",
  width = 8,
  height = 7
)

Output:

123

Create Diagrams (PDF) with DiagrammeR in R

You can see this saved file in console part of the R Environment. This command will save the diagram in the directed repository with name “Graphforgeeks” in PDF format. It is important to note that if file_type will be separately used and defined then file will be saved with “File” type which can be opened in PDF. However, introducing “Graph_Name.pdf” will save it in PDF format as represented in the code.

There are many types of diagrams which can be created from DiagrammeR. Here is another example which can provide a flowchart and network diagram with short R syntax for creation of graph as well as export as PDF.

R
library(DiagrammeR)
# Flowchart
nodes_flowchartgeeks = create_node_df(n = 3, type = "empty")
edges_flowchartgeeks = create_edge_df(from = c(1, 1), to = c(2, 3))
graph_flowchartgeeks = create_graph(nodes_df = nodes_flowchartgeeks, 
                                    edges_df = edges_flowchartgeeks)

# Rendering of flowchart and exporting as PDF
render_graph(graph_flowchartgeeks)
export_graph(graph_flowchartgeeks, "graph_GFG.pdf")

# Network diagram
nodes_networkgeeks = create_node_df(n = 5)
edges_networkgeeks = create_edge_df(from = c(1, 2, 2, 3, 4), to = c(2, 3, 4, 4, 5))
graph_networkgeeks = create_graph(nodes_df = nodes_networkgeeks, 
                                  edges_df = edges_networkgeeks)

# Rendering of graph and exporting as PDF
render_graph(graph_networkgeeks)
export_graph(graph_networkgeeks, "networkdiagram_GFG.pdf")

Output:

Rplot02

Create Diagrams (PDF) with DiagrammeR in R

Conclusion

DiagrammeR is a useful package which has been introduced in R for creating different type of graphs and diagrams. Additionally, this package helps in exporting the generated diagrams and graphs into different formats by providing simple commands in R. These diagrams can also be exported in PDF format into the defined directory of R studio.



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads