Skip to content

Instantly share code, notes, and snippets.

@aravindhebbali
Last active September 25, 2017 09:59
Show Gist options
  • Save aravindhebbali/246091b512a6c006e68374e2d24caf7c to your computer and use it in GitHub Desktop.
Save aravindhebbali/246091b512a6c006e68374e2d24caf7c to your computer and use it in GitHub Desktop.
ggplot2: Box Plot
# install
install.packages('ggplot2')
install.packages('readr')
# library
library(ggplot2)
library(readr)
# import data
ecom <- readr::read_csv('https://raw.githubusercontent.com/rsquaredacademy/datasets/master/web.csv')
ecom
# Univariate Box Plot
ggplot(ecom) +
geom_boxplot(aes(x = factor(1), y = n_visit))
# Box Plot
ggplot(ecom) +
geom_boxplot(aes(x = factor(device), y = n_visit))
# Horizontal Box Plot
ggplot(ecom) +
geom_boxplot(aes(x = factor(device), y = n_visit)) +
coord_flip()
# Notch
ggplot(ecom) +
geom_boxplot(aes(x = factor(device), y = n_visit),
notch = TRUE)
# Jitter
ggplot(ecom, aes(x = factor(device), y = n_visit)) +
geom_boxplot() +
geom_jitter(width = 0.2, color = 'blue')
# Outlier Color
ggplot(ecom) +
geom_boxplot(aes(x = factor(device), y = duration),
outlier.color = 'red')
# Outlier Shape
ggplot(ecom) +
geom_boxplot(aes(x = factor(device), y = duration),
outlier.shape = 23)
# Outlier Shape
ggplot(ecom) +
geom_boxplot(aes(x = factor(device), y = duration), outlier.shape = 23) +
expand_limits(y = c(0, 1100))
# Outlier Size
ggplot(ecom) +
geom_boxplot(aes(x = factor(device), y = duration), outlier.size = 3) +
expand_limits(y = c(0, 1100))
# Outlier Alpha
ggplot(ecom) +
geom_boxplot(aes(x = factor(device), y = duration),
outlier.color = 'blue', outlier.alpha = 0.3) +
expand_limits(y = c(0, 1100))
# Specify Values for Fill
ggplot(ecom) +
geom_boxplot(aes(x = factor(device), y = duration),
fill = c('blue', 'red', 'green')) +
expand_limits(y = c(0, 1100))
# Map Fill to Variable
ggplot(ecom) +
geom_boxplot(aes(x = factor(device), y = duration,
fill = factor(device))) +
expand_limits(y = c(0, 1100))
# Specify Values for Alpha
ggplot(ecom) +
geom_boxplot(aes(x = factor(device), y = duration),
fill = 'blue', alpha = 0.3) +
expand_limits(y = c(0, 1100))
# Specify Values for Color
ggplot(ecom) +
geom_boxplot(aes(x = factor(device), y = duration),
color = c('blue', 'red', 'green')) +
expand_limits(y = c(0, 1100))
# Map Color to Variables
ggplot(ecom) +
geom_boxplot(aes(x = factor(device), y = duration,
color = factor(device))) +
expand_limits(y = c(0, 1100))
# Specify Values for Line Width
ggplot(ecom) +
geom_boxplot(aes(x = factor(device), y = duration),
size = 1.5) +
expand_limits(y = c(0, 1100))
# Specify Values for Line Type
ggplot(ecom) +
geom_boxplot(aes(x = factor(device), y = duration),
linetype = 2) +
expand_limits(y = c(0, 1100))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment