Geom - Boxplot - Examples - Plotly
Geom - Boxplot - Examples - Plotly
geom_boxplot in ggplot2
How to make a box plot in ggplot2. Examples of box plots in R that are grouped, colored, and display the
underlying data distribution.
R Python Javascript
New to Plotly?
Version Check
library(plotly)
packageVersion('plotly')
## [1] '4.5.6.9000'
Basic Boxplot
library(plotly)
https://plot.ly/ggplot2/box-plots/ 1/7
11/7/2019 geom_boxplot | Examples | Plotly
set.seed(1234)
dat <- data.frame(cond = factor(rep(c("A","B"), each=200)), rating = c(rnorm(200),rnorm
(200, mean=.8)))
p <- ggplotly(p)
Colored Boxplot
library(plotly)
set.seed(1234)
dat <- data.frame(cond = factor(rep(c("A","B"), each=200)), rating = c(rnorm(200),rnorm
(200, mean=.8)))
p <- ggplotly(p)
Flipped Boxplot
library(plotly)
set.seed(1234)
dat <- data.frame(cond = factor(rep(c("A","B"), each=200)), rating = c(rnorm(200),rnorm
https://plot.ly/ggplot2/box-plots/ 2/7
11/7/2019 geom_boxplot | Examples | Plotly
(200, mean=.8)))
p <- ggplotly(p)
Boxplot w/ Stats
library(plotly)
set.seed(1234)
dat <- data.frame(cond = factor(rep(c("A","B"), each=200)), rating = c(rnorm(200),rnorm
(200, mean=.8)))
p <- ggplotly(p)
Boxplot Facets
library(plyr)
library(reshape2)
library(plotly)
https://plot.ly/ggplot2/box-plots/ 3/7
11/7/2019 geom_boxplot | Examples | Plotly
set.seed(1234)
x<- rnorm(100)
y.1<-rnorm(100)
y.2<-rnorm(100)
y.3<-rnorm(100)
y.4<-rnorm(100)
df<- (as.data.frame(cbind(x,y.1,y.2,y.3,y.4)))
p <- ggplotly(p)
library(foreign)
library(MASS)
library(Hmisc)
library(reshape2)
library(plotly)
p <- ggplotly(p)
Outliers
library(plotly)
set.seed(123)
# Need to modify the plotly object and make outlier points have opacity equal to 0
p <- plotly_build(p)
https://plot.ly/ggplot2/box-plots/ 5/7
11/7/2019 geom_boxplot | Examples | Plotly
Linewidth
library(plotly)
set.seed(123)
# Need to modify the plotly object to make sure line width is larger than default
p <- plotly_build(p)
Whiskers
library(plotly)
set.seed(123)
https://plot.ly/ggplot2/box-plots/ 6/7
11/7/2019 geom_boxplot | Examples | Plotly
# Note that plotly will automatically add horozontal lines to the whiskers
p <- ggplot(df, aes(cut, price, fill = cut)) +
geom_boxplot()+
ggtitle("Add horizontal lines to whiskers using ggplot2")
p <- ggplotly(p)
https://plot.ly/ggplot2/box-plots/ 7/7