library(here)
library(readr)
library(tidyverse)
library(ggplot2)
library(scales)
knitr::opts_chunk$set(echo = TRUE, warning=FALSE, message=FALSE)Challenge 8
Challenge Overview
Today’s challenge is to:
- read in multiple data sets, and describe the data set using both words and any supporting information (e.g., tables, etc)
- tidy data as needed.
- mutate variables as needed.
- join two or more data sets and analyze some aspect of the joined data.
Read in data
Read in one (or more) of the following datasets, using the correct R package and command.
- faostat ⭐⭐
I will be reading in the 3 animal stock based data sets from the faostat group.
cattle <- read_csv(here("data", "FAOSTAT_cattle_dairy.csv"))
head(cattle)# A tibble: 6 × 14
`Domain Code` Domain `Area Code` Area `Element Code` Element `Item Code`
<chr> <chr> <dbl> <chr> <dbl> <chr> <dbl>
1 QL Livestock … 2 Afgh… 5318 Milk A… 882
2 QL Livestock … 2 Afgh… 5420 Yield 882
3 QL Livestock … 2 Afgh… 5510 Produc… 882
4 QL Livestock … 2 Afgh… 5318 Milk A… 882
5 QL Livestock … 2 Afgh… 5420 Yield 882
6 QL Livestock … 2 Afgh… 5510 Produc… 882
# ℹ 7 more variables: Item <chr>, `Year Code` <dbl>, Year <dbl>, Unit <chr>,
# Value <dbl>, Flag <chr>, `Flag Description` <chr>
eggs_chicken <- read_csv(here("data", "FAOSTAT_egg_chicken.csv"))
head(eggs_chicken)# A tibble: 6 × 14
`Domain Code` Domain `Area Code` Area `Element Code` Element `Item Code`
<chr> <chr> <dbl> <chr> <dbl> <chr> <dbl>
1 QL Livestock … 2 Afgh… 5313 Laying 1062
2 QL Livestock … 2 Afgh… 5410 Yield 1062
3 QL Livestock … 2 Afgh… 5510 Produc… 1062
4 QL Livestock … 2 Afgh… 5313 Laying 1062
5 QL Livestock … 2 Afgh… 5410 Yield 1062
6 QL Livestock … 2 Afgh… 5510 Produc… 1062
# ℹ 7 more variables: Item <chr>, `Year Code` <dbl>, Year <dbl>, Unit <chr>,
# Value <dbl>, Flag <chr>, `Flag Description` <chr>
livestock <- read_csv(here("data", "FAOSTAT_livestock.csv"))
head(livestock)# A tibble: 6 × 14
`Domain Code` Domain `Area Code` Area `Element Code` Element `Item Code`
<chr> <chr> <dbl> <chr> <dbl> <chr> <dbl>
1 QA Live Anima… 2 Afgh… 5111 Stocks 1107
2 QA Live Anima… 2 Afgh… 5111 Stocks 1107
3 QA Live Anima… 2 Afgh… 5111 Stocks 1107
4 QA Live Anima… 2 Afgh… 5111 Stocks 1107
5 QA Live Anima… 2 Afgh… 5111 Stocks 1107
6 QA Live Anima… 2 Afgh… 5111 Stocks 1107
# ℹ 7 more variables: Item <chr>, `Year Code` <dbl>, Year <dbl>, Unit <chr>,
# Value <dbl>, Flag <chr>, `Flag Description` <chr>
Describe the data
colnames(cattle) [1] "Domain Code" "Domain" "Area Code" "Area"
[5] "Element Code" "Element" "Item Code" "Item"
[9] "Year Code" "Year" "Unit" "Value"
[13] "Flag" "Flag Description"
colnames(eggs_chicken) [1] "Domain Code" "Domain" "Area Code" "Area"
[5] "Element Code" "Element" "Item Code" "Item"
[9] "Year Code" "Year" "Unit" "Value"
[13] "Flag" "Flag Description"
colnames(livestock) [1] "Domain Code" "Domain" "Area Code" "Area"
[5] "Element Code" "Element" "Item Code" "Item"
[9] "Year Code" "Year" "Unit" "Value"
[13] "Flag" "Flag Description"
range(cattle$Year)[1] 1961 2018
length(unique(eggs_chicken$Area))[1] 245
unique(livestock$Item)[1] "Asses" "Camels" "Cattle" "Goats" "Horses" "Mules"
[7] "Sheep" "Buffaloes" "Pigs"
We can see that all 3 data sets have the exact same columns. They each capture the measurements of certain types of animals and the amount of goods they produce. There are descriptions for various stocks from 1961 to 2018 with data representing 245 different areas around the world.
Tidy
These 3 data sets come in fairly tidy. We can see that each of these data frames all have matching columns and data types. This should make joining very easy. I am going to mutate the data frames to create a new column indicating the source of the data for ease of classification after we merge the data. I also noticed that the eggs_chicken data frame tracks the number of chickens in 1000s of heads, while the other data frames just use heads. We can mutate the unit and value column here to match the other data sets.
cattle_tidy <- cattle %>%
mutate(class = "cattle_dairy")
eggs_chicken_tidy <- eggs_chicken %>%
mutate(class = "eggs_chicken",
Value = case_when(
Unit == "1000 Head" ~ Value * 1000,
TRUE ~ Value
),
Unit = str_remove(Unit, "1000 "))
livestock_tidy <- livestock %>%
mutate(class = "livestock")Join
The join becomes very simple, we are going to use a full_join to merge all the data from all data frames and more or less stack the rows on top of another. We can see that after the join we have the same number of columns and the total number of rows should be the sum of the 3 original data frames.
joined_fao <- full_join(cattle_tidy, eggs_chicken_tidy)
joined_fao <- full_join(joined_fao, livestock_tidy)
colnames(joined_fao) [1] "Domain Code" "Domain" "Area Code" "Area"
[5] "Element Code" "Element" "Item Code" "Item"
[9] "Year Code" "Year" "Unit" "Value"
[13] "Flag" "Flag Description" "class"
dim(joined_fao)[1] 156735 15
Compare Classes of Animal Stocks
Now that we have all the data in one data frame we cn make some comparisons across them. One interesting comparison is how do the amount of animals from the different data sets match up against each other at a global scale. Given that the cattle_dairy and egg_chicken data also measure production and yield, we will filter out those rows so we are only counting the heads or number of animals. Then grouping by year and ‘class’ of animal, we can get the average number of animal stocks across all countries per year. If we graph this data we can see that the number of chickens is the clear majority population and has been steadily increasing over time with a small dip around 1990. The average number of dairy cows and all other livestock have interestingly remained fairly constant over the past 60 years.
#Group by year
#Global averages for milk animals, chicken, livestock
joined_fao %>%
filter(Element %in% c('Milk Animals', 'Laying', 'Stocks')) %>%
group_by(Year, class) %>%
summarise(mean = mean(Value, na.rm = TRUE)) %>%
ungroup() %>%
ggplot(aes(Year, mean, fill = class)) + geom_bar(stat="identity") +
scale_y_continuous(labels = comma) +
labs(x = "Year", y= "Average number of animals", title = "Average Animal Stocks per Year")+
theme(plot.title = element_text(hjust = 0.5))