knitr::opts_chunk$set(echo = TRUE, warning = FALSE, message = FALSE)
library(tidyverse)
library(viridis)
library(colorspace)
Data comes from TidyTuesday’s Github
key_crop_yields <- readr::read_csv('https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2020/2020-09-01/key_crop_yields.csv') %>%
janitor::clean_names()
# dplyr::glimpse(key_crop_yields)
# extract map data
map_df <- map_data("world")
# dplyr::glimpse(map_df)
# join map data with yield data
map_crops <- left_join(
map_df, key_crop_yields, by = c("region" = "entity"))
map_crops %>%
filter(year == 1998) %>%
ggplot(aes(long, lat, group = group,
fill = wheat_tonnes_per_hectare)) +
geom_polygon(color = "white") +
coord_equal() +
theme_void() +
scale_fill_viridis() +
ggtitle("Wheat Yield per hectare by Country in 1998") +
labs(fill = "wheat yield")
# 2018
map_crops %>%
filter(year == 2018, na.rm = TRUE) %>%
ggplot(aes(x = long, y = lat, group = group,
fill = wheat_tonnes_per_hectare)) +
geom_polygon(color = "white") +
coord_equal() +
theme_void() +
scale_fill_viridis() +
ggtitle("Wheat Yield by Country in 2018") +
labs(fill = "wheat yield")
# which countries have greatest crop yields per hectare (all types)
map_crops_long <- map_crops %>%
filter(year == 2018) %>%
pivot_longer(cols = wheat_tonnes_per_hectare:bananas_tonnes_per_hectare,
names_to = "type",
values_to = "hectare") %>%
group_by(region) %>%
mutate(total_hectare = sum(hectare, na.rm = TRUE))
# plot map; with sequential fill; according to total crop yields per hectare
map_crops_long %>%
ggplot(aes(long, lat, group = group,
fill = total_hectare)) +
geom_polygon(color = "white") +
coord_equal() +
theme_void() +
scale_fill_continuous_sequential(palette = "Dark Mint") +
ggtitle("2018 World Crop Capital! Crop yield per hectare (all types) in all countries")