Okay, I am having issues getting this to work. I looked at the other forum but it's just saying that my data is now messed up and full of NAs
I'm using EC-Earth GCM (under experiment id) for precipitation ("pr" under variable) variable for monthly (under frequency), year 2050 from the cmip6 website but any netcdf file would work (https://esgf-node.llnl.gov/search/cmip6/)
I got the shape file from the ArcGIS website.
> library(ncdf4)
> library(sf)
> library(ggplot2)
> library(stars)
> library(dplyr)
> library(maps)
> library(sp)
> library(maptools)
> library(raster)
>
>
> #attempt to rotate from 0-360 to -180 to 180 lon
> r <- raster::raster(file.choose())
#open pr_Amon_EC-Earth3_ssp370_r1i1p1f1_gr_205001-205012.nc
> r1 <- raster::rotate(r)
>
> print(r1)
class : RasterLayer
dimensions : 256, 512, 131072 (nrow, ncol, ncell)
resolution : 0.703125, 0.7016692 (x, y)
extent : -180, 180, -89.81366, 89.81366 (xmin, xmax, ymin, ymax)
crs : +proj=longlat +datum=WGS84
source : memory
names : Precipitation
values : -1.745207e-25, 0.000460271 (min, max)
z-value : 2050-01-16
>
> stars_object <- raster::raster(r1) %>% st_as_stars()
> sf_object <- sf::st_read(file.choose())
#open Countries_WGS84.shp
Reading layer `Countries_WGS84' from data source `/Users/8kk/Desktop/RStudio Projects/NetCDF Mapping/Countries_WGS84/Countries_WGS84.shp' using driver `ESRI Shapefile'
Simple feature collection with 251 features and 2 fields
geometry type: MULTIPOLYGON
dimension: XY
bbox: xmin: -180 ymin: -90 xmax: 180 ymax: 83.6236
epsg (SRID): 4326
proj4string: +proj=longlat +datum=WGS84 +no_defs
>
> #Make sure they have the same CRS
> sf::st_crs(stars_object) <- sf::st_crs(sf_object)
>
> ggplot()+
+ geom_stars(data = stars_object) +
+ geom_sf(data = sf_object) # + aes(color = values)
**Error: Must request at least one colour from a hue palette.**
> str (stars_object)
List of 1
$ layer: logi [1:512, 1:256] NA NA NA NA NA NA ...
- attr(*, "dimensions")=List of 2
..$ x:List of 7
.. ..$ from : num 1
.. ..$ to : int 512
.. ..$ offset: num -180
.. ..$ delta : num 0.703
.. ..$ refsys: chr "+proj=longlat +datum=WGS84 +no_defs"
.. ..$ point : logi NA
.. ..$ values: NULL
.. ..- attr(*, "class")= chr "dimension"
..$ y:List of 7
.. ..$ from : num 1
.. ..$ to : int 256
.. ..$ offset: num 89.8
.. ..$ delta : num -0.702
.. ..$ refsys: chr "+proj=longlat +datum=WGS84 +no_defs"
.. ..$ point : logi NA
.. ..$ values: NULL
.. ..- attr(*, "class")= chr "dimension"
..- attr(*, "raster")=List of 3
.. ..$ affine : num [1:2] 0 0
.. ..$ dimensions : chr [1:2] "x" "y"
.. ..$ curvilinear: logi FALSE
.. ..- attr(*, "class")= chr "stars_raster"
..- attr(*, "class")= chr "dimensions"
- attr(*, "class")= chr "stars"