library(here)
library(readr)
library(tidyverse)
library(ggplot2)
library(scales)
options(scipen = 999)
knitr::opts_chunk$set(echo = TRUE, warning=FALSE, message=FALSE)Challenge 7
Challenge Overview
Today’s challenge is to:
- read in a data set, and describe the data set using both words and any supporting information (e.g., tables, etc)
- tidy data as needed.
- mutate variables as needed.
- Make at least two graphs using ggplot functionality (color, shape, line, facet, etc), in particular trying to use functionality that you haven’t used before.
- Explain why you choose the specific graph type.
- If you haven’t tried in previous weeks, work this week to make your graphs “publication” ready with titles, captions, and pretty axis labels and other viewer-friendly features.
Read in Data
birds <- read_csv(here("data", "birds.csv"))
head(birds)# 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… 5112 Stocks 1057
2 QA Live Anima… 2 Afgh… 5112 Stocks 1057
3 QA Live Anima… 2 Afgh… 5112 Stocks 1057
4 QA Live Anima… 2 Afgh… 5112 Stocks 1057
5 QA Live Anima… 2 Afgh… 5112 Stocks 1057
6 QA Live Anima… 2 Afgh… 5112 Stocks 1057
# ℹ 7 more variables: Item <chr>, `Year Code` <dbl>, Year <dbl>, Unit <chr>,
# Value <dbl>, Flag <chr>, `Flag Description` <chr>
Describe the Data
dim(birds)[1] 30977 14
colnames(birds) [1] "Domain Code" "Domain" "Area Code" "Area"
[5] "Element Code" "Element" "Item Code" "Item"
[9] "Year Code" "Year" "Unit" "Value"
[13] "Flag" "Flag Description"
unique(birds$Item)[1] "Chickens" "Ducks" "Geese and guinea fowls"
[4] "Turkeys" "Pigeons, other birds"
length(unique(birds$Area))[1] 248
range(birds$Year)[1] 1961 2018
This data set contains 30977 rows and 14 columns capturing the amount of stocks of birds and supporting characteristics. We can see there are 5 types of birds in the study being Chickens, Ducks, Geese and guinea fowls, Turkeys, and Pigeons and other. We can also see that there are 248 Areas that we have data for, which include countries, regions, continents and the world. Using the range function it shows that this data set contains values for the year from 1961 up to 2018.
Tidy the Data
This data has already come in fairly tidy but there are a lot of columns that are not useful in describing the data or do not change across rows that we can remove. The columns for Domain Code, Domain, Element Code, and Element are static across all data and they can be filtered out. Other columns such as Area Code and Item Code do not mean much without their textual description so they will be removed as well.
birds <- read_csv(here("data", "birds.csv"),
col_names = c("del", "del", "del", "area", "del", "del", "del",
"item", "del", "year", "unit", "value", "flag", "flag_description"),
skip = 1) %>%
select(!contains("del"))
head(birds)# A tibble: 6 × 7
area item year unit value flag flag_description
<chr> <chr> <dbl> <chr> <dbl> <chr> <chr>
1 Afghanistan Chickens 1961 1000 Head 4700 F FAO estimate
2 Afghanistan Chickens 1962 1000 Head 4900 F FAO estimate
3 Afghanistan Chickens 1963 1000 Head 5000 F FAO estimate
4 Afghanistan Chickens 1964 1000 Head 5300 F FAO estimate
5 Afghanistan Chickens 1965 1000 Head 5500 F FAO estimate
6 Afghanistan Chickens 1966 1000 Head 5800 F FAO estimate
Mutate the Data
We can also pivot the data wider so each row represents a country’s bird stocks for a single year. Mutating the data we can calculate the total about of bird stocks per country, per year.
birds_wide <- birds %>%
# Mutate data in preparation for friendly column names
mutate(item = case_when(
item == "Chickens" ~ "chickens",
item == "Turkeys" ~ "turkeys",
item == "Ducks" ~ "ducks",
item == "Geese and guinea fowls" ~ "geese_guinea_fowls",
item == "Pigeons, other birds" ~ "pigeons_other"
)) %>%
# Pivot wider to make new columns
pivot_wider(names_from = item, values_from = value)%>%
# Calculate the total for each row
mutate(total = rowSums(across(c(chickens, turkeys,ducks, geese_guinea_fowls, pigeons_other)), na.rm = TRUE))Visualizations
2018 Bird Stocks Across Area
One visualization we can make is to compare 2018 bird stocks across different areas. There are about 250 areas in this study so it is difficult to show all those in a comprehensible fashion. There are a lot of NA values in this data set however, so I am going to filter on areas that have a valid value for every type of bird stock in the study to hopefully give us a better visualization. Once we have filtered down the data we’ll just use the first 4 areas and stuff them into a vector so we can filter our graph further.
areas <- birds_wide %>%
# Filter on year and make sure we have values for each item type
filter(year == 2018 & (!is.na(chickens) & chickens >0) & (!is.na(ducks) & ducks > 0) & (!is.na(turkeys) & turkeys > 0) & (!is.na(geese_guinea_fowls) & geese_guinea_fowls >0) & (!is.na(pigeons_other) & pigeons_other > 0)) %>%
# Sort by amount of turkeys
arrange(desc(turkeys)) %>%
# # Select area column
select(area) %>%
# # Slice to omit "World"
slice(2:5) %>%
# # Pull data into vector
pull(area)Now that we have the sample size we want, we can create a bar graph of the different stock sizes. We will also use the facet_wrap function to create a separate bar graph per area and the particular stock will be color coded across area for easy comparison. Although we have filtered down the data to contain valid values for each stock, it seems the stock of “Chickens” is so large that other stocks are barely visible, if at all in this graph.
birds %>%
# Filter on year and areas of interest
filter(year == 2018 & area %in% areas) %>%
# Plot item vs value, adding color to different items
ggplot(aes(x = item, y = value, fill=item)) +
# Bar graph and group by area
geom_bar(stat="identity") + facet_wrap(vars(area)) +
# Add labels
labs(x = "Bird Product", y = "Stocks (in 1000 Heads)", title = "2018 Bird Stocks Across Regions", fill = "Bird Product") +
# Dark theme, center title, change legend color
theme_dark() + theme(plot.title = element_text(hjust = 0.5), axis.text.x = element_blank(), legend.key = element_rect(fill = "black"))Bird Stocks Over Time
We can also graph how global bird stocks have changed over time. The data comes with an area for “World” so we filter on this attribute and plot the value of particular bird stocks against time. We have also seen that the amount of chickens drastically outweighs all other bird stocks and makes things tough to visualize, so for the sake of visualization we will filter out all the chicken stocks. If we add color for the individual stocks, we are able to have separate paths for each on the same graph with a legend indicating color correspondence. From this we can see that ducks are the most dominant type of stock other than chickens and have roughly quintupled over the period of the study. Turkeys and geese saw steady increases in their stocks over the given time years, while the amount of pigeons seem to remain almost stagnant.
birds %>%
# Filter on world and not chickens
filter(area == "World" & item!="Chickens") %>%
# Plot year vs value, adding color to the item
ggplot(aes(year, value, col=item)) +
# Plot path
geom_path() +
# Scale x axis for years
scale_x_continuous(breaks=seq(1961, 2018,5)) +
# Scale y and add commas
scale_y_continuous(labels = comma) +
# Labels
labs(x = "Year", y = "Stocks (1000 Heads)", title= "Global Bird Stocks over Time", subtitle = "1961-2018", col = "Bird Product") +
# Center titles, and add themes
theme_linedraw() + theme(plot.title = element_text(hjust = 0.5),plot.subtitle = element_text(hjust = 0.5),
legend.key = element_rect(color = "black"))Chicken vs Duck Stocks
It would also be interesting to see if there is any relationship between the amount of chicken stocks and the amount of duck stocks. We can create a scatter plot that graphs that relationship. Adding the smoothed conditional mean this graph seems to indicate that there is a somewhat linear relationship between the 2, although the amount of chickens is much greater. It is also worth noting that roughly 2/3 of the data does not have valid values for both chicken and duck stocks so no definitive conclusions can be made from this graph.
birds_wide %>%
ggplot(aes(chickens, ducks)) +
geom_point(color="darkblue") + geom_smooth(color = "orange") +
scale_x_continuous(labels = comma) +
scale_y_continuous(labels = comma) +
labs(x = "Chickens (1000 Heads)", y = "Ducks (1000 Heads)", title = "Chicken vs Duck Stocks (All time)") +
theme_gray() + theme(plot.title = element_text(hjust = 0.5))