Practice 6 Boxplots with R

6.1 Directions


In this practice exercise, you will load data into R, and create a boxplot

6.2 R code used in the VoiceThread


# Loads the tidyverse package
library(tidyverse)
colors <- c("#FFFFFF","#F5FCC2","#E0ED87","#CCDE57","#B3C732","#94A813","#718200")

# Boxplot in base R
boxplot(data$price)

# Boxplot in ggplot2
data %>% ggplot(aes(y = price)) +
  geom_boxplot() +
  theme(axis.text.x = element_blank(),
  axis.ticks.x = element_blank())

# Side-by-side boxplots in base R
boxplot(data$price ~ data$color)

# Side-by-side boxplots in ggplot2
data %>% ggplot(aes(x = color, y = price, fill = color)) +
  geom_boxplot() +
  scale_fill_manual(values = colors)

6.3 Now you try


Use R to complete the following activities (this is just for practice you do not need to turn anything in).

  1. Plot a histogram of mpg from the mtcars dataset.
  2. Plot a histogram of mpg from the mtcars dataset using 10 bins.
  3. BONUS: Use xlim = c(0,40) to make the 10 bin histogram fit the axis better.

R Code Window

eyJsYW5ndWFnZSI6InIiLCJwcmVfZXhlcmNpc2VfY29kZSI6ImRhdGEoXCJtdGNhcnNcIikiLCJzYW1wbGUiOiIjIFRoZSBkYXRhIGhhcyBhbHJlYWR5IGJlZW4gbG9hZGVkXG5cbiMgUXVlc3Rpb24gMVxuXG5cbiMgUXVlc3Rpb24gMiIsInNvbHV0aW9uIjoiIyBMb2FkIGRhdGFcbmRhdGEoXCJtdGNhcnNcIilcblxuIyBRdWVzdGlvbiAxXG5oaXN0KG10Y2FycyRtcGcpXG5cbiMgUXVlc3Rpb24gMlxuaGlzdChtdGNhcnMkbXBnLCBicmVha3MgPSAxMClcblxuIyBCT05VU1xuaGlzdChtdGNhcnMkbXBnLCBicmVha3MgPSAxMCwgeGxpbT1jKDAsNDApKSJ9