how can i obtain the same basemaps frames with lat lons, from diferents netcdf files, when i plot it - netcdf

I have three netcdf files with different grid, I need to plot the three files in three different maps, but with the same geographical limits, without the data being stretched or shrunk to cover the entire box, I have tried with:
m = Basemap (llcrnrlon = -85., llcrnrlat = 8., urcrnrlon = -68., urcrnrlat = 17, rsphere = 6371200., resolution = 'l')
but what basemap does is to adapt the data that was previously defined when reading the netcdf to the box, not cutting the data in the latitudes and longitudes defined in the basemap if not adapting the data to that box.
In synthesis I need the data to be cut at the latitudes and longitudes defined with basemap, so that the three netcdf files are comparable in the three maps I require.

Thanks, i did it changing map.makegrid by map.meshgrid, with this method I could fix it.

Related

Trying to convert Numerical Values of Lat/Long into Spatial Data

I am working with a dataset that features chemical analyses from different locations within a cave, with each analysis ordered by a site number and that sites latitude and longitude. This first image is what I had done originally simply using ggplot.
Concentrations mapped by color over map
But what I want to do is use the shapefile of the cave system from which the data is sourced from and do something similar by plotting the points over the system and then coloring them by concentration. This below is the shapefile that I uploaded
Cave system shapefile
So basically I want to be able to map the chemical data from my dataset used to map the first figure, but on the map of the shapefile. Initially it kept on saying that it could not plot on top of it. So I figured I had to convert the latitude and longitude into spatial coordinates that could then be mapped on the shapefile.
Master_Cave_data <- Master_cave_data %>%
st_as_sf(MastMaster_cave_data, agr = "identity", coord = Lat_DD)
This was what I had thought to use in order to convert the numerical Latitude cooridnates into spatial data.

Crop out all mapped area beyond region of 13 U.S. states

I've just started with mapping in R and I've managed to convert a lat, lon dataframe to a raster file and then plot state borders on top of that.
Now I need to get it ready to publish and would like to include only the shape of my 13-state region (and no great lakes).
library(tmap)
usa <- st_as_sf(maps::map("state", fill=TRUE, plot =FALSE))
map_us<- tm_shape(usa)+
tm_borders()
tm_shape(raster_file) +
tm_raster(style = "cont", palette = "viridis", midpoint = 0)+
map_us
I'm having a hard time finding something out there that would provide a polygon for multiple states and I have been through a lot of mapping packages. Sorry I can't include my raster data here.
To crop a raster file to {sf} vector shape you have in principle two options:
crop at data level; this involves raster::mask() with possibly raster::crop() later to reduce the extent of the raster (masked raster retains the original size)
retain the data, and overlay a white polygon with a hole over your plot
Cropping on data level is more "pure", but will leave you with ragged edges (a raster cell has to be square).
Overlaying a white polygon is not so pure, but might be preferable if your key aim is a slick presentation (and purity of essence be damned...)
You will find both of them discussed, together with examples and sample code, in this post on the RStudio community site.

Plotting Latitude and Longitude over a map

I'm currently using ggmaps to display a map of the location I'm trying to display points over but when I use the points function to try to add the points over the map it says that my list of latitudes and longitudes cannot be used for the arguments in x and y. Is there any better way to plot the points other than having to go one by one and individually code them onto my map?
Edit: Here's the data I'm working with right now and the first few lines of code that aren't working-
-A table of 2 variables "lat" and "lon" that show all the points geocoded
-A list of the addresses where the geo coordinates came from
mapTampa <- get_map(location = 'Tampa', zoom = 10)
points(lon, lat, col = "red", cex = 0.5)
The first line works and gives me the map but when I tried to add points thats when it gave me the x y error so I tried:
finalMap<-mapTampa+geom_point(aes(x=lon, y=lat, data=Filtered_Data))
Where latlong is my table with both latitudes and longitudes but that doesn't work either. I'm fairly new to R and this is my first project working with it but it seems like I've read everything about ggplot and ggmap and nothing is helping me.

Overlap in labels of point data in rasterVis

I have a raster r, one polygon shapefile regions and a point shapefile cities. I need to plot all three into one map layout. In addition to this I need to label point file with names of cities (cities$city$Town.Name) and their temperature and precipitation value (assigned as cities$labels). So I have used the following code with packages 'raster' and 'rasterVis'.
p1<-levelplot(regions.r,par.settings=mytheme,scales=list(draw=FALSE),xlab="",ylab="",margin=F)+
layer(sp.polygons(regions))+
layer(sp.points(cities,pch=20,cex=1.5,col="black"))
p1+
layer(sp.text(coordinates(cities), txt = cities$city$Town.Name, pos = 3,col="black",font=list(face="bold"),cex=0.8))+
layer(sp.text(coordinates(cities),txt = cities$label,
pos = 1,cex=0.6,col="black"))#Add shapefile labels
This works fine when area has scattered cities distribution (see figure below).
However, if the cities are concentrated in one part I experience overlap of labels (see figure below). Is there a way to avoid the label overlap?

R Converting contour lines to elevation plot

I would like to be able to create an elevation plot from contour lines in R. I am very new to using shape files
At the moment I have downloaded data from here
which provides .shp files for all of the UK.
It also provides the contour lines, summarising the topology of the UK.
For the elevation plot I would like a data.frame or data.table of evenly spaced points (100m apart from each other) to produce a data output giving an x, y and z value. Where x and y represent the latitude and longitude (or Eastings and Northings), and z represent the height (in meters above sea-level).
I think there are probably some tools that will automatically carry out the interpolation for you, but am unsure how it would work with geo-spatial data.
This is my basic start...
require(maptools)
xx <- readShapeSpatial("HP40_line.shp")
Choose "ASCII Grid and GML (Grid)" as download format for the "OS Terrain 50" product, and download the file. This will give you a zip file containing many directories of zip files, each of which contains portions of a 50 m elevation grid of the UK (the portion I looked at had 200 x 200 cells, meaning 10 km x 10 km). I went into the directory data/su, unzipped the zip file there, and did
library(raster)
r = raster("SU99.asc")
plot(r)
to aggregate this to a 100 m grid, I did
r100 = aggregate(r) # default is factor 2: 50 -> 100 m
As mentioned above, the advice is to work on the grids as contour lines are derived from grids, working the other way around is a painful and a great loss of information.
Getting grid values in longitude latitude as a data.frame can be done in two ways:
df = as.data.frame(projectRaster(r, crs = CRS("+proj=longlat")), xy = TRUE)
unprojects the grid to a new grid in longitude / latitude. As these grids cannot coincide, it minimally moves points (see ?projectRaster).
The second option is to convert the grid to points, and unproject these to longitude latitude, by
df2 = as.data.frame(spTransform(as(r, "SpatialPointsDataFrame"), CRS("+proj=longlat")))
This does not move points, and as a consequence does not result in a grid.

Resources