Untitled

Unroasted Coffee (Golden Bean)

Specialty Coffee, Origin, and Quality

Specialty coffee has become popular these last couple of years. Sociey has change their preference for coffee chains to specialized coffee stores. In fact, more often than before we found small coffee shops close to our neighborhoods offering good quality coffee. I do not consider myself the judge of every coffee bean, but a truly enthusiast. I wonder if the quality of the coffee is related with the origin, production, and process.

In this project I will try to answer the question above and more questions The Coffee Dataset is interesting and contains 23 variables, including 8 qualitative variables, and at least 4 quantitative variables that we will use in the visualizations. Some of the variables describes coffee scores and ratings.

#I loaded this libraries that I alreade have loaded in R studio
library(dplyr)

Attaching package: 'dplyr'
The following objects are masked from 'package:stats':

    filter, lag
The following objects are masked from 'package:base':

    intersect, setdiff, setequal, union
library(tidyverse)
Warning: package 'ggplot2' was built under R version 4.6.2
Warning: package 'tidyr' was built under R version 4.6.2
── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ forcats   1.0.1     ✔ readr     2.2.0
✔ ggplot2   4.0.4     ✔ stringr   1.6.0
✔ lubridate 1.9.5     ✔ tibble    3.3.1
✔ purrr     1.2.2     ✔ tidyr     1.3.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
library(ggplot2)
library(highcharter)
Warning: package 'highcharter' was built under R version 4.6.2
Registered S3 method overwritten by 'quantmod':
  method            from
  as.zoo.data.frame zoo 
Highcharts (www.highcharts.com) is a Highsoft software product which is
not free for commercial and Governmental use
library(countrycode)
Warning: package 'countrycode' was built under R version 4.6.2
#I loaded the dataset from my carpet at the DISC C in the laptop
coffee_ratings <- read_csv("C:/Users/MFERN123/DATA_110/coffee_ratings.csv")
Rows: 989 Columns: 23
── Column specification ────────────────────────────────────────────────────────
Delimiter: ","
chr  (7): Location.Country, Location.Region, Data.Owner, Data.Type.Species, ...
dbl (16): Location.Altitude.Min, Location.Altitude.Max, Location.Altitude.Av...

ℹ 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.
#Exploration of the data the dataset contains 989 observations and 23 variables
dim(coffee_ratings)
[1] 989  23
#Here we can observe a glanze of the dataset 
head(coffee_ratings)
# A tibble: 6 × 23
  Location.Country Location.Region   Location.Altitude.Min Location.Altitude.Max
  <chr>            <chr>                             <dbl>                 <dbl>
1 United States    kona                                  0                     0
2 Brazil           sul de minas - c…                    12                    12
3 Brazil           sul de minas - c…                    12                    12
4 Ethiopia         sidamo                                0                     0
5 Ethiopia         sidamo                                0                     0
6 United States    kona                                  0                     0
# ℹ 19 more variables: Location.Altitude.Average <dbl>, Year <dbl>,
#   Data.Owner <chr>, Data.Type.Species <chr>, Data.Type.Variety <chr>,
#   `Data.Type.Processing method` <chr>,
#   `Data.Production.Number of bags` <dbl>, `Data.Production.Bag weight` <dbl>,
#   Data.Scores.Aroma <dbl>, Data.Scores.Flavor <dbl>,
#   Data.Scores.Aftertaste <dbl>, Data.Scores.Acidity <dbl>,
#   Data.Scores.Body <dbl>, Data.Scores.Balance <dbl>, …
#I use the function structure to see what kind of variables the dataset contains 
str(coffee_ratings)
spc_tbl_ [989 × 23] (S3: spec_tbl_df/tbl_df/tbl/data.frame)
 $ Location.Country              : chr [1:989] "United States" "Brazil" "Brazil" "Ethiopia" ...
 $ Location.Region               : chr [1:989] "kona" "sul de minas - carmo de minas" "sul de minas - carmo de minas" "sidamo" ...
 $ Location.Altitude.Min         : num [1:989] 0 12 12 0 0 0 1300 0 0 640 ...
 $ Location.Altitude.Max         : num [1:989] 0 12 12 0 0 0 1400 0 0 1400 ...
 $ Location.Altitude.Average     : num [1:989] 0 12 12 0 0 0 1350 0 0 1020 ...
 $ Year                          : num [1:989] 2010 2010 2010 2010 2010 2010 2010 2010 2010 2010 ...
 $ Data.Owner                    : chr [1:989] "kona pacific farmers cooperative" "jacques pereira carneiro" "jacques pereira carneiro" "ethiopia commodity exchange" ...
 $ Data.Type.Species             : chr [1:989] "Arabica" "Arabica" "Arabica" "Arabica" ...
 $ Data.Type.Variety             : chr [1:989] "nan" "Yellow Bourbon" "Yellow Bourbon" "nan" ...
 $ Data.Type.Processing method   : chr [1:989] "nan" "nan" "nan" "nan" ...
 $ Data.Production.Number of bags: num [1:989] 25 300 300 360 300 12 10 360 300 85 ...
 $ Data.Production.Bag weight    : num [1:989] 45.4 60 60 6 6 ...
 $ Data.Scores.Aroma             : num [1:989] 8.25 8.17 8.42 7.67 7.58 7.5 7.67 7.25 7.42 6.92 ...
 $ Data.Scores.Flavor            : num [1:989] 8.42 7.92 7.92 8 7.83 7.92 7.58 7.25 7.42 6.75 ...
 $ Data.Scores.Aftertaste        : num [1:989] 8.08 7.92 8 7.83 7.58 7.42 7.5 7.25 7.5 7.08 ...
 $ Data.Scores.Acidity           : num [1:989] 7.75 7.75 7.75 8 8 7.67 7.58 7.33 7.92 7.17 ...
 $ Data.Scores.Body              : num [1:989] 7.67 8.33 7.92 7.92 7.83 7.83 7.67 7.5 7.75 7.33 ...
 $ Data.Scores.Balance           : num [1:989] 7.83 8 8 7.83 7.5 7.58 7.58 8 7.58 6.67 ...
 $ Data.Scores.Uniformity        : num [1:989] 10 10 10 10 10 10 10 9.33 8.67 10 ...
 $ Data.Scores.Sweetness         : num [1:989] 10 10 10 10 10 10 10 10 8.67 8.67 ...
 $ Data.Scores.Moisture          : num [1:989] 0 0.08 0.01 0 0.1 0.01 0 0.1 0.05 0.08 ...
 $ Data.Scores.Total             : num [1:989] 86.2 86.2 86.2 85.1 83.8 ...
 $ Data.Color                    : chr [1:989] "Unknown" "Unknown" "Unknown" "Unknown" ...
 - attr(*, "spec")=
  .. cols(
  ..   Location.Country = col_character(),
  ..   Location.Region = col_character(),
  ..   Location.Altitude.Min = col_double(),
  ..   Location.Altitude.Max = col_double(),
  ..   Location.Altitude.Average = col_double(),
  ..   Year = col_double(),
  ..   Data.Owner = col_character(),
  ..   Data.Type.Species = col_character(),
  ..   Data.Type.Variety = col_character(),
  ..   `Data.Type.Processing method` = col_character(),
  ..   `Data.Production.Number of bags` = col_double(),
  ..   `Data.Production.Bag weight` = col_double(),
  ..   Data.Scores.Aroma = col_double(),
  ..   Data.Scores.Flavor = col_double(),
  ..   Data.Scores.Aftertaste = col_double(),
  ..   Data.Scores.Acidity = col_double(),
  ..   Data.Scores.Body = col_double(),
  ..   Data.Scores.Balance = col_double(),
  ..   Data.Scores.Uniformity = col_double(),
  ..   Data.Scores.Sweetness = col_double(),
  ..   Data.Scores.Moisture = col_double(),
  ..   Data.Scores.Total = col_double(),
  ..   Data.Color = col_character()
  .. )
 - attr(*, "problems")=<pointer: 0x0000014c860ff0f0> 
#In this chunk I realized that some of the names of the variables are written different, so I will change them later 
names(coffee_ratings)
 [1] "Location.Country"               "Location.Region"               
 [3] "Location.Altitude.Min"          "Location.Altitude.Max"         
 [5] "Location.Altitude.Average"      "Year"                          
 [7] "Data.Owner"                     "Data.Type.Species"             
 [9] "Data.Type.Variety"              "Data.Type.Processing method"   
[11] "Data.Production.Number of bags" "Data.Production.Bag weight"    
[13] "Data.Scores.Aroma"              "Data.Scores.Flavor"            
[15] "Data.Scores.Aftertaste"         "Data.Scores.Acidity"           
[17] "Data.Scores.Body"               "Data.Scores.Balance"           
[19] "Data.Scores.Uniformity"         "Data.Scores.Sweetness"         
[21] "Data.Scores.Moisture"           "Data.Scores.Total"             
[23] "Data.Color"                    
#I wanted to make sure if the countries of the dataset where all of them different or if some of them were repeated. I found a misspelling in the name of Ivory Coast, however, the information of this country it has not enough weight in the future visualization, so I decided to do not rename the country. 
unique(coffee_ratings$Location.Country)
 [1] "United States"                "Brazil"                      
 [3] "Ethiopia"                     "Indonesia"                   
 [5] "Haiti"                        "Thailand"                    
 [7] "Colombia"                     "Peru"                        
 [9] "Mexico"                       "Tanzania, United Republic Of"
[11] "Uganda"                       "Guatemala"                   
[13] "Papua New Guinea"             "El Salvador"                 
[15] "Kenya"                        "Costa Rica"                  
[17] "Taiwan"                       "Burundi"                     
[19] "Vietnam"                      "Panama"                      
[21] "Honduras"                     "Philippines"                 
[23] "India"                        "China"                       
[25] "Ecuador"                      "Nicaragua"                   
[27] "Malawi"                       "Zambia"                      
[29] "Myanmar"                      "Rwanda"                      
[31] "Laos"                         "Cote d?Ivoire"               
#Here I was checking how many countries I have to try to create a visualization and I realized that there were many of them 
length(unique(coffee_ratings$Location.Country))
[1] 32
#So I decided to check which ones of them has more observations, but the problem was that countries with good rating score were not in the top of observations registered. That is why I decided to look for other visualizations where I can include more countries 
sort(table(coffee_ratings$Location.Country),decreasing = TRUE)

                      Mexico                    Guatemala 
                         201                          168 
                    Colombia                United States 
                         128                           65 
                      Brazil                     Honduras 
                          61                           48 
                  Costa Rica Tanzania, United Republic Of 
                          43                           36 
                      Uganda                     Ethiopia 
                          36                           28 
                       Kenya                     Thailand 
                          21                           20 
                   Indonesia                    Nicaragua 
                          18                           18 
                       China                        India 
                          16                           14 
                      Malawi                         Peru 
                          11                            9 
                     Myanmar                      Vietnam 
                           8                            8 
                 El Salvador                        Haiti 
                           6                            6 
                 Philippines                       Panama 
                           5                            4 
                     Ecuador                      Burundi 
                           3                            2 
               Cote d?Ivoire                         Laos 
                           1                            1 
            Papua New Guinea                       Rwanda 
                           1                            1 
                      Taiwan                       Zambia 
                           1                            1 
#Getting the average score of the ratings to try to create a visualizations based on the score.
coffee_map <- coffee_ratings|>
  group_by(Location.Country)|>
  summarise(Average_Score = mean(Data.Scores.Total, na.rm = TRUE))
head(coffee_map)
# A tibble: 6 × 2
  Location.Country Average_Score
  <chr>                    <dbl>
1 Brazil                    83.0
2 Burundi                   81.8
3 China                     82.9
4 Colombia                  83.2
5 Costa Rica                82.6
6 Cote d?Ivoire             79.3
#I found out that similar to tableau we can create global maps in r, so with the help of chat gpt I create this he made download the library "countrycode" and basically this GeoJSON on Highcharts create a link between the name of the country in my data set with the geographic map trought a the 3 letters with every country is represented in the world matching them with "iso#c"
coffee_map <- coffee_map |>
  mutate(iso3 = countrycode(Location.Country,origin = "country.name",destination = "iso3c"))
coffee_map |>
  filter(is.na(iso3))
# A tibble: 0 × 3
# ℹ 3 variables: Location.Country <chr>, Average_Score <dbl>, iso3 <chr>
data(worldgeojson, package = "highcharter")
highchart() |>
  hc_add_series_map(
    map = worldgeojson,
    df = coffee_map,
    value = "Average_Score",
    joinBy = "iso3",
    name = "Average Coffee Rating") |>
  hc_title(text = "Average Coffee Ratings by Country")|>
  hc_colorAxis(minColor = "#FFBD46",maxColor = "#7e395c") |>
  hc_tooltip(pointFormat = "<b>{point.name}</b><br>Average Score: {point.value:.2f}") |>
  hc_mapNavigation(enabled = TRUE)
clean_coffee <- coffee_ratings%>%
  rename(Number_of_bags=`Data.Production.Number of bags`)
clean_coffee
# A tibble: 989 × 23
   Location.Country Location.Region  Location.Altitude.Min Location.Altitude.Max
   <chr>            <chr>                            <dbl>                 <dbl>
 1 United States    kona                                 0                     0
 2 Brazil           sul de minas - …                    12                    12
 3 Brazil           sul de minas - …                    12                    12
 4 Ethiopia         sidamo                               0                     0
 5 Ethiopia         sidamo                               0                     0
 6 United States    kona                                 0                     0
 7 Indonesia        dolok sanggul                     1300                  1400
 8 Ethiopia         kelem welega                         0                     0
 9 Ethiopia         limu                                 0                     0
10 Haiti            marmelade                          640                  1400
# ℹ 979 more rows
# ℹ 19 more variables: Location.Altitude.Average <dbl>, Year <dbl>,
#   Data.Owner <chr>, Data.Type.Species <chr>, Data.Type.Variety <chr>,
#   `Data.Type.Processing method` <chr>, Number_of_bags <dbl>,
#   `Data.Production.Bag weight` <dbl>, Data.Scores.Aroma <dbl>,
#   Data.Scores.Flavor <dbl>, Data.Scores.Aftertaste <dbl>,
#   Data.Scores.Acidity <dbl>, Data.Scores.Body <dbl>, …
clean_coffee <- clean_coffee%>%
  select(Data.Type.Variety,Location.Country,Data.Scores.Total,Number_of_bags)%>%
  filter(!is.na(Data.Type.Variety),!is.na(Location.Country),!is.na(Data.Scores.Total),!is.na(Number_of_bags))%>%
           group_by(Location.Country,Data.Type.Variety)%>%
  summarise(average_score=mean(Data.Scores.Total),average_bags=mean(Number_of_bags),.groups="drop")
clean_coffee
# A tibble: 116 × 4
   Location.Country Data.Type.Variety average_score average_bags
   <chr>            <chr>                     <dbl>        <dbl>
 1 Brazil           Arusha                     82.3         67.5
 2 Brazil           Bourbon                    83.9        192. 
 3 Brazil           Catuai                     83.0        157. 
 4 Brazil           Mundo Novo                 82.3        112. 
 5 Brazil           Yellow Bourbon             84          246. 
 6 Brazil           nan                        81.9        160. 
 7 Burundi          Bourbon                    83.3        220  
 8 Burundi          nan                        80.3        300  
 9 China            Catimor                    83.8          3  
10 China            Other                      82.5          1  
# ℹ 106 more rows
# I was trying to find a relation between the scores ratings of the coffee beans and the amount produced, however the chart prove me wrong. There is no relationship between score and bags produced 
ggplot(clean_coffee)+
  aes(x = average_bags,
    y = average_score,
    color = Data.Type.Variety) +
  geom_point(size = 2, alpha = 0.6) +
  geom_smooth(method = "lm",se = FALSE) +
  scale_color_brewer(palette = "Set2") +
  labs(title = "Relationship Between Coffee Quality and Production",
    subtitle = "Comparing total quality scores with the number of bags produced",
    x = "Total Coffee Quality Score",
    y = "Number of Bags Produced",
    color = "Coffee Variety",
    caption = "Source: Original coffee ratings dataset") +
  theme_minimal()
`geom_smooth()` using formula = 'y ~ x'
Warning in RColorBrewer::brewer.pal(n, pal): n too large, allowed maximum for palette Set2 is 8
Returning the palette you asked for with that many colors
Warning: Removed 73 rows containing missing values or values outside the scale range
(`geom_point()`).

#I wanted to know wich ones were the top 5 best ranked coffee beans 
top_5_coffee <- clean_coffee %>%
  arrange(desc(average_score)) %>%
  slice_head(n = 5)
top_5_coffee
# A tibble: 5 × 4
  Location.Country Data.Type.Variety     average_score average_bags
  <chr>            <chr>                         <dbl>        <dbl>
1 Peru             Other                          88.8         100 
2 Thailand         Caturra                        86.2          53 
3 Ethiopia         Other                          86.2         211.
4 Ethiopia         Ethiopian Yirgacheffe          86.0         250 
5 Panama           Gesha                          85.8          37 
#Here I wanted to know wich were the top 5 coffee producers
top_5_coffee_producers <- clean_coffee %>%
  arrange(desc(average_bags)) %>%
  slice_head(n = 5)
top_5_coffee_producers
# A tibble: 5 × 4
  Location.Country Data.Type.Variety average_score average_bags
  <chr>            <chr>                     <dbl>        <dbl>
1 Ethiopia         Caturra                    85.8         320 
2 Ethiopia         Gesha                      84.1         320 
3 India            Other                      82.0         320 
4 Uganda           Bourbon                    83.2         318.
5 Nicaragua        Caturra                    79.1         306.

Later I was trying to find a relation between the scores and production but I did not have enough time to finish my research.

But I can conclude that even if Ethiopia is not between the top 5 coffee producers in the world, the quality of their coffee beans is consistent with the bags produced. In addition, Peru appears as good quality beans producer (Top 5), but this information is not supported since the data set does not contain the name of the coffee variety.

India also appears in the top 5 producers countries, but checking the data set the rating scores are low. Which means that they produce big quantities of coffee, but not of the best quality.

Another interesting observation is Panama, which Geisha coffee beans have won several competitions, however there are not many observations of Panama. Also, that can be explained by the size of the country.