0% found this document useful (0 votes)
59 views

Geom - Boxplot - Examples - Plotly

This document provides examples of how to create different types of box plots in R using the ggplot2 and plotly packages. It shows how to make basic, colored, flipped, and grouped box plots as well as box plots with statistics overlaid, outliers ignored, adjusted line widths, and horizontal whisker lines. The examples demonstrate how to customize box plots and convert ggplot graphs to interactive web graphs using Plotly.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
59 views

Geom - Boxplot - Examples - Plotly

This document provides examples of how to create different types of box plots in R using the ggplot2 and plotly packages. It shows how to make basic, colored, flipped, and grouped box plots as well as box plots with statistics overlaid, outliers ignored, adjusted line widths, and horizontal whisker lines. The examples demonstrate how to customize box plots and convert ggplot graphs to interactive web graphs using Plotly.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

11/7/2019 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?

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.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 <- ggplot(dat, aes(x=cond, y=rating)) + geom_boxplot()

p <- ggplotly(p)

# Create a shareable link to your chart


# Set up API credentials: https://plot.ly/r/getting-started
chart_link = plotly_POST(p, filename="geom_boxplot/basic")
chart_link

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 <- ggplot(dat, aes(x=cond, y=rating, fill=cond)) + geom_boxplot()

p <- ggplotly(p)

# Create a shareable link to your chart


# Set up API credentials: https://plot.ly/r/getting-started
chart_link = plotly_POST(p, filename="geom_boxplot/colored")
chart_link

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 <- ggplot(dat, aes(x=cond, y=rating, fill=cond)) + geom_boxplot() +


guides(fill=FALSE) + coord_flip()

p <- ggplotly(p)

# Create a shareable link to your chart


# Set up API credentials: https://plot.ly/r/getting-started
chart_link = plotly_POST(p, filename="geom_boxplot/flipped")
chart_link

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 <- ggplot(dat, aes(x=cond, y=rating)) + geom_boxplot() +


stat_summary(fun.y=mean, geom="point", shape=5, size=4)

p <- ggplotly(p)

# Create a shareable link to your chart


# Set up API credentials: https://plot.ly/r/getting-started
chart_link = plotly_POST(p, filename="geom_boxplot/stats")
chart_link

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)))

dfmelt<-melt(df, measure.vars = 2:5)

p <- ggplot(dfmelt, aes(x=factor(round_any(x,0.5)), y=value,fill=variable))+


geom_boxplot()+
facet_grid(.~variable)+
labs(x="X (binned)")+
theme(axis.text.x=element_text(angle=-90, vjust=0.4,hjust=1))

p <- ggplotly(p)

# Create a shareable link to your chart


# Set up API credentials: https://plot.ly/r/getting-started
chart_link = plotly_POST(p, filename="geom_boxplot/facets")
chart_link

Time Series Facets

library(foreign)
library(MASS)
library(Hmisc)

## Error in library(Hmisc): there is no package called 'Hmisc'

library(reshape2)
library(plotly)

dat <- read.dta("http://www.ats.ucla.edu/stat/data/ologit.dta")


https://plot.ly/ggplot2/box-plots/ 4/7
11/7/2019 geom_boxplot | Examples | Plotly

lapply(dat[, c("apply", "pared", "public")], table)


ftable(xtabs(~ public + apply + pared, data = dat))

p <- ggplot(dat, aes(x = apply, y = gpa)) +


geom_boxplot(size = .75) +
facet_grid(pared ~ public, margins = TRUE)

p <- ggplotly(p)

# Create a shareable link to your chart


# Set up API credentials: https://plot.ly/r/getting-started
chart_link = plotly_POST(p, filename="geom_boxplot/time-series")
chart_link

Outliers

library(plotly)
set.seed(123)

df <- diamonds[sample(1:nrow(diamonds), size = 1000),]

p <- ggplot(df, aes(cut, price, fill = cut)) +


geom_boxplot(outlier.shape = NA) +
ggtitle("Ignore outliers in ggplot2")

# Need to modify the plotly object and make outlier points have opacity equal to 0
p <- plotly_build(p)

p$data <- lapply(p$data, FUN = function(x){


x$marker = list(opacity = 0)
return(x)
})

# Create a shareable link to your chart


# Set up API credentials: https://plot.ly/r/getting-started
chart_link = plotly_POST(p, filename="geom_boxplot/outliers")
chart_link

https://plot.ly/ggplot2/box-plots/ 5/7
11/7/2019 geom_boxplot | Examples | Plotly

Linewidth

library(plotly)
set.seed(123)

df <- diamonds[sample(1:nrow(diamonds), size = 1000),]

p <- ggplot(df, aes(cut, price, fill = cut)) +


geom_boxplot(size = 1) +
ggtitle("Adjust line width of boxplot in ggplot2")

# Need to modify the plotly object to make sure line width is larger than default
p <- plotly_build(p)

p$data <- lapply(p$data, FUN = function(x){


x$line = list(width = 10)
return(x)
})

# Create a shareable link to your chart


# Set up API credentials: https://plot.ly/r/getting-started
chart_link = plotly_POST(p, filename="geom_boxplot/linewidth")
chart_link

Whiskers

library(plotly)
set.seed(123)

df <- diamonds[sample(1:nrow(diamonds), size = 1000),]

# This is how it needs to be done in ggplot


p <- ggplot(df, aes(color, price)) +
stat_boxplot(geom ='errorbar') +
geom_boxplot()+
ggtitle("Add horizontal lines to whiskers using ggplot2")

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)

# Create a shareable link to your chart


# Set up API credentials: https://plot.ly/r/getting-started
chart_link = plotly_POST(p, filename="geom_boxplot/whiskers")
chart_link

These example were inspired by Cookbook for R.

https://plot.ly/ggplot2/box-plots/ 7/7

You might also like