Skip to content

Instantly share code, notes, and snippets.

@aravindhebbali
Last active September 26, 2017 08:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aravindhebbali/d5e49b7903a54bd02d77ae2057f05d4a to your computer and use it in GitHub Desktop.
Save aravindhebbali/d5e49b7903a54bd02d77ae2057f05d4a to your computer and use it in GitHub Desktop.
Data Visualization: Box Plots
# basic plot
boxplot(mtcars$mpg)
# horizontal box plot
boxplot(mtcars$mpg, horizontal = TRUE)
# color
boxplot(mtcars$mpg, col = 'blue')
# border color
boxplot(mtcars$mpg, border = 'red')
# range
boxplot(mtcars$mpg, range = 0)
boxplot(mtcars$mpg, range = 1)
# outline
boxplot(mtcars$mpg, range = 1, outline = FALSE)
boxplot(mtcars$mpg, range = 1, outline = TRUE)
# varwidth
boxplot(mtcars$mpg, varwidth = TRUE, range = 1)
boxplot(mtcars$mpg, varwidth = FALSE, range = 1)
# bivariate plot
# using formula
boxplot(mtcars$mpg ~ mtcars$cyl)
# using data
mpg_split <- split(mtcars$mpg, mtcars$cyl)
mpg_4 <- mpg_split$`4`
mpg_6 <- mpg_split$`6`
mpg_8 <- mpg_split$`8`
boxplot(mpg_4, mpg_6, mpg_8)
# color
boxplot(mtcars$mpg ~ mtcars$cyl, col = 'blue')
# compare medians
hsb <- read.table('https://stats.idre.ucla.edu/wp-content/uploads/2016/02/hsb2-2.csv', header=T, sep=",")
boxplot(hsb$read ~ hsb$female, notch = TRUE,
col = c('red', 'blue'))
# putting it all together
boxplot(mtcars$mpg ~ mtcars$cyl, range = 1, outline = TRUE,
horizontal = TRUE, col = c('red', 'blue', 'yellow'),
main = 'Miles Per Gallon by Cylinders',
ylab = 'Number of Cylinders', xlab = 'Miles Per Gallon',
names = c('Four', 'Six', 'Eight'))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment