Project Goals

  • Practice making a dataset “tidy”
  • Get more familiar with ggplot
  • Use clipr and datawrapper to make graphics more presentable

Setup

load in packages to tidy up our data

library(tidyverse)
library(janitor)
library(lubridate)
library(ggthemes)
library(clipr)

Import data

Load class skittles data

# save skittles data as new tibble
data <- read_csv("https://docs.google.com/spreadsheets/d/e/2PACX-1vTxm9NxK67thlGjYOQBo_0JRvx2d137xt0nZGffqR6P1vl8QrlTUduiOsDJ2FKF6yLgQAQphVZve76z/pub?output=csv") %>% clean_names()
## Rows: 124 Columns: 7
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (2): Timestamp, Name
## dbl (5): Red, Green, Orange, Yellow, Purple
## 
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
# peek at the data
data %>% glimpse()
## Rows: 124
## Columns: 7
## $ timestamp <chr> "7/27/2020 18:21:19", "8/1/2020 16:04:56", "7/30/2020 14:56:…
## $ name      <chr> "Alora Jones", "Alyssa Hiarker", "Annie Patton", "Christian …
## $ red       <dbl> 12, 13, 12, 9, 7, 10, 12, 11, 7, 18, 13, 11, 10, 14, 8, 8, 1…
## $ green     <dbl> 11, 15, 12, 10, 12, 14, 15, 5, 10, 9, 11, 13, 7, 12, 15, 10,…
## $ orange    <dbl> 12, 10, 8, 17, 13, 9, 12, 17, 10, 13, 11, 7, 12, 10, 20, 13,…
## $ yellow    <dbl> 9, 9, 10, 6, 11, 11, 10, 14, 21, 7, 11, 7, 15, 9, 4, 14, 10,…
## $ purple    <dbl> 15, 15, 18, 18, 17, 16, 12, 13, 14, 13, 12, 21, 16, 14, 10, …

Fix Dates

Change the dates from characters to real dates using lubridate

skittles <- data %>% 
  mutate(
    date_entered = mdy_hms(timestamp) %>% date() #use lubridate to change timestamp str to actual timestamp, save as new column
  ) %>% 
  select(-timestamp) # use select to get rid of old timestamp column

skittles %>% glimpse()
## Rows: 124
## Columns: 7
## $ name         <chr> "Alora Jones", "Alyssa Hiarker", "Annie Patton", "Christi…
## $ red          <dbl> 12, 13, 12, 9, 7, 10, 12, 11, 7, 18, 13, 11, 10, 14, 8, 8…
## $ green        <dbl> 11, 15, 12, 10, 12, 14, 15, 5, 10, 9, 11, 13, 7, 12, 15, …
## $ orange       <dbl> 12, 10, 8, 17, 13, 9, 12, 17, 10, 13, 11, 7, 12, 10, 20, …
## $ yellow       <dbl> 9, 9, 10, 6, 11, 11, 10, 14, 21, 7, 11, 7, 15, 9, 4, 14, …
## $ purple       <dbl> 15, 15, 18, 18, 17, 16, 12, 13, 14, 13, 12, 21, 16, 14, 1…
## $ date_entered <date> 2020-07-27, 2020-08-01, 2020-07-30, 2020-06-23, 2020-07-…

Pivot data

Change our data shape from a “wide” to a “long” table to work better for our plotting purposes

skittles_long <- skittles %>% 
  pivot_longer(
    cols = red:purple,
    names_to = "color",
    values_to = "candies"
  )

# take a look
skittles_long %>% head()

Average color counts

Use group_by and summarise to get the average counts for each color

skittles_avg <- skittles_long %>% 
  group_by(color) %>% 
  summarise(
    avg_candies = mean(candies)
  ) %>% 
  mutate(
    avg_candies = round(avg_candies, 1),
    color = str_to_title(color)
  ) %>% 
  arrange(avg_candies %>% desc())

skittles_avg

Chart the color averages

Plot our avg skittles data using ggplot2. Then, make it prettier by applying a theme!

avg_plot <- ggplot(skittles_avg, aes(x = reorder(color, avg_candies), y = avg_candies)) +
  geom_col() +
  geom_text(aes(label = avg_candies), hjust = 2, color = "white") +
  coord_flip() + # Flip it so it's more readable
  labs(
    title = "Average colors per bag",
    subtitle = "Calculated from student Skittles bags in Reporting with Data",
    caption = "By Ana Goodwin",
    x = "Colors",
    y = "Average amount per bag"
  ) +
  theme_solarized()

avg_plot

Datawrapper

Get data to use for datawrapper

# add data from skittles long to clipboard
skittles_avg %>% write_clip(allow_non_interactive = T)

Bonus questions

Let’s answer the following questions for some additional points: - Who got the least amount of Skittles in their bag? - Who got the most amount of Skittles in their bag? - What is the average number of candies in each bag?

Least Skittles

Unfortunately it looks like Jimena Pinzon got the least amount of Skittles in their bag – Sorry about that, Jimena.

skittles_long %>% 
  group_by(name) %>% 
  summarise(total_skittles = sum(candies)) %>% 
  slice(which.min(total_skittles)) # Find min row

Most Skittles

Looks like Mikayla Mondragon was the lucky recipient of a whopping 69 Skittles in her bag.

skittles_long %>% 
  group_by(name) %>% 
  summarise(total_skittles = sum(candies)) %>% 
  slice(which.max(total_skittles)) # Find max row

Average Skittles per bag

Looks like 58.4 is the average number of Skittles in a bag

skittles_long %>% 
  group_by(name) %>% 
  summarise(total_skittles = sum(candies)) %>% 
  summarise(average_skittles = mean(total_skittles)) %>% 
  mutate(
    average_skittles = round(average_skittles, 1)
  )