International College of Digital Innovation, CMU
February 27, 2026
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
From mtcars data set
We can use \(+\) to add plots together in the same row (up to three plots).
We can use \(/\) to add plots together in the same column.
We can mix the plots with multiple rows and columns.
Or
We can control by using this command
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.
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.
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.
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()
Input
viewof chartType = Inputs.radio(
["Scatter", "Bar", "Box", "Line", "Heatmap"],
{ label: "Chart Type", value: "Scatter", inline: true }
)
// เลือกชุดข้อมูล (ไม่มี diamonds, ไม่มี native plotly)
viewof datasetName = Inputs.select(
["mtcars", "iris", "mpg", "economics"],
{ label: "Dataset", value: "mtcars" }
)
// เลือกการทำ color mapping
viewof colorAes = Inputs.radio(
["None", "Category", "Numeric"],
{ label: "Color mapping", value: "Category", inline: true }
)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.
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:
Create a standard plot using ggplot2 syntax.
Pass the plot object into ggplotly().
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.
subplot() function is used to arrange multiple plotly objects in a grid.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.ggplot2 handles the data, layout() handles the environment.layout() function to ensure all subplots have clear titles.subplot() use the same ggplot2 theme for a cohesive look.| 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() |