I am working on mapping some netcdf data in R and have been having a tough time trying to perform a clip with an imported country shapefile that I'm using. Here is my question from yesterday. If you know anything about that, feel free to pop over there and answer that question. :)
BUT I'll start fresh here. So for my work, we are planning on mapping tons and tons of weather data all across the globe. For practice, the guy providing our data provided us with some "example" data to play with. My boss would like for me to create my maps in R. These maps are intended to be return period maps. The data is in netcdf format. I was able to map the netcdf data with the levelplot function from the rasterVis package. I was also able to map the netcdf data with a simple image function. Here's my code (sorry for the funky naming conventions; I've just been playing around with this):
library(maptools) library(ncdf4) library(RColorBrewer) library(raster) setwd("D:\\stuff") #Madagascar shapefile madagascar <- readShapeSpatial("MDG_adm0.shp") plot(madagascar) #Call on netcdf file ncdf.data <- nc_open("swio_rpmaps_200_83.nc") #Call on the 10 year wind return periods returns <- ncvar_get(ncdf.data, "y010") #Define latitude and longitude lon <- ncdf.data$dim$longitude$vals lat <- ncdf.data$dim$latitude$vals #plot image of windspeeds image(lon, lat, returns, col = cm.colors(9,alpha=.6), add = TRUE) #plot Madagascar on top of windspeeds plot(madagascar, add = T)
Is there any way to clip the Madagascar shapefile to this image? Or another way to manipulate the data so that I can perform a clip? Ideally, I would only have the data associated within the bounds of whatever country I'm mapping.