## ----include = FALSE----------------------------------------------------------
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)

## ----setup--------------------------------------------------------------------
library(agridatasets)
library(dplyr)
library(ggplot2)

## ----agridatasets-datasets,echo = TRUE,message = FALSE,warning = FALSE,results = 'markup'----


view_datasets_agridatasets()



## ----bamboo-growth-plot, fig.width=6, fig.height=4, out.width="100%"----------
# Summarize average shoot counts by Compartment using base R + dplyr
summary_data <- bamboo_growth %>%
  dplyr::group_by(Compartment) %>%
  dplyr::summarise(
    Old_Shoots = mean(Old_Shoots, na.rm = TRUE),
    New_Shoots = mean(New_Shoots, na.rm = TRUE)
  ) %>%
  as.data.frame() %>%
  reshape(
    varying = c("Old_Shoots", "New_Shoots"),
    v.names = "Value",
    timevar = "Shoot_Type",
    times = c("Old_Shoots", "New_Shoots"),
    direction = "long"
  ) %>%
  dplyr::select(Compartment, Shoot_Type, Value)

# Create a grouped bar chart
ggplot(summary_data, aes(x = factor(Compartment), y = Value, fill = Shoot_Type)) +
  geom_col(position = "dodge", color = "white") +
  scale_fill_manual(values = c("Old_Shoots" = "lightblue", "New_Shoots" = "darkred")) +
  labs(
    title = "Average Old vs New Bamboo Shoots by Compartment",
    x = "Compartment",
    y = "Average Number of Shoots",
    fill = "Shoot Type"
  ) +
  theme_minimal() +
  theme(axis.text.x = element_text(angle = 45, hjust = 1))

## ----rice-wheat-yield-plot, fig.width=7, fig.height=4, out.width="100%"-------
# Extract the starting year as numeric for correct chronological ordering
plot_data <- rice_wheat_production %>%
  dplyr::mutate(
    Year_num = as.numeric(substr(as.character(Year), 1, 4))
  ) %>%
  dplyr::arrange(Year_num)

# Create a line plot comparing Yield trends for Rice and Wheat
ggplot2::ggplot(plot_data, ggplot2::aes(x = Year_num, y = Yield, color = Food)) +
  ggplot2::geom_line(linewidth = 1) +
  ggplot2::geom_point(size = 1.5, alpha = 0.7) +
  ggplot2::scale_color_manual(values = c("Rice" = "darkgreen", "Wheat" = "goldenrod")) +
  ggplot2::labs(
    title = "Rice vs Wheat Yield Over Time",
    x = "Year",
    y = "Yield (kg/hectare)",
    color = "Crop"
  ) +
  ggplot2::theme_minimal() +
  ggplot2::theme(axis.text.x = ggplot2::element_text(angle = 45, hjust = 1))

## ----cattle-butterfat-plot, fig.width=7, fig.height=4, out.width="100%"-------
# Create a boxplot comparing Butterfat content across Breed, split by Age
ggplot2::ggplot(cattle_butterfat, ggplot2::aes(x = Breed, y = Butterfat, fill = Age)) +
  ggplot2::geom_boxplot(outlier.color = "black", outlier.size = 1.5) +
  ggplot2::scale_fill_manual(values = c("2year" = "lightblue", "Mature" = "darkred")) +
  ggplot2::labs(
    title = "Butterfat Content by Cattle Breed and Age",
    x = "Breed",
    y = "Butterfat (%)",
    fill = "Age"
  ) +
  ggplot2::theme_minimal() +
  ggplot2::theme(axis.text.x = ggplot2::element_text(angle = 45, hjust = 1))

