knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  echo = TRUE,
  message = FALSE,
  warning = FALSE
)
excluded_crops <- c("Common bean", "Groundnut", "Cowpea", "Soybean", 
                    "Bush Bean", "Climbing Bean", "Faba Bean", "Green grams", "Chickpea","Common Bean",
                    "Bambara")

# Filter dataset to exclude selected crops
filtered_data <- dataset %>%
  filter(!Crop %in% excluded_crops) %>%
  mutate(
    longitude = as.numeric(longitude),
    latitude = as.numeric(latitude)
  ) %>%
  filter(!is.na(longitude) & !is.na(latitude))  # Ensure valid coordinates

# Count unique DOIs for each crop
crop_study_counts <- filtered_data %>%
  group_by(Crop) %>%
  summarise(Study_Count = n_distinct(doi)) %>%
  ungroup()

# Generate crop labels with study count
filtered_data <- filtered_data %>%
  left_join(crop_study_counts, by = "Crop") %>%
  mutate(Crop_Label = paste0(Crop, " (", Study_Count, ")"))  # Format as "Crop (Count)"

# Convert to spatial data
filtered_data_sf <- st_as_sf(filtered_data, coords = c("longitude", "latitude"), crs = 4326)

# Define a **bold & vibrant** color palette
crop_colors <- c(
  "Bambara" = "red",
  "Cassava" = "blue",
  "Cocoyam" = "limegreen",
  "Common Bean" = "darkorange",
  "Cooking Banana" = "purple",
  "Cotton" = "pink",
  "Maize" = "cyan",
  "Millet" = "gold",
  "Pearl Millet" = "magenta",
  "Pineapple" = "black",
  "Potato" = "darkred",
  "Rice" = "yellow",
  "Sorghum" = "navy",
  "Sugercane" = "coral",
  "Sunflower" = "mediumvioletred",
  "Sweet Banana" = "dodgerblue",
  "Sweet Potato" = "deeppink",
  "Wheat" = "green",
  "Yam" = "turquoise"
)

# Load map data for Africa
africa <- ne_countries(scale = "medium", continent = "Africa", returnclass = "sf")

# Create the map
ggplot() +
  geom_sf(data = africa, fill = "gray95", color = "gray80") +  # Base map
  geom_sf(data = filtered_data_sf, aes(color = Crop), size = 2, alpha = 0.8) +  # Crop points
  scale_color_manual(values = crop_colors) +
  labs(
    title = "Study Locations by Crop",
    color = "Crop"
  ) +
  theme_minimal() +
  theme(
    legend.position = "bottom",
    plot.title = element_text(hjust = 0.5, size = 14, face = "bold"),
    legend.title = element_text(size = 12),
    legend.text = element_text(size = 10)
  )


# Sort the data from highest to lowest and keep only the top 5
top_crops <- crop_study_counts %>%
  arrange(desc(Study_Count)) %>%
  head(5)

# Display the table using knitr::kable
kable(top_crops, caption = "Top 5 crops with the most papers")
Top 5 crops with the most papers
Crop Study_Count
Maize 53
Wheat 17
Sorghum 16
Rice 13
Cassava 6
# Filter dataset to exclude unwanted crops
filtered_data <- dataset %>%
  filter(!Crop %in% excluded_crops) %>%   # Exclude unwanted crops
  filter(!is.na(`Climate impacts (%)`))   # Ensure 'Climate impacts (%)' is not NA

# Count observations for each crop
filtered_data <- filtered_data %>%
  group_by(Crop) %>%
  mutate(Count = n()) %>%
  ungroup()

# Create the boxplot
ggplot(filtered_data, aes(x = `Climate impacts (%)`, y = reorder(Crop, Count, .desc = TRUE))) +
  geom_boxplot(outlier.size = 1, outlier.color = "red") +
  geom_vline(xintercept = 0, color = "red", linetype = "dashed") +
  labs(
    x = "Climate Impact (%)",
    y = "Crop (n)",
    title = "Climate Impact (%) on yield of Selected Crops"
  ) +
  theme_minimal(base_size = 14) +
  theme(
    axis.title.y = element_text(size = 12),
    axis.title.x = element_text(size = 12),
    axis.text.y = element_text(size = 10)
  )

# Filter data for specific crops and climate scenarios
filtered_data <- dataset %>%
  filter(!Crop %in% excluded_crops) %>%
  filter(!is.na(`Climate impacts (%)`)) %>%  # Ensure 'Climate impacts (%)' is not NA
  filter(`Climate scenario` %in% c("RCP8.5", "RCP4.5", "RCP2.6", "SSP370", "SSP126", "SSP585"))  # Include only specified scenarios

# Count observations for each climate scenario
filtered_data <- filtered_data %>%
  group_by(`Climate scenario`) %>%
  mutate(Count = n()) %>%
  ungroup()

# Create the boxplot
ggplot(filtered_data, aes(x = `Climate impacts (%)`, y = reorder(`Climate scenario`, Count, .desc = TRUE))) +
  geom_boxplot(outlier.size = 1, outlier.color = "red") +
  geom_vline(xintercept = 0, color = "red", linetype = "dashed") +
  labs(
    x = "Climate Impact (%)",
    y = "Climate Scenario",
    title = "Climate Impact (%) on yield for different Climate Scenarios"
  ) +
  theme_minimal(base_size = 14) +
  theme(
    axis.title.y = element_text(size = 12),
    axis.title.x = element_text(size = 12),
    axis.text.y = element_text(size = 10)
  )

# Filter data for specific crops
filtered_data <- dataset %>%
  filter(!Crop %in% excluded_crops) %>%
  filter(!`Adaptation type` %in% "Yield Potential") %>%
  filter(!is.na(`Climate impacts (%)`))  # Ensure 'Climate impacts (%)' is not NA

# Count observations for each crop
filtered_data <- filtered_data %>%
  group_by(`Adaptation type`) %>%
  mutate(Count = n()) %>%
  ungroup()

# Create the boxplot
ggplot(filtered_data, aes(x = `Climate impacts (%)`, y = reorder(`Adaptation type`, Count, .desc = TRUE))) +
  geom_boxplot(outlier.size = 1, outlier.color = "red") +
  geom_vline(xintercept = 0, color = "red", linetype = "dashed") +
  labs(
    x = "Climate Impact (%)",
    y = "Adaptation type",
    title = "Climate Impact (%) on yield with different adaptation strategies"
  ) +
  theme_minimal(base_size = 14) +
  theme(
    axis.title.y = element_text(size = 12),
    axis.title.x = element_text(size = 12),
    axis.text.y = element_text(size = 10)
  )

The treatment vs. control approach greatly reduces the number of studies included (7), as most focus on a single adaptation strategy rather than projecting both scenarios. This script filters the dataset for selected crops, separating “no adaptation” (control) and specific adaptation strategies (treatment). The two groups are merged based on shared attributes like site, crop, and climate scenario, allowing for direct comparisons.


# Filter dataset to exclude unwanted crops
filtered_data <- dataset %>%
  filter(!Crop %in% excluded_crops) %>%
  filter(!`Adaptation type` %in% "Yield Potential")

# Create control dataset
control_data <- filtered_data %>%
  filter(`Adaptation type` == "No") %>%
  rename(YieldChange_Control = `Climate impacts (%)`)

# Create treatment dataset
treatment_data <- filtered_data %>%
  filter(`Adaptation type` != "No") %>%
  rename(YieldChange_Treatment = `Climate impacts (%)`)

# Merge control and treatment datasets
merged_data <- merge(
  control_data,
  treatment_data,
  by = c("Ref No", "Future_Mid-point", "Climate scenario",  
         "Site(location)", "Crop","GCM/RCM","Crop Model"),
  all = FALSE
)

# Transform the data for ridge plots
plot_data <- merged_data %>%
  pivot_longer(
    cols = c(YieldChange_Control, YieldChange_Treatment),
    names_to = "Management_Type",
    values_to = "YieldChange"
  ) %>%
  mutate(
    Management_Group = case_when(
      Management_Type == "YieldChange_Control" ~ "No Adaptation",
      TRUE ~ `Adaptation type.y`
    )
  )

# Define **brighter & clearer** color palette
crop_colors <- c(
  "Maize" = "#377EB8",
  "Millet" = "#4DAF4A",
  "Rice" = "#FF7F00",
  "Sorghum" = "#984EA3",
  "Sunflower" = "#FFFF33",
  "Wheat" = "#E41A1C"
)



# Improved Ridge Plot without Dots & with Tick Marks
ggplot(plot_data, aes(
  x = YieldChange,
  y = reorder(Management_Group, YieldChange, median),
  fill = Crop
)) +
  geom_density_ridges(
    scale = 1.5, alpha = 0.7, rel_min_height = 0.01,
    quantile_lines = TRUE, quantiles = 2  # Add median tick marks
  ) +
  scale_fill_manual(values = crop_colors) +  # Use defined colors
  labs(
    title = "Yield Change Distribution by Management Strategy- Trt vs Control",
    x = "Yield Change (%)",
    y = "Management Strategy",
    fill = "Crop"
  ) +
  theme_minimal() +
  theme(
    axis.text.y = element_text(size = 12, color = "black"),  # Improve label readability
    axis.title.y = element_text(size = 12, face = "bold"),
    axis.title.x = element_text(size = 12, face = "bold"),
    plot.title = element_text(size = 14, face = "bold", hjust = 0.5),
    legend.position = "right",
    legend.text = element_text(size = 12)
  )


crop_study_trt_control <- merged_data %>%
  group_by(Crop) %>%
  summarise(Study_Count = n_distinct(doi.x)) %>%
  ungroup()


# Display the table using knitr::kable
kable(crop_study_trt_control, caption = "Number of ppaers used for this plot")
Number of ppaers used for this plot
Crop Study_Count
Maize 18
Millet 2
Rice 3
Sorghum 3
Sunflower 1
Wheat 7

OR A BOX PLOT TO SHOW THE SAME

# Filter dataset to exclude unwanted crops
filtered_data <- dataset %>%
  filter(!Crop %in% excluded_crops) %>%
  filter(!`Adaptation type` %in% "Yield Potential")

# Create control dataset
control_data <- filtered_data %>%
  filter(`Adaptation type` == "No") %>%
  rename(YieldChange_Control = `Climate impacts (%)`)

# Create treatment dataset
treatment_data <- filtered_data %>%
  filter(`Adaptation type` != "No") %>%
  rename(YieldChange_Treatment = `Climate impacts (%)`)

# Merge control and treatment datasets
merged_data <- merge(
  control_data,
  treatment_data,
  by = c("Ref No", "Future_Mid-point", "Climate scenario",  
         "Site(location)", "Crop","GCM/RCM","Crop Model"),
  all = FALSE
)

# Transform the data for box plot
plot_data <- merged_data %>%
  pivot_longer(
    cols = c(YieldChange_Control, YieldChange_Treatment),
    names_to = "Management_Type",
    values_to = "YieldChange"
  ) %>%
  mutate(
    Management_Group = case_when(
      Management_Type == "YieldChange_Control" ~ "No Adaptation",
      TRUE ~ `Adaptation type.y`
    )
  )

# Define **brighter & clearer** color palette
crop_colors <- c(
  "Maize" = "#377EB8",
  "Millet" = "#4DAF4A",
  "Rice" = "#FF7F00",
  "Sorghum" = "#984EA3",
  "Sunflower" = "#FFFF33",
  "Wheat" = "#E41A1C"
)

# Create **Box Plot with Crop Dots**
ggplot(plot_data, aes(
  x = YieldChange,
  y = reorder(Management_Group, YieldChange, median),
  fill = Management_Group
)) +
  geom_boxplot(
    alpha = 0.5, outlier.shape = 16, outlier.size = 1.5, 
    aes(fill = NULL), width = 0.6  # Light transparency, smaller width
  ) +
  labs(
    title = "Yield Change Distribution by Management Strategy -trt vs control",
    x = "Yield Change (%)",
    y = "Management Strategy"
  ) +
  theme_minimal() +
  theme(
    axis.text.y = element_text(size = 12, color = "black"),  
    axis.title.y = element_text(size = 12, face = "bold"),
    axis.title.x = element_text(size = 12, face = "bold"),
    plot.title = element_text(size = 14, face = "bold", hjust = 0.5),
    legend.position = "none"  # Completely remove the legend
  )