Read in data

coffee_ratings <- readr::read_csv('https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2020/2020-07-07/coffee_ratings.csv')
## Parsed with column specification:
## cols(
##   .default = col_character(),
##   total_cup_points = col_double(),
##   number_of_bags = col_double(),
##   aroma = col_double(),
##   flavor = col_double(),
##   aftertaste = col_double(),
##   acidity = col_double(),
##   body = col_double(),
##   balance = col_double(),
##   uniformity = col_double(),
##   clean_cup = col_double(),
##   sweetness = col_double(),
##   cupper_points = col_double(),
##   moisture = col_double(),
##   category_one_defects = col_double(),
##   quakers = col_double(),
##   category_two_defects = col_double(),
##   altitude_low_meters = col_double(),
##   altitude_high_meters = col_double(),
##   altitude_mean_meters = col_double()
## )
## See spec(...) for full column specifications.

Set up country codes

coffee_ratings %>% 
  count(country_of_origin, sort = T)
## # A tibble: 37 x 2
##    country_of_origin                n
##    <chr>                        <int>
##  1 Mexico                         236
##  2 Colombia                       183
##  3 Guatemala                      181
##  4 Brazil                         132
##  5 Taiwan                          75
##  6 United States (Hawaii)          73
##  7 Honduras                        53
##  8 Costa Rica                      51
##  9 Ethiopia                        44
## 10 Tanzania, United Republic Of    40
## # ... with 27 more rows
coffee_ratings <- coffee_ratings %>% 
  mutate(country_code = countrycode::countrycode(coffee_ratings$country_of_origin, origin = "country.name", destination = "iso3c"),
         iso_a3 = country_code)
## Warning in countrycode::countrycode(coffee_ratings$country_of_origin, origin = "country.name", : Some values were not matched unambiguously: United States (Puerto Rico)
## Warning in countrycode::countrycode(coffee_ratings$country_of_origin, origin = "country.name", : Some strings were matched more than once, and therefore set to <NA> in the result: United States (Puerto Rico),PRI,USA

Set up mapping data

p_load(rnaturalearth, rnaturalearthdata, rgeos, ggthemes)

world <- ne_countries(scale = "medium", returnclass = "sf")
class(world)
## [1] "sf"         "data.frame"
coffee_ratings <- coffee_ratings %>% 
  group_by(country_code, species) %>% 
  mutate(mean_country_rating = mean(total_cup_points)) %>% 
  ungroup()

df <- inner_join(coffee_ratings, world, by = "iso_a3")
# jpeg(
#     filename="coffee_map.jpeg",
#     width=6,
#     height=6,
#     units="in",
#     res=1000)

ggplot(data = df) +
  borders() +
  geom_sf(aes(fill = mean_country_rating, geometry = geometry)) +
  scale_fill_viridis_c(trans = "sqrt") +
  theme(legend.position = "bottom") +
  coord_sf(ylim = c(-50, 80)) +
  facet_wrap(~ species, nrow = 2) +
  labs(title = "Average Coffee Rating",
       subtitle = "By country and species",
       fill = "Average Rating / 100",
       caption = "Data: James LeDoux - https://github.com/jldbc/coffee-quality-database",
       x = "",
       y = "")

# dev.off()