# 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


### Choosing The Algorithm For Computing Quartiles

By default, quartiles for box plots are computed using the linear method (for more about linear interpolation, see #10 listed on http://www.amstat.org/publications/jse/v14n3/langford.html and https://en.wikipedia.org/wiki/Quartile for more details).

However, you can also choose to use an exclusive or an inclusive algorithm to compute quartiles.

The exclusive algorithm uses the median to divide the ordered dataset into two halves. If the sample is odd, it does not include the median in either half. Q1 is then the median of the lower half and Q3 is the median of the upper half.

The inclusive algorithm also uses the median to divide the ordered dataset into two halves, but if the sample is odd, it includes the median in both halves. Q1 is then the median of the lower half and Q3 the median of the upper half.

library(plotly)
fig <- plot_ly(y = list(1,2,3,4,5), type = "box", quartilemethod="exclusive") # or "inclusive", or "linear" by default

fig


### Modifying The Algorithm For Computing Quartiles

For an explanation of how each algorithm works, see Choosing The Algorithm For Computing Quartiles

library(plotly)
fig <- plot_ly(y = list(1,2,3,4,5), type = "box", quartilemethod="linear", name="Linear Quartile Mode")
fig <- fig %>% add_trace(y = list(1,2,3,4,5), quartilemethod="inclusive", name="Inclusive Quartile Mode")
fig <- fig %>% add_trace(y = list(1,2,3,4,5), quartilemethod="exclusive", name="Exclusive Quartile Mode")
fig <- fig %>% layout(title = "Modifying The Algorithm For Computing Quartiles")

fig


### Box Plot With Precomputed Quartiles

You can specify precomputed quartile attributes rather than using a built-in quartile computation algorithm.

This could be useful if you have already pre-computed those values or if you need to use a different algorithm than the ones provided.

library(plotly)
fig <- plot_ly(y = list(1,2,3,4,5,6,7,8,9), type = "box", q1=list(1, 2, 3), median=list(4, 5, 6),
q3=list(7, 8, 9 ), lowerfence=list(-1, 0, 1),
upperfence=list(5, 6, 7), mean=list(2.2, 2.8, 3.2 ),
sd=list(0.2, 0.4, 0.6), notchspan=list(0.2, 0.4, 0.6))

fig


### Horizontal Boxplot

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

fig


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