climateR icon indicating copy to clipboard operation
climateR copied to clipboard

Question and Help: On How to Plot Monthly Data into Yearly Data In Tempreture change Map using getTerra climate

Open Heed725 opened this issue 1 year ago • 10 comments

Hello @mikejohnson51 and @Rapsodia86 hopefully you guys are doing well and alright, I have three chunk of codes for mean,yearly and monthly for climateR they seemed to work about couple of year ago but now they don't seem to work any more i'll share the codes

Mean Monthly yearly

But mostly i'm getting masking error ,could you help me out on updating or figuring out so it can work again

Heed725 avatar Mar 17 '24 08:03 Heed725

library(climateR)
library(terra)
library(tidyterra)
library(ggplot2)

SAU = AOI::aoi_get(country = "SAU")

test_data = getTerraClim(
  AOI = SAU,
  varname = "tmax",
  startDate = "2011-01-01",
  endDate   = "2012-12-01"
)

data = tapp(test_data[[1]],
            rep(1:12, (nlyr(test_data[[1]]) / 12)),
            mean) |>
  mask(project(vect(SAU), crs(test_data[[1]])))

ggplot() +
  geom_spatraster(data = data) +
  geom_spatvector(data = SAU, fill = NA, lwd = 1) +
  facet_wrap( ~ lyr) +
  scale_fill_whitebox_c(
    palette  = "muted",
    n.breaks = 12,
    guide    = guide_legend(reverse = TRUE)
  ) +
  theme_minimal()

Created on 2024-03-17 with reprex v2.0.2

mikejohnson51 avatar Mar 17 '24 22:03 mikejohnson51

Okay thank you much very this is cool visual but I have few 3 questions.

1.The above is cool but instead of x1 ,x2, x3 how can I label like January, February , march etc.? 2.The above is for monthly basis which is cool how will code appear in yearly basis? like it to appear two maps of only 2011 and 2012. 3.How does code appear like in one mean average temperature ? like one big visual showing mean Temperature of 2011 and 2012 ?

Thats all I think but thank you very much for this one

Heed725 avatar Mar 18 '24 02:03 Heed725

Two small adjustments should get you that!

library(climateR)
library(terra)
library(tidyterra)
library(ggplot2)

SAU = AOI::aoi_get(country = "SAU")

test_data = getTerraClim(
  AOI = SAU,
  varname = "tmax",
  startDate = "2011-01-01",
  endDate   = "2012-12-01"
)

data = tapp(test_data[[1]],
            rep(1:(nlyr(test_data[[1]]) / 12), 12),
            mean) |>
  mask(project(vect(SAU), crs(test_data[[1]])))

names(data) = c("Y2011", "Y2012")

ggplot() +
  geom_spatraster(data = data) +
  geom_spatvector(data = SAU, fill = NA, lwd = 1) +
  facet_wrap( ~ lyr) +
  scale_fill_whitebox_c(
    palette  = "muted",
    n.breaks = 12,
    guide    = guide_legend(reverse = TRUE)
  ) +
  theme_minimal()

Created on 2024-03-18 with reprex v2.0.2

mikejohnson51 avatar Mar 18 '24 14:03 mikejohnson51

One last qn like how do you create mean day temperature combining 2011 and 201220240318_200939.jpg

Something like that

Heed725 avatar Mar 18 '24 17:03 Heed725

Maybe something like:


data = mask(mean(test_data[[1]]), project(vect(SAU), crs(test_data[[1]])))

ggplot() +
  geom_spatraster(data = data) +
  geom_spatvector(data = SAU, fill = NA, lwd = 1) +
  scale_fill_whitebox_c(
    palette  = "muted",
    n.breaks = 12,
    guide    = guide_legend(reverse = TRUE)
  ) +
  labs(title = "Mean Daily Max Temp") + 
  theme_minimal()


mikejohnson51 avatar Mar 18 '24 17:03 mikejohnson51

Hi @mikejohnson51, I wanted to answer but looks like I am not able to get the data downloaded. It just runs and never finishes. I tried on this example and on #93

system.time({
  cdat = terra::ext(c(-98.90219, -65.21105, 28.12502, 50.43962)) |>
    vect(crs = '+proj=longlat +ellps=WGS84 +no_defs') |>
    getTerraClimNormals(
      varname = "tmin",
      scenario = "19812010",
      month = 5:9)
})

Here is the session info:

sessionInfo()
R version 4.2.2 (2022-10-31 ucrt)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 19045)

Matrix products: default

locale:
[1] LC_COLLATE=English_United States.utf8 
[2] LC_CTYPE=English_United States.utf8   
[3] LC_MONETARY=English_United States.utf8
[4] LC_NUMERIC=C                          
[5] LC_TIME=English_United States.utf8    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] terra_1.7-74   climateR_0.3.4

loaded via a namespace (and not attached):
 [1] Rcpp_1.0.12         pillar_1.9.0        compiler_4.2.2     
 [4] viridis_0.6.5       tools_4.2.2         digest_0.6.35      
 [7] bit_4.0.5           lifecycle_1.0.4     tibble_3.2.1       
[10] gtable_0.3.4        viridisLite_0.4.2   RNetCDF_2.9-1      
[13] pkgconfig_2.0.3     rlang_1.1.3         cli_3.6.2          
[16] rstudioapi_0.15.0   parallel_4.2.2      ncmeta_0.3.6       
[19] gridExtra_2.3       dplyr_1.1.4         globals_0.16.3     
[22] generics_0.1.3      vctrs_0.6.5         hms_1.1.3          
[25] bit64_4.0.5         grid_4.2.2          tidyselect_1.2.1   
[28] glue_1.7.0          listenv_0.9.1       R6_2.5.1           
[31] gifski_1.12.0-2     parallelly_1.37.1   future.apply_1.11.1
[34] fansi_1.0.6         ggplot2_3.4.4       readr_2.1.5        
[37] tzdb_0.4.0          purrr_1.0.2         magrittr_2.0.3     
[40] scales_1.3.0        codetools_0.2-18    assertthat_0.2.1   
[43] future_1.33.1       colorspace_2.1-0    arrow_15.0.1       
[46] utf8_1.2.4          munsell_0.5.0  

Maybe I need some updates? Thanks!

Rapsodia86 avatar Mar 18 '24 17:03 Rapsodia86

@mikejohnson51 Hey I think for me I'm done ,Thank you so so much for the great help....I'm really grateful for that.....I guess I'll see you when you update climateR again :).

Heed725 avatar Mar 19 '24 09:03 Heed725

I'm Back again with one last tiny question ? I altered the code a bit let me show you

library(climateR)
library(terra)
library(tidyterra)
library(ggplot2)
library(colorspace)

SAU = AOI::aoi_get(country = "SAU")

test_data = getTerraClim(
  AOI = SAU,
  varname = "tmax",
  startDate = "2011-01-01",
  endDate   = "2012-12-01"
)

data = tapp(test_data[[1]],
            rep(1:(nlyr(test_data[[1]]) / 12), 12),
            mean) |>
  mask(project(vect(SAU), crs(test_data[[1]])))

names(data) = c("2011", "2012")

ggplot() +
  geom_spatraster(data = data) +
  geom_spatvector(data = SAU, fill = NA, lwd = 1) + # Set color to "black"
  facet_wrap( ~ lyr) +
  scale_fill_continuous_sequential(palette = "YlOrRd") +
  labs(title = "Yearly temperature of Saudi Arabia of the years 2011 and 2012",
       fill = "Temperature (°C)") + # Add title
  theme_minimal() 

It gives me this output but how do I remove grey color surrounding The Area of Interest(Saudi Arabia)

Saudii

Heed725 avatar Mar 21 '24 15:03 Heed725

Maybe this will help: https://dieghernan.github.io/tidyterra/articles/faqs.html#nas-remove

Rapsodia86 avatar Mar 21 '24 16:03 Rapsodia86

Thank you so so much Saudi New

Heed725 avatar Mar 21 '24 16:03 Heed725