Advanced ggplot2 & plotly
R Programming & Data Analytics / Advanced ggplot2 & plotly

Advanced ggplot2 & plotly

Intermediate 10 hrs 3 Concepts
Your Learning Map
📌 You already know
You can build a basic ggplot with geoms and themes.
🎯 You'll learn here
Faceting (small multiples), interactive charts with plotly, and composing plots with patchwork.
🌍 Where it's used
Comparing many groups at once, and building the interactive visuals dashboards demand.
M1

Small Multiples

Concept 1

Faceting with facet_wrap and facet_grid

facet_wrap() wraps 1D panels; facet_grid() creates a 2D grid of panels. Both produce 'small multiples' — the most powerful EDA technique.

R
ggplot(df, aes(x=score)) + geom_histogram(binwidth=5) + facet_wrap(~subject, nrow=2)
ggplot(df, aes(x=score, y=grade)) + geom_point() + facet_grid(year~subject)
R — Means across two facets LIVE READY
Output below is verified. Click to run real R in your browser (first run loads ~20 MB once).
Output (verified)
  cyl gear    mpg
1   4    3 21.500
2   6    3 19.750
3   8    3 15.050
4   4    4 26.925
5   6    4 19.750
6   4    5 28.200
7   6    5 19.700
8   8    5 15.400
Solved Examples
Example 1 Apply the concept of Faceting with facet_wrap and facet_grid to a sample dataset. Show at least two approaches.

# See the code example above and adapt it to your data. # Always check your output with str() and head().

Self-Assessment (2 questions)
Q1. facet_wrap(~ category) creates:
Faceting splits the data into small-multiple panels, one per level of the faceting variable.
Q2. facet_grid() differs from facet_wrap() because it:
facet_grid(rows ~ cols) arranges panels in a 2-D grid defined by up to two variables.
M2

Interactive Charts

Concept 1

plotly Integration

ggplotly() converts any ggplot2 chart to an interactive Plotly chart in one line.

R
library(plotly)
p <- ggplot(mtcars, aes(x=wt, y=mpg, text=rownames(mtcars))) + geom_point()
ggplotly(p, tooltip=c('text','x','y'))
# Click, zoom, pan, hover — all built in
Solved Examples
Example 1 Apply the concept of plotly Integration to a sample dataset. Show at least two approaches.

# See the code example above and adapt it to your data. # Always check your output with str() and head().

Self-Assessment (2 questions)
Q1. plotly::ggplotly() is used to:
ggplotly() converts a static ggplot into an interactive, hoverable HTML chart.
Q2. An advantage of interactive plotly charts is:
plotly charts let users hover for values, zoom and pan - ideal for dashboards.
Concept 2

Combining Plots with patchwork

patchwork lets you combine multiple ggplot2 charts with + and / operators.

R
library(patchwork)
p1 + p2            # side by side
p1 / p2            # stacked
(p1 + p2) / p3     # top row + bottom
Solved Examples
Example 1 Apply the concept of Combining Plots with patchwork to a sample dataset. Show at least two approaches.

# See the code example above and adapt it to your data. # Always check your output with str() and head().

Self-Assessment (2 questions)
Q1. In patchwork, which operator places two ggplots side by side?
With patchwork, p1 | p2 puts plots side by side; p1 / p2 stacks them vertically.
Q2. patchwork is used to:
patchwork composes several ggplot objects into a single multi-panel figure.
Data Visualisation with ggplot2 Statistical Analysis in R