library(tidyverse)
## ── Attaching packages ─────────────────────────────────────── tidyverse 1.3.2 ──
## ✔ ggplot2 3.4.3 ✔ purrr 1.0.2
## ✔ tibble 3.2.1 ✔ dplyr 1.1.2
## ✔ tidyr 1.3.0 ✔ stringr 1.5.0
## ✔ readr 2.1.3 ✔ forcats 0.5.2
## Warning: package 'ggplot2' was built under R version 4.2.3
## Warning: package 'tibble' was built under R version 4.2.3
## Warning: package 'tidyr' was built under R version 4.2.3
## Warning: package 'purrr' was built under R version 4.2.3
## Warning: package 'dplyr' was built under R version 4.2.3
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag() masks stats::lag()
library(dplyr)
The Ultimate Halloween Candy Power Ranking was selected as my data for this assignment. The goal of this data is to figure out what Halloween candy people most prefer.The data was collected by letting 8,371 voters from different IP to vote on about 269,000 randomly generated matchups.
The dataset was downloaded from the following link and uploaded to Github.
https://fivethirtyeight.com/videos/the-ultimate-halloween-candy-power-ranking/
# Downloading data from
candy_data <- read.csv("https://raw.githubusercontent.com/tonyCUNY/tonyCUNY/main/candy-data.csv")
as.data.frame(candy_data)
## competitorname chocolate fruity caramel peanutyalmondy nougat
## 1 100 Grand 1 0 1 0 0
## 2 3 Musketeers 1 0 0 0 1
## 3 One dime 0 0 0 0 0
## 4 One quarter 0 0 0 0 0
## 5 Air Heads 0 1 0 0 0
## 6 Almond Joy 1 0 0 1 0
## 7 Baby Ruth 1 0 1 1 1
## 8 Boston Baked Beans 0 0 0 1 0
## 9 Candy Corn 0 0 0 0 0
## 10 Caramel Apple Pops 0 1 1 0 0
## 11 Charleston Chew 1 0 0 0 1
## 12 Chewey Lemonhead Fruit Mix 0 1 0 0 0
## 13 Chiclets 0 1 0 0 0
## 14 Dots 0 1 0 0 0
## 15 Dum Dums 0 1 0 0 0
## 16 Fruit Chews 0 1 0 0 0
## 17 Fun Dip 0 1 0 0 0
## 18 Gobstopper 0 1 0 0 0
## 19 Haribo Gold Bears 0 1 0 0 0
## 20 Haribo Happy Cola 0 0 0 0 0
## 21 Haribo Sour Bears 0 1 0 0 0
## 22 Haribo Twin Snakes 0 1 0 0 0
## 23 Hershey's Kisses 1 0 0 0 0
## 24 Hershey's Krackel 1 0 0 0 0
## 25 Hershey's Milk Chocolate 1 0 0 0 0
## 26 Hershey's Special Dark 1 0 0 0 0
## 27 Jawbusters 0 1 0 0 0
## 28 Junior Mints 1 0 0 0 0
## 29 Kit Kat 1 0 0 0 0
## 30 Laffy Taffy 0 1 0 0 0
## 31 Lemonhead 0 1 0 0 0
## 32 Lifesavers big ring gummies 0 1 0 0 0
## 33 Peanut butter M&M's 1 0 0 1 0
## 34 M&M's 1 0 0 0 0
## 35 Mike & Ike 0 1 0 0 0
## 36 Milk Duds 1 0 1 0 0
## 37 Milky Way 1 0 1 0 1
## 38 Milky Way Midnight 1 0 1 0 1
## 39 Milky Way Simply Caramel 1 0 1 0 0
## 40 Mounds 1 0 0 0 0
## 41 Mr Good Bar 1 0 0 1 0
## 42 Nerds 0 1 0 0 0
## 43 Nestle Butterfinger 1 0 0 1 0
## 44 Nestle Crunch 1 0 0 0 0
## 45 Nik L Nip 0 1 0 0 0
## 46 Now & Later 0 1 0 0 0
## 47 Payday 0 0 0 1 1
## 48 Peanut M&Ms 1 0 0 1 0
## 49 Pixie Sticks 0 0 0 0 0
## 50 Pop Rocks 0 1 0 0 0
## 51 Red vines 0 1 0 0 0
## 52 Reese's Miniatures 1 0 0 1 0
## 53 Reese's Peanut Butter cup 1 0 0 1 0
## 54 Reese's pieces 1 0 0 1 0
## 55 Reese's stuffed with pieces 1 0 0 1 0
## 56 Ring pop 0 1 0 0 0
## 57 Rolo 1 0 1 0 0
## 58 Root Beer Barrels 0 0 0 0 0
## 59 Runts 0 1 0 0 0
## 60 Sixlets 1 0 0 0 0
## 61 Skittles original 0 1 0 0 0
## 62 Skittles wildberry 0 1 0 0 0
## 63 Nestle Smarties 1 0 0 0 0
## 64 Smarties candy 0 1 0 0 0
## 65 Snickers 1 0 1 1 1
## 66 Snickers Crisper 1 0 1 1 0
## 67 Sour Patch Kids 0 1 0 0 0
## 68 Sour Patch Tricksters 0 1 0 0 0
## 69 Starburst 0 1 0 0 0
## 70 Strawberry bon bons 0 1 0 0 0
## 71 Sugar Babies 0 0 1 0 0
## 72 Sugar Daddy 0 0 1 0 0
## 73 Super Bubble 0 1 0 0 0
## 74 Swedish Fish 0 1 0 0 0
## 75 Tootsie Pop 1 1 0 0 0
## 76 Tootsie Roll Juniors 1 0 0 0 0
## 77 Tootsie Roll Midgies 1 0 0 0 0
## 78 Tootsie Roll Snack Bars 1 0 0 0 0
## 79 Trolli Sour Bites 0 1 0 0 0
## 80 Twix 1 0 1 0 0
## 81 Twizzlers 0 1 0 0 0
## 82 Warheads 0 1 0 0 0
## 83 Welch's Fruit Snacks 0 1 0 0 0
## 84 Werther's Original Caramel 0 0 1 0 0
## 85 Whoppers 1 0 0 0 0
## crispedricewafer hard bar pluribus sugarpercent pricepercent winpercent
## 1 1 0 1 0 0.732 0.860 66.97173
## 2 0 0 1 0 0.604 0.511 67.60294
## 3 0 0 0 0 0.011 0.116 32.26109
## 4 0 0 0 0 0.011 0.511 46.11650
## 5 0 0 0 0 0.906 0.511 52.34146
## 6 0 0 1 0 0.465 0.767 50.34755
## 7 0 0 1 0 0.604 0.767 56.91455
## 8 0 0 0 1 0.313 0.511 23.41782
## 9 0 0 0 1 0.906 0.325 38.01096
## 10 0 0 0 0 0.604 0.325 34.51768
## 11 0 0 1 0 0.604 0.511 38.97504
## 12 0 0 0 1 0.732 0.511 36.01763
## 13 0 0 0 1 0.046 0.325 24.52499
## 14 0 0 0 1 0.732 0.511 42.27208
## 15 0 1 0 0 0.732 0.034 39.46056
## 16 0 0 0 1 0.127 0.034 43.08892
## 17 0 1 0 0 0.732 0.325 39.18550
## 18 0 1 0 1 0.906 0.453 46.78335
## 19 0 0 0 1 0.465 0.465 57.11974
## 20 0 0 0 1 0.465 0.465 34.15896
## 21 0 0 0 1 0.465 0.465 51.41243
## 22 0 0 0 1 0.465 0.465 42.17877
## 23 0 0 0 1 0.127 0.093 55.37545
## 24 1 0 1 0 0.430 0.918 62.28448
## 25 0 0 1 0 0.430 0.918 56.49050
## 26 0 0 1 0 0.430 0.918 59.23612
## 27 0 1 0 1 0.093 0.511 28.12744
## 28 0 0 0 1 0.197 0.511 57.21925
## 29 1 0 1 0 0.313 0.511 76.76860
## 30 0 0 0 0 0.220 0.116 41.38956
## 31 0 1 0 0 0.046 0.104 39.14106
## 32 0 0 0 0 0.267 0.279 52.91139
## 33 0 0 0 1 0.825 0.651 71.46505
## 34 0 0 0 1 0.825 0.651 66.57458
## 35 0 0 0 1 0.872 0.325 46.41172
## 36 0 0 0 1 0.302 0.511 55.06407
## 37 0 0 1 0 0.604 0.651 73.09956
## 38 0 0 1 0 0.732 0.441 60.80070
## 39 0 0 1 0 0.965 0.860 64.35334
## 40 0 0 1 0 0.313 0.860 47.82975
## 41 0 0 1 0 0.313 0.918 54.52645
## 42 0 1 0 1 0.848 0.325 55.35405
## 43 0 0 1 0 0.604 0.767 70.73564
## 44 1 0 1 0 0.313 0.767 66.47068
## 45 0 0 0 1 0.197 0.976 22.44534
## 46 0 0 0 1 0.220 0.325 39.44680
## 47 0 0 1 0 0.465 0.767 46.29660
## 48 0 0 0 1 0.593 0.651 69.48379
## 49 0 0 0 1 0.093 0.023 37.72234
## 50 0 1 0 1 0.604 0.837 41.26551
## 51 0 0 0 1 0.581 0.116 37.34852
## 52 0 0 0 0 0.034 0.279 81.86626
## 53 0 0 0 0 0.720 0.651 84.18029
## 54 0 0 0 1 0.406 0.651 73.43499
## 55 0 0 0 0 0.988 0.651 72.88790
## 56 0 1 0 0 0.732 0.965 35.29076
## 57 0 0 0 1 0.860 0.860 65.71629
## 58 0 1 0 1 0.732 0.069 29.70369
## 59 0 1 0 1 0.872 0.279 42.84914
## 60 0 0 0 1 0.220 0.081 34.72200
## 61 0 0 0 1 0.941 0.220 63.08514
## 62 0 0 0 1 0.941 0.220 55.10370
## 63 0 0 0 1 0.267 0.976 37.88719
## 64 0 1 0 1 0.267 0.116 45.99583
## 65 0 0 1 0 0.546 0.651 76.67378
## 66 1 0 1 0 0.604 0.651 59.52925
## 67 0 0 0 1 0.069 0.116 59.86400
## 68 0 0 0 1 0.069 0.116 52.82595
## 69 0 0 0 1 0.151 0.220 67.03763
## 70 0 1 0 1 0.569 0.058 34.57899
## 71 0 0 0 1 0.965 0.767 33.43755
## 72 0 0 0 0 0.418 0.325 32.23100
## 73 0 0 0 0 0.162 0.116 27.30386
## 74 0 0 0 1 0.604 0.755 54.86111
## 75 0 1 0 0 0.604 0.325 48.98265
## 76 0 0 0 0 0.313 0.511 43.06890
## 77 0 0 0 1 0.174 0.011 45.73675
## 78 0 0 1 0 0.465 0.325 49.65350
## 79 0 0 0 1 0.313 0.255 47.17323
## 80 1 0 1 0 0.546 0.906 81.64291
## 81 0 0 0 0 0.220 0.116 45.46628
## 82 0 1 0 0 0.093 0.116 39.01190
## 83 0 0 0 1 0.313 0.313 44.37552
## 84 0 1 0 0 0.186 0.267 41.90431
## 85 1 0 0 1 0.872 0.848 49.52411
glimpse(candy_data)
## Rows: 85
## Columns: 13
## $ competitorname <chr> "100 Grand", "3 Musketeers", "One dime", "One quarter…
## $ chocolate <int> 1, 1, 0, 0, 0, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0,…
## $ fruity <int> 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1,…
## $ caramel <int> 1, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ peanutyalmondy <int> 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ nougat <int> 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0,…
## $ crispedricewafer <int> 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ hard <int> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 1,…
## $ bar <int> 1, 1, 0, 0, 0, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0,…
## $ pluribus <int> 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 1, 1, 0, 1, 0, 1,…
## $ sugarpercent <dbl> 0.732, 0.604, 0.011, 0.011, 0.906, 0.465, 0.604, 0.31…
## $ pricepercent <dbl> 0.860, 0.511, 0.116, 0.511, 0.511, 0.767, 0.767, 0.51…
## $ winpercent <dbl> 66.97173, 67.60294, 32.26109, 46.11650, 52.34146, 50.…
# Creating a subset data frame that contain Candy's name and Percentage data.
sub_candy <- candy_data %>% select(competitorname, sugarpercent, pricepercent, winpercent)
head(sub_candy)
## competitorname sugarpercent pricepercent winpercent
## 1 100 Grand 0.732 0.860 66.97173
## 2 3 Musketeers 0.604 0.511 67.60294
## 3 One dime 0.011 0.116 32.26109
## 4 One quarter 0.011 0.511 46.11650
## 5 Air Heads 0.906 0.511 52.34146
## 6 Almond Joy 0.465 0.767 50.34755
#rename the variables
colnames(sub_candy) <- c("Competitor", "Sugar_percent", "Price_percent", "Win_percent")
head(sub_candy)
## Competitor Sugar_percent Price_percent Win_percent
## 1 100 Grand 0.732 0.860 66.97173
## 2 3 Musketeers 0.604 0.511 67.60294
## 3 One dime 0.011 0.116 32.26109
## 4 One quarter 0.011 0.511 46.11650
## 5 Air Heads 0.906 0.511 52.34146
## 6 Almond Joy 0.465 0.767 50.34755
#arrange the data by Win percentage
#Recreating a plot to reflect each Brand win rate.
desc_candy <- sub_candy %>% arrange(desc(Win_percent))
desc_candy
## Competitor Sugar_percent Price_percent Win_percent
## 1 Reese's Peanut Butter cup 0.720 0.651 84.18029
## 2 Reese's Miniatures 0.034 0.279 81.86626
## 3 Twix 0.546 0.906 81.64291
## 4 Kit Kat 0.313 0.511 76.76860
## 5 Snickers 0.546 0.651 76.67378
## 6 Reese's pieces 0.406 0.651 73.43499
## 7 Milky Way 0.604 0.651 73.09956
## 8 Reese's stuffed with pieces 0.988 0.651 72.88790
## 9 Peanut butter M&M's 0.825 0.651 71.46505
## 10 Nestle Butterfinger 0.604 0.767 70.73564
## 11 Peanut M&Ms 0.593 0.651 69.48379
## 12 3 Musketeers 0.604 0.511 67.60294
## 13 Starburst 0.151 0.220 67.03763
## 14 100 Grand 0.732 0.860 66.97173
## 15 M&M's 0.825 0.651 66.57458
## 16 Nestle Crunch 0.313 0.767 66.47068
## 17 Rolo 0.860 0.860 65.71629
## 18 Milky Way Simply Caramel 0.965 0.860 64.35334
## 19 Skittles original 0.941 0.220 63.08514
## 20 Hershey's Krackel 0.430 0.918 62.28448
## 21 Milky Way Midnight 0.732 0.441 60.80070
## 22 Sour Patch Kids 0.069 0.116 59.86400
## 23 Snickers Crisper 0.604 0.651 59.52925
## 24 Hershey's Special Dark 0.430 0.918 59.23612
## 25 Junior Mints 0.197 0.511 57.21925
## 26 Haribo Gold Bears 0.465 0.465 57.11974
## 27 Baby Ruth 0.604 0.767 56.91455
## 28 Hershey's Milk Chocolate 0.430 0.918 56.49050
## 29 Hershey's Kisses 0.127 0.093 55.37545
## 30 Nerds 0.848 0.325 55.35405
## 31 Skittles wildberry 0.941 0.220 55.10370
## 32 Milk Duds 0.302 0.511 55.06407
## 33 Swedish Fish 0.604 0.755 54.86111
## 34 Mr Good Bar 0.313 0.918 54.52645
## 35 Lifesavers big ring gummies 0.267 0.279 52.91139
## 36 Sour Patch Tricksters 0.069 0.116 52.82595
## 37 Air Heads 0.906 0.511 52.34146
## 38 Haribo Sour Bears 0.465 0.465 51.41243
## 39 Almond Joy 0.465 0.767 50.34755
## 40 Tootsie Roll Snack Bars 0.465 0.325 49.65350
## 41 Whoppers 0.872 0.848 49.52411
## 42 Tootsie Pop 0.604 0.325 48.98265
## 43 Mounds 0.313 0.860 47.82975
## 44 Trolli Sour Bites 0.313 0.255 47.17323
## 45 Gobstopper 0.906 0.453 46.78335
## 46 Mike & Ike 0.872 0.325 46.41172
## 47 Payday 0.465 0.767 46.29660
## 48 One quarter 0.011 0.511 46.11650
## 49 Smarties candy 0.267 0.116 45.99583
## 50 Tootsie Roll Midgies 0.174 0.011 45.73675
## 51 Twizzlers 0.220 0.116 45.46628
## 52 Welch's Fruit Snacks 0.313 0.313 44.37552
## 53 Fruit Chews 0.127 0.034 43.08892
## 54 Tootsie Roll Juniors 0.313 0.511 43.06890
## 55 Runts 0.872 0.279 42.84914
## 56 Dots 0.732 0.511 42.27208
## 57 Haribo Twin Snakes 0.465 0.465 42.17877
## 58 Werther's Original Caramel 0.186 0.267 41.90431
## 59 Laffy Taffy 0.220 0.116 41.38956
## 60 Pop Rocks 0.604 0.837 41.26551
## 61 Dum Dums 0.732 0.034 39.46056
## 62 Now & Later 0.220 0.325 39.44680
## 63 Fun Dip 0.732 0.325 39.18550
## 64 Lemonhead 0.046 0.104 39.14106
## 65 Warheads 0.093 0.116 39.01190
## 66 Charleston Chew 0.604 0.511 38.97504
## 67 Candy Corn 0.906 0.325 38.01096
## 68 Nestle Smarties 0.267 0.976 37.88719
## 69 Pixie Sticks 0.093 0.023 37.72234
## 70 Red vines 0.581 0.116 37.34852
## 71 Chewey Lemonhead Fruit Mix 0.732 0.511 36.01763
## 72 Ring pop 0.732 0.965 35.29076
## 73 Sixlets 0.220 0.081 34.72200
## 74 Strawberry bon bons 0.569 0.058 34.57899
## 75 Caramel Apple Pops 0.604 0.325 34.51768
## 76 Haribo Happy Cola 0.465 0.465 34.15896
## 77 Sugar Babies 0.965 0.767 33.43755
## 78 One dime 0.011 0.116 32.26109
## 79 Sugar Daddy 0.418 0.325 32.23100
## 80 Root Beer Barrels 0.732 0.069 29.70369
## 81 Jawbusters 0.093 0.511 28.12744
## 82 Super Bubble 0.162 0.116 27.30386
## 83 Chiclets 0.046 0.325 24.52499
## 84 Boston Baked Beans 0.313 0.511 23.41782
## 85 Nik L Nip 0.197 0.976 22.44534
#Top 10 Candy
desc_candy %>%
top_n(10, Win_percent) %>%
ggplot()+geom_bar(mapping=aes(x=reorder(Competitor, -Win_percent), y=Win_percent),stat = "identity")+
theme(axis.text.x=element_text(angle=90,hjust=1)) +
labs(
title = "Top 10 Candy",
x = "Candy", y = "Win Percentage",
)
The most popular Candy Brand is Reese’s Candy. I would like to make a bar chart to include all Candy Win percentage but the x-axis label overlapped and I couldn’t figure a way to reslove it yet.