🏥 🏭 Working on the COVID-19 response in Canada? Plotly & the Canadian government can help you and your organization. Learn more and get in touch.

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 is a free and open-source graphing library for R. We recommend you read our Getting Started guide for the latest installation or upgrade instructions, then move on to our Plotly Fundamentals tutorials or dive straight in to some Basic Charts tutorials.

Basic Boxplot

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

fig

Horizontal Boxplot

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

fig

Adding Jittered Points

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

fig

Several Box Plots

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

fig

Grouped Box Plots

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

fig