Written and maintained by CASRAI Editorial Board
Published · Last updated
On this page
Short answer: build the box plot with ggplot2 (geom_boxplot()), store it in an object, and pass that object to ggplotly() from the plotly package. The result is an interactive plotly.js figure. For a grouped box plot, map a second factor to fill or colour; ggplot2 dodges the boxes automatically. This guide is for R users who already have a static box plot and need an interactive version for a report, Shiny app or HTML document.
What it is used for in research
A box plot summarises a distribution by its quartiles and flags unusual values, so it is a standard first look at group differences before a formal test. Plotly describes ggplotly() as converting ggplot2 figures into interactive ones powered by plotly.js, and the tool is free and open source (plotly.com/ggplot2, read 2026-11-08). Interactivity is useful in HTML outputs where readers want to inspect individual points or groups; in a printed paper the static ggplot2 figure is usually what is submitted. For the static basics see box plots in R, and for the general plotly workflow see plotly in R.
Install or access
Both packages are distributed through CRAN, R’s package repository. In R or RStudio run:
install.packages(c("ggplot2", "plotly"))
library(ggplot2)
library(plotly)
Plotly’s ggplot2 documentation is at plotly.com/ggplot2 and the box-plot reference for ggplot2 is at ggplot2.tidyverse.org/reference/geom_boxplot.html.
Step-by-step: from geom_boxplot to ggplotly
1. A basic interactive box plot
This follows the example on Plotly’s ggplot2 box-plot page (read 2026-11-08):
- Create two groups of simulated ratings:
set.seed(1234) dat <- data.frame( cond = factor(rep(c("A", "B"), each = 200)), rating = c(rnorm(200), rnorm(200, mean = .8)) ) - Build the static plot and store it in
p:p <- ggplot(dat, aes(x = cond, y = rating)) + geom_boxplot() - Convert it:
ggplotly(p)
To colour each box, add fill = cond to aes(), as the same Plotly page does.
2. Grouped box plot (also searched as “grouped boxplot” or “grouped box plot”)
A grouped box plot places several boxes side by side within each category of the x axis, one per level of a second factor. The ggplot2 reference states that boxplots are automatically dodged when any aesthetic is a factor, and shows the pattern geom_boxplot(aes(colour = drv)) on the built-in mpg data; the default position is "dodge2" (ggplot2 reference, read 2026-11-08). Applying that pattern and then converting:
p2 <- ggplot(mpg, aes(x = class, y = hwy, fill = drv)) +
geom_boxplot()
ggplotly(p2)
Compare the converted widget with the static plot before publishing. If the dodged result does not look right, the plotly-native route below is the documented alternative.
3. The plotly-native route for grouped boxes
Plotly’s R box-plot page builds grouped boxes without ggplot2 by mapping a variable to color and setting the layout’s box mode (read 2026-11-08):
fig <- plot_ly(ggplot2::diamonds, x = ~cut, y = ~price,
color = ~clarity, type = "box")
fig <- fig %>% layout(boxmode = "group")
fig
The same page notes that quartiles are computed with the “linear” method by default and that quartilemethod can be set to “exclusive” or “inclusive” in plot_ly() or add_trace() (source: plotly.com/r/box-plots). That setting is a reason to choose this route when you need to state exactly how quartiles were computed.
4. Facets instead of one crowded panel
Plotly’s ggplot2 page also shows a faceted box plot (fill = variable with facet_grid(.~variable)). Faceting is the usual alternative when too many grouped boxes make a single panel unreadable.
“Stacked box plot”
Box plots are not stacked the way bar charts are, because quartile boxes from different groups do not add up to a total. People searching for a “stacked box plot” usually want either boxes side by side within each category (the grouped form above) or one panel per group (facets). That is an editorial reading of the search term, not a named chart type in ggplot2 or plotly.
Output and how to read it
ggplot2 defines the box as running from the 25th to the 75th percentile, with the median inside. The upper whisker extends from the upper hinge to the largest value no further than 1.5 times the interquartile range from the hinge, the lower whisker likewise, and data beyond the whiskers are plotted individually as outliers (ggplot2 reference, read 2026-11-08). In the interactive figure you can hover to inspect values and click legend entries to hide groups. Related reading: boxplot and IQR, outliers in R and box plot skewness.
When boxes sit on a continuous x variable, ggplot2 needs a grouping aesthetic, for example geom_boxplot(aes(group = cut_width(carat, 0.25))) as in the reference.
Troubleshooting
- One box instead of many: the x variable is continuous. Convert it to a factor or add a
groupaesthetic, as above. - Boxes not side by side: confirm a factor is mapped to
fillorcolour; the ggplot2 reference says dodging occurs when any aesthetic is a factor. - Converted plot looks different from the static one: try the plotly-native route in step 3, which sets
boxmode = "group"explicitly. - Different quartile values between tools: quartile methods differ; plotly lets you set
quartilemethod(see step 3).
Free alternatives
| Tool | Route | Notes |
|---|---|---|
| plotly for R (native) | plot_ly(type = "box") with layout(boxmode = "group") |
Plotly states the library is free and open source; quartile method selectable. |
| Static ggplot2 | geom_boxplot() without ggplotly() |
Best for print figures; see ggplots in R. |
| Seaborn (Python) | See seaborn boxplot | Python route. |
| Matplotlib (Python) | See box plot in Matplotlib | Python route. |
FAQ
How do I make a ggplot2 box plot interactive?
Save the ggplot2 object (for example p) and call ggplotly(p) after loading the plotly package.
How do I make a grouped box plot with ggplotly?
Map a second factor to fill or colour inside aes() in geom_boxplot(); ggplot2 dodges the boxes automatically, then wrap the plot in ggplotly(). Or use plot_ly() with color and layout(boxmode = "group").
Is there a stacked box plot?
Not as a standard chart type. Use grouped (dodged) boxes or facets.
Is ggplotly free?
Plotly states that converting ggplot2 figures with ggplotly() is free and open source.







