Visualizing Data in R with ggplot2:
Patchwork and Plotly Package

Somsak Chanaim

International College of Digital Innovation, CMU

February 27, 2026

What is the patchwork package?

The patchwork package is the one extension of ggplot2 package.

The goal of patchwork package is to make it ridiculously simple to combine separate ggplots into the same graphic.

How to in install

How to use their package

Interactive

Choose label:

Tags:

The ggplot Graph Example

From mtcars data set

Adding plots to the patchwork

We can use \(+\) to add plots together in the same row (up to three plots).

Stacking and packing

We can use \(/\) to add plots together in the same column.

Mixed plot with multi rows and columns

We can mix the plots with multiple rows and columns.

Or

For up to 4 plots or more

We can control by using this command

Nesting the left-hand side

As plots will always be added to the patchwork on the left-hand side, it is not possible to nest the left-hand side beside the right-hand side with the standard operators shown above.

Nesting the right-hand side

As plots will always be added to the patchwork on the right-hand side, it is not possible to nest the left-hand side beside the right-hand side with the standard operators shown above.

Modifying theme

Often, especially when it comes to theming, you want to modify everything at once. patchwork provides two additional operators that facilitates this.

  • & will add the element to all subplots in the patchwork.

  • * will add the element to all the subplots in the current nesting level.

Adding Annotation and Style

Titles, subtitles and captions

One of the most needed things is to add descriptive text to your plot ensemble. To achieve this, you simply add it to your patchwork using plot_annotation()

Interactive Plotly

Input

Plotly in R

  • Plotly is a high-level, interactive graphing library.

  • It creates modern, sleek, and high-fidelity interactive charts directly from R.

  • It supports a wide range of chart types, including 3D plots, maps, and statistical charts.

  • Key benefit: Users can zoom, pan, and hover over data points for more detail.

The Power of ggplotly()

  • The ggplotly() function acts as a bridge between ggplot2 and Plotly.

  • It converts a static ggplot2 object into an interactive HTML-based widget.

  • Workflow:

    1. Create a standard plot using ggplot2 syntax.

    2. Pass the plot object into ggplotly().

Simple ggplotly Example

Multiple Plots: The subplot() Function

  • To display multiple interactive plots together, Plotly uses the subplot() function.

  • This is the Plotly equivalent to facet_wrap() or patchwork for static plots.

Key Features:

  • Syncing: You can link axes so that zooming in one plot zooms the other.

  • Flexibility: Define rows, columns, and relative widths/heights.

Multiple Plot Layout Example

Why Use Advanced Layouts?

  • Standard plots often fail to show the full story of complex datasets.
  • Advanced layouts allow for side-by-side comparisons of different variables.
  • You can combine different chart types (e.g., a Scatter plot and a Boxplot) into one unified view.
  • Interactive layouts provide a professional Dashboard-like experience for users.

Organizing Plots with subplot()

  • The subplot() function is used to arrange multiple plotly objects in a grid.
  • Key Arguments:
    • nrows: Defines how many rows to use.
    • widths: A vector of relative widths for each column.
    • shareX / shareY: Links the axes so zooming is synchronized across plots.

Example: Synchronized Subplots

Precision Control with layout()

  • While ggplot2 handles the data, layout() handles the environment.
  • It is used to modify non-data attributes of the plotly object.
  • Common uses include:
  • Setting Title position and font styles.
  • Managing Legend orientation (e.g., moving it to the bottom).
  • Configuring Hovermode (e.g., comparing points on a vertical line).

Example: Custom Legend & Titles

Best Practices for Interactivity

  • Don’t Overcrowd: Too many interactive plots in one layout can slow down the browser.
  • Clear Labels: Use the layout() function to ensure all subplots have clear titles.
  • Consistent Themes: Ensure all plots in a subplot() use the same ggplot2 theme for a cohesive look.

Summary: Plotly vs. ggplot2

Feature ggplot2 (Static) Plotly (Interactive)
Logic Grammar of Graphics Web-based Widgets
Interactivity None (Static Image) Tooltips, Zoom, Pan
Publication Best for PDF/Print Best for HTML/Dashboards
Layouts facet_wrap, patchwork subplot(), layout()