Data Dive Week 3: Yuritzi Avila-Robledo

3 Group By Categorical

library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.1.4     ✔ readr     2.1.4
## ✔ forcats   1.0.0     ✔ stringr   1.5.1
## ✔ ggplot2   3.4.4     ✔ tibble    3.2.1
## ✔ lubridate 1.9.3     ✔ tidyr     1.3.0
## ✔ purrr     1.0.2     
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
chocolate <- read.csv("~/Desktop/chocolate.csv")
library(readr)
summary(chocolate)
##       ref       company_manufacturer company_location    review_date  
##  Min.   :   5   Length:2530          Length:2530        Min.   :2006  
##  1st Qu.: 802   Class :character     Class :character   1st Qu.:2012  
##  Median :1454   Mode  :character     Mode  :character   Median :2015  
##  Mean   :1430                                           Mean   :2014  
##  3rd Qu.:2079                                           3rd Qu.:2018  
##  Max.   :2712                                           Max.   :2021  
##  country_of_bean_origin specific_bean_origin_or_bar_name cocoa_percent   
##  Length:2530            Length:2530                      Min.   :0.4200  
##  Class :character       Class :character                 1st Qu.:0.7000  
##  Mode  :character       Mode  :character                 Median :0.7000  
##                                                          Mean   :0.7164  
##                                                          3rd Qu.:0.7400  
##                                                          Max.   :1.0000  
##  ingredients        most_memorable_characteristics     rating     
##  Length:2530        Length:2530                    Min.   :1.000  
##  Class :character   Class :character               1st Qu.:3.000  
##  Mode  :character   Mode  :character               Median :3.250  
##                                                    Mean   :3.196  
##                                                    3rd Qu.:3.500  
##                                                    Max.   :4.000
library(dplyr)
  1. Average Rating Grouped By Company Manufacturers Location
rating_based_on_manufacturer <- chocolate |>
  group_by(company_location)|>
  summarize(avg_rating = mean(rating,na.rm = TRUE), count_n=n())
rating_based_on_manufacturer
## # A tibble: 67 × 3
##    company_location avg_rating count_n
##    <chr>                 <dbl>   <int>
##  1 Amsterdam              3.31      12
##  2 Argentina              3.31       9
##  3 Australia              3.36      53
##  4 Austria                3.26      30
##  5 Belgium                3.10      63
##  6 Bolivia                3.25       2
##  7 Brazil                 3.28      25
##  8 Canada                 3.30     177
##  9 Chile                  3.75       2
## 10 Colombia               3.20      29
## # ℹ 57 more rows

Plot:

library(ggplot2)

ggplot(rating_based_on_manufacturer, aes(x = company_location, y = avg_rating)) +
  geom_bar(stat = "identity", fill = "skyblue") +
  labs(x = "Company Location", y = "Average Rating") +
  ggtitle("Average Rating by Company Location") + theme(legend.position = "right", axis.text.x = element_text(angle = 90, hjust = 1)) 

From this group by function, I was able to group the average rating per manufacturers location. From this bar chart I can see that India as well as Portugal are alarmingly the two lowest rated countries that manufacture chocolate, meanwhile the highest rated chocolate is manufactured in Chile. I could assume this is due to the country’s land, Chile is very mountainous and tropical that could be better suited for harvesting the cocoa needed for the chocolate production. Meanwhile India and Portugal do not have tropical landscapes thus their chocolate ratings are lower.

  1. Average Rating Grouped By Country of Bean Origin
rating_based_on_bean_origin <- chocolate |>
  group_by(country_of_bean_origin)|>
  summarize(avg_rating = mean(rating,na.rm = TRUE), count_n=n())
print(rating_based_on_bean_origin)
## # A tibble: 62 × 3
##    country_of_bean_origin avg_rating count_n
##    <chr>                       <dbl>   <int>
##  1 Australia                    3.25       3
##  2 Belize                       3.23      76
##  3 Blend                        3.04     156
##  4 Bolivia                      3.18      80
##  5 Brazil                       3.26      78
##  6 Burma                        3          1
##  7 Cameroon                     3.08       3
##  8 China                        3.5        1
##  9 Colombia                     3.20      79
## 10 Congo                        3.32      11
## # ℹ 52 more rows

Plot:

ggplot(rating_based_on_bean_origin, aes(x = country_of_bean_origin, y = avg_rating)) +
  geom_bar(stat = "identity", fill = "skyblue") +
  labs(x = "Cocoa Bean Country of Origin ", y = "Average Rating") +
  ggtitle("Average Rating by Cocoa Bean Country of Origin") + theme(legend.position = "right", axis.text.x = element_text(angle = 90, hjust = 1)) 

From this plot I can see that the lowest rated chocolate’s cocoa bean origin is Puerto Rico which is actually unexpected as it is a very tropical land and it has rich history with cacao fruit. I could possibly attribute the low rating with Puerto Rico’s manufacturer not having suitable, tropical lands to keep the cocoa beans in while waiting to produce chocolate.

  1. Group By using CUT function -> Group By Cocoa Percentage
cocoa_percent_ranges<-c(0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100)

chocolate$cocoa_percent<-chocolate$cocoa_percent*100

chocolate$Cocoa_Percentages<-cut(chocolate$cocoa_percent,breaks=cocoa_percent_ranges)

chocolate$Cocoa_Percentages <- as.factor(chocolate$Cocoa_Percentages)

rating_based_on_cocoa_percent_ranges <- chocolate |>
  group_by(Cocoa_Percentages)|>
  summarize(avg_rating = mean(rating,na.rm = TRUE))

Plot:

library(ggplot2)

ggplot(rating_based_on_cocoa_percent_ranges, aes(x = Cocoa_Percentages, y = avg_rating, fill = "orange")) +
  geom_bar(stat = "identity") +
  scale_x_discrete(labels = c("0-10%", "10-20%", "20-30%", "30-40%", "40-50%", "50-60%", "60-70%", "70-80%", "80-90%", "90-100%")) +
  labs(title = "Average Rating Based on Cocoa Percentage Range")

From this plot we can see that the average rating of chocolate bars is higher with chocolate bars having 20-30% cocoa, and we also see that the highly concentrated cocoa chocolate bars have the lowest ratings. This seems fair as a highly concentrated chocolate bar may have a more bitter taste!

Categorical Variable Combinations

bean_and_manufacturer<-chocolate|>
  group_by(company_manufacturer,country_of_bean_origin)|>
  summarise(count=n())
## `summarise()` has grouped output by 'company_manufacturer'. You can override
## using the `.groups` argument.
bean_and_manufacturer
## # A tibble: 1,762 × 3
## # Groups:   company_manufacturer [580]
##    company_manufacturer country_of_bean_origin count
##    <chr>                <chr>                  <int>
##  1 5150                 Dominican Republic         1
##  2 5150                 Fiji                       1
##  3 5150                 India                      1
##  4 5150                 Madagascar                 1
##  5 5150                 Tanzania                   1
##  6 5150                 Uganda                     1
##  7 5150                 Venezuela                  1
##  8 A. Morin             Bolivia                    1
##  9 A. Morin             Brazil                     1
## 10 A. Morin             Burma                      1
## # ℹ 1,752 more rows
library(ggplot2)

ggplot(bean_and_manufacturer, aes(x = company_manufacturer, y = count, fill = country_of_bean_origin)) +
  geom_bar(stat = "identity") +
  labs(title = "Count of Bean Origins by Manufacturer",
       x = "Manufacturer",
       y = "Count",
       fill = "Bean Origin") +
  theme(axis.text.x = element_text(angle = 90, hjust = 1))

overall_max_count <- max(bean_and_manufacturer$count)

manufacturer_highest_count <- bean_and_manufacturer %>%
  filter(count == overall_max_count) %>%
  pull(company_manufacturer)

cat("Maximum count of bean origins across all manufacturers:", overall_max_count)
## Maximum count of bean origins across all manufacturers: 12
cat("Manufacturer(s) with the highest count of bean origins overall:", paste(manufacturer_highest_count, collapse = ", "))
## Manufacturer(s) with the highest count of bean origins overall: Domori, Friis Holm

For this section I decided to use the two categorical variable combinations of company_manufacturer and country_of_bean_origin. I decided to plot a bar plot where each bar represents a manufacturer, and the height of the bar represents the count of occurrences of each combination of manufacturer and bean origin. I decided to calculate the frequency of each combination of “company_location” and “country_of_bean_origin” to find the most and least common ones. The manufacturers with the highest count of bean origins were Domori and Friis Holm. This might be because these manufacturers are highly seeked due to their quality of chocolate bar production and/or their proximity to the countries where the cocoa beans are harvested.