Show Sidebar Hide Sidebar

Box Plots in R

How to make an interactive box plot in R. Examples of box plots in R that are grouped, colored, and display the underlying data distribution.

New to Plotly?

Plotly's R library is free and open source!
Get started by downloading the client and reading the primer.
You can set up Plotly to work in online or offline mode.
We also have a quick-reference cheatsheet (new!) to help you get started!

Version Check

Version 4 of Plotly's R package is now available!
Check out this post for more information on breaking changes and new features available in this version.

library(plotly)
packageVersion('plotly')
## [1] '4.9.1'

Basic Boxplot

library(plotly)
p <- plot_ly(y = ~rnorm(50), type = "box") %>%
  add_trace(y = ~rnorm(50, 1))

p

Horizontal Boxplot

library(plotly)
p <- plot_ly(x = ~rnorm(50), type = "box") %>%
  add_trace(x = ~rnorm(50, 1))

p

Adding Jittered Points

p <- plot_ly(y = ~rnorm(50), type = "box", boxpoints = "all", jitter = 0.3,
        pointpos = -1.8)

p

Several Box Plots

p <- plot_ly(ggplot2::diamonds, y = ~price, color = ~cut, type = "box")

p

Grouped Box Plots

p <- plot_ly(ggplot2::diamonds, x = ~cut, y = ~price, color = ~clarity, type = "box") %>%
  layout(boxmode = "group")

p