Advanced ggplot2 & plotly
Small Multiples
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.
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)
# See the code example above and adapt it to your data. # Always check your output with str() and head().
Interactive Charts
plotly Integration
ggplotly() converts any ggplot2 chart to an interactive Plotly chart in one line.
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
# See the code example above and adapt it to your data. # Always check your output with str() and head().
Combining Plots with patchwork
patchwork lets you combine multiple ggplot2 charts with + and / operators.
library(patchwork)
p1 + p2 # side by side
p1 / p2 # stacked
(p1 + p2) / p3 # top row + bottom
# See the code example above and adapt it to your data. # Always check your output with str() and head().