Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Plotting Yield Anomaly #2

Closed
Stanley997 opened this issue Jun 27, 2024 · 2 comments
Closed

Plotting Yield Anomaly #2

Stanley997 opened this issue Jun 27, 2024 · 2 comments

Comments

@Stanley997
Copy link

Stanley997 commented Jun 27, 2024

@codename5281

Hi team,

Thank you for your assistance for a successful test simulation.
My simulation was for a historical yield and future yield. Hence, I want to plot the mean of both yields as well as the percentage change between the two periods (yield anomaly).

Here is my, Script for the task. However, the percentage change in yield is showing a blank map, any suggestions? I will be glad for any input.

# Load historical raster data
historical_rasters <- list.files(pattern = "hotfix_20230329_hj_m2_d_CHIRPS_rdt_s0_Mil_120_F4.*tif$", full.names = TRUE)
historical_stack <- stack(historical_rasters)

# Calculate mean yield for historical period
mean_historical <- mean(historical_stack)
print(mean_historical)

# Set working directory for future period
setwd("C:\\Users\\User\\Pictures\\IPSL Yield data\\Future Yield\\rdt")

# Load future raster data
future_rasters <- list.files(pattern = "hotfix_20230329_hj_m2_d_CHIRPS_rdt_s0_Mil_120_F4.*tif$", full.names = TRUE)
future_yield <- stack(future_rasters)

# Calculate mean yield for future period
mean_future <- mean(future_yield)
print(mean_future)

# Compute percentage change
percentage_change <- ((mean_future - mean_historical) / mean_historical) * 100
print(percentage_change)

# Convert mean raster data to data frames
mean_historical_df <- as.data.frame(mean_historical, xy = TRUE)
mean_future_df <- as.data.frame(mean_future, xy = TRUE)
percentage_change_df <- as.data.frame(percentage_change, xy = TRUE)

# Plot mean yield for historical period with custom colors
ggplot() +
  geom_tile(data = mean_historical_df, aes(x = x, y = y, fill = layer)) +
  scale_fill_gradient(name = "Mean Yield", low = "white", high = "green") +
  labs(title = "Mean Yield - Historical Period") +
  theme_minimal() +
  coord_equal() +
  geom_sf(data = shapefile, fill = "transparent", color = "black", size = 0.5)

# Plot mean yield for future period with diverging colors
ggplot() +
  geom_tile(data = mean_future_df, aes(x = x, y = y, fill = layer)) +
  scale_fill_gradient2(name = "Mean Yield", low = "brown", mid = "white", high = "green", midpoint = mean(mean_future_df$layer, na.rm = TRUE)) +
  labs(title = "Mean Yield - Future Period") +
  theme_minimal() +
  coord_equal() +
  geom_sf(data = shapefile, fill = "transparent", color = "black", size = 0.5)

# Plot percentage change with multi-color gradient
ggplot() +
  geom_tile(data = percentage_change_df, aes(x = x, y = y, fill = layer)) +
  scale_fill_gradientn(name = "Percentage Change (%)", colors = c("white", "blue", "red"), labels = scales::percent_format(accuracy = 1)) +
  labs(title = "Percentage Change") +
  theme_minimal() +
  coord_equal() +
  geom_sf(data = shapefile, fill = "transparent", color = "black", size = 0.5)
@codename5281
Copy link
Contributor

Hello @Stanley997 ,

I am not (anymore) familiar with R so i won't be of any help for this post-processing step. For your troubleshooting, and if you can correctly open the yield geotiff files in QGis, i would advise you to give a try with ChatGPT, that will gladly help you find where your issue may be coming from.

Cheers,
J.

@Stanley997
Copy link
Author

@codename5281
Okay Jay,
Thank you for the info. I have done so, and I was able to find something

Cheers,
Stan.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants