This challenge is about using purrr to any analysis task. We use the function created in Challenge 9.
We first load the libraries
library(readr)
library(here)
## here() starts at C:/Users/SHAURYA/Desktop/Studies/Winter 2024 601/Challenges/challenge 10
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(ggplot2)
library(purrr)
The dataset is then loaded.
weight <- read_csv("animal_weight.csv", show_col_types = FALSE)
weight
## # A tibble: 9 × 17
## `IPCC Area` `Cattle - dairy` `Cattle - non-dairy` Buffaloes `Swine - market`
## <chr> <dbl> <dbl> <dbl> <dbl>
## 1 Indian Subco… 275 110 295 28
## 2 Eastern Euro… 550 391 380 50
## 3 Africa 275 173 380 28
## 4 Oceania 500 330 380 45
## 5 Western Euro… 600 420 380 50
## 6 Latin America 400 305 380 28
## 7 Asia 350 391 380 50
## 8 Middle east 275 173 380 28
## 9 Northern Ame… 604 389 380 46
## # ℹ 12 more variables: `Swine - breeding` <dbl>, `Chicken - Broilers` <dbl>,
## # `Chicken - Layers` <dbl>, Ducks <dbl>, Turkeys <dbl>, Sheep <dbl>,
## # Goats <dbl>, Horses <dbl>, Asses <dbl>, Mules <dbl>, Camels <dbl>,
## # Llamas <dbl>
We see that the dataset as Area followed by different categories of various animals.
This function reads and cleans the data which is followed by a histogram. The final data is then returned by the function.
func <- function(data, category) {
cols <- c("IPCC Area", category)
new_data <- data[, cols]
# Clean Data
new_data[[category]] <- as.numeric(new_data[[category]])
# Remove missing values if there
new_data <- na.omit(new_data)
# The plot
ggplot(new_data, aes(x = `IPCC Area`, y = new_data[[category]])) +
geom_bar(stat = "identity", fill = "skyblue", color = "black", alpha = 0.7) +
labs(title = paste("Weight of ", category),
x = "IPCC Area", y = category) +
theme_minimal() +
theme(axis.text.x = element_text(angle = 45, hjust = 1))
}
categories <- c("Cattle - dairy", "Cattle - non-dairy", "Buffaloes", "Swine - market", "Swine - breeding", "Chicken - Broilers", "Chicken - Layers")
plots <- map(categories, ~func(weight, .))
walk(plots, print)
## Warning: Use of `new_data[[category]]` is discouraged.
## ℹ Use `.data[[category]]` instead.
## Warning: Use of `new_data[[category]]` is discouraged.
## ℹ Use `.data[[category]]` instead.
## Warning: Use of `new_data[[category]]` is discouraged.
## ℹ Use `.data[[category]]` instead.
## Warning: Use of `new_data[[category]]` is discouraged.
## ℹ Use `.data[[category]]` instead.
## Warning: Use of `new_data[[category]]` is discouraged.
## ℹ Use `.data[[category]]` instead.
## Warning: Use of `new_data[[category]]` is discouraged.
## ℹ Use `.data[[category]]` instead.
## Warning: Use of `new_data[[category]]` is discouraged.
## ℹ Use `.data[[category]]` instead.
The above function with purrr plots multiple graphs. Each graph shows the weight of a certain animal category across the areas. map function is used to apply the function to multiple categories. The walk function then prints all these plots.
We modified the function created in Challenge 9 with purrr to apply it over multiple categories.