Plotting map points in R - r

I have successfully loaded a shapefile of NYC PUMA areas into R with maptools and I want to plot 55 points on top of it that I have in another file as follows:
X Y pumace10 events_2008 events_2009
-73.9092456917 40.8916125162 3701 2 0
-73.8617096298 40.8899373255 3702 0 0
-73.8010284966 40.8460832277 3703 1 1
However, the points will not plot.
First I do this to plot the shapefile:
plot(nycs)
And it plots the shapefile
Then I try to plot the points on top but no matter which of the following I do it always fails:
points(nyc_data$X,nnyc_data$Y,pch=20,col="red")
or
plot(nyc_data, pch=16, col='firebrick',add=TRUE)
or
plot(nyc_data$X,nyc_data$Y,pch=20,col="red")
(that final one plots the data on a new plot that is just an X-Y scatter instead of overlaid on the shapefile)
Any ideas how to do this?
EDIT, files added (amended to working files, hopefully!):
Shapefile info: https://www.sendspace.com/file/wbqrpb
Points file: https://www.sendspace.com/file/9yrrbu

Anyway you could send the files you are using?
My guess is that you have one of two problems:
1) either the "points" spatial data frame or the NYC PUMA file do not have a coordinate reference system.
2) they both have reference systems, but they are different.
Likely, your problem is that the points lack a reference system.
EDIT:
The problem is that the two datasets are in different coordinates, and I'm not sure what the coordinate system for the points is. Here is my code. The problem is that the CRS for the points is clearly neither regular lat/long nor the projection used in the shapefile. If you have more info on the source of the data points maybe we can see what projection they use.
library(sp)
library(maptools)
library(rgdal)
library(rgeos)
Points=read.csv("nyc_data_sample.csv",stringsAsFactors=F)
shapefile=readOGR("shapefiles","nyu_2451_34512")
Points_Shape = SpatialPoints(Points[,c("X","Y")],proj4string=CRS("+proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0"))
#Points_Shape = SpatialPoints(Points[,c("X","Y")],proj4string=CRS(proj4string(shapefile)))
Points_Shape = SpatialPointsDataFrame(Points_Shape,Points)

Related

Shapefiles do not overlay raster layer in R

I have hundreds of shapefiles without a coordinate reference system. My goal is the overlay the spatial polygons over the WorldClim raster layer. I used this approach before without any problems. However, this time the coordinates from my shapefiles are strange for me. Each coordinate for bounding box and coords within polygons is composed of 8 digit numbers without comma or dot to separate de decimals.
This is the bounding box from one of the shapes:
SHP bbox: xmin:-17367529, xmax:17367529, ymin:-5997367 and ymax:7052489
which are clearly different from the bounding box of the WorldClim raster layer.
WorldClim bbox: xmin=-180,xmax=180,ymin=-60 and ymax=90
When I tried to overlay the shapefile over the raster layer using plot command nothing happens.
plot(shapefile, add=T)
I understood that this is a projection problem. Then I tried to assign the same coordinate system of the WorldClim raster layer in the shapefile using the CRS function. However, the result remains the same (i.e. the shapefiles do not over the raster). In the sequence, I tried to use the spTransform function from the rgdal package to reproject the shapefile coordinates. However, because shapefile does not have any reference system the function does not work and I do not know how to reproject the shapefile in order to match with the raster layer. I've been researching for a few days about how to deal with this problem and I believe that the absence of a reference system is a key point to the problem. However, I'm failing to overcome this problem and I would like to know if someone could help how to deal with this situation.
You need to define the projection of shape files first using proj4string(meuse) or crs(shapefile)<-crs string then you can use spTransform:
library(rgdal)
data(meuse)
coordinates(meuse) <- c("x", "y")
Here you have the spatial data with x and y but you do not have the crs yet! So if you use spTransform it will fail.
summary(meuse) #proj4string : [NA] so below line fails!
meuse.utm <- spTransform(meuse, CRS("+proj=utm +zone=32 +datum=WGS84"))
# Error in spTransform(xSP, CRSobj, ...) :
# No transformation possible from NA reference system
To get around this, as mentioned above, you first need to define the projection as below:
proj4string(meuse) <- CRS(paste("+init=epsg:28992",
"+towgs84=565.237,50.0087,465.658,-0.406857,0.350733,-1.87035,4.0812"))
summary(meuse) #proj4string : epsg:28992... and then you may use spTransform
and then:
meuse.utm <- spTransform(meuse, CRS("+proj=utm +zone=32 +datum=WGS84"))

geom_sf does not use geometry coordinates in axes but plots correct shape of polygon?

My overall aim is to combine multiple shape files (polygons of river sub-basins from within a large river basin) into one file and plot as a map. This new combined file will later combine with variable data e.g.(rainfall) and plot by aes().
My problem is:
ggplot()+geom_sf() plots the correct shapes of the polygons but doesn't have the correct co-ordinates on the axes - it doesn't use the values given in the geometry column on the axes.
My thoughts on what is wrong, but I'm not sure how to correct:
The shape file read in has geometry in 'long' 'lat' (crs= 4326) but the crs is saying the coordinates are in UTM Zone 48N WGS84 (crs=32648). If I try and force the crs to 4326 the coordinate values change as if the conversion formula is trying to correct them.
geom_sf and coord_sf are doing something that I don't understand!
.
library(sp)
library(raster)
library(ggplot2)
library(sf)
library(ggsf)
library(rgdal)
library(plyr)
library(dplyr)
library(purrr)
setwd("/Users/.../Sub_Basin_Outlines_withSdata/")
list.files('/Users/.../Sub_Basin_Outlines_withSdata/', pattern='\\.shp$')
Read in individual polygon shape files from folder. Combine with ID.
bangsai <- st_read("./without_S_data/", "Nam Bang Sai")
BasinID <- "BGS"
bangsai <- cbind(bangsai,BasinID)
ing <- st_read("./without_S_data/", "Nam Ing Outline")
BasinID <- "ING"
The two individual shape files import as simple features, see image of R code
Combine the individual sub-basin polygon shape files into one shapefile with multiple features.
all_sub_basins <- rbind(bangsai,ing)
The image shows the values of the coordinates of the polygons/features in all_sub_basins$geometry. They are long lat format yet the proj4sting suggests UTM?
Plot the all_sub_basins simple feature shapefile in ggplot
subbasins <- ggplot()+
geom_sf(data=all_sub_basins, colour="red", fill=NA)
subbasins
The result is a correctly plotted shape file with multiple features (there are more polygons in this image than read in above). However the axes are incorrect (nonsense values) and are not plotting the same values as in the geometry field.
If I add in coord_sf and confirm the crs:
subbasins <- ggplot() +
geom_sf(data=all_sub_basins, colour="red", fill=NA)
coord_sf(datum=st_crs(32648), xlim = c(94,110), ylim = c(9,34))
subbasins
Then I get the Correct axes values but not as coordinates with N and E. It seems as if the geometry isn't recognised as coordinates, just as forced numbers?
I don't mind if the coordinates are UTM Zone 48N or lat long. Could I fix it in any of these ways? If so, how do I achieve that?
Change the shape file crs without changing the values in the geometry column so geom_sf would know to plot the correct axes text.
Extract the geometry from the shape file into a two column .csv file with long and lat columns. Convert csv into a sf and create my own shape file with correct crs.
Last resort, leave the plot as it is and replace new axes text manually.
Any help is much appreciated!

An irregular polygon area as plot on spatstat

it's my first time using the spatstat package, so I would like some advice. I am attempting to plot coordinate data into a irregular polygon area (format .shp), to calculate spatial analysis like Ripley's K. How can I add an irregular polygon area as a plot? How can I merge the .ppp data from the coordinates into the polygon area?
I have used the following codes:
Converting the coordinate data to .ppp format
library(spatstat)
library(sp)
library(maptools)
tree.simu <- read.table("simulation.txt", h=T)
tree.simu.ppp <-ppp(x=tree.simu$X,y=tree.simu$Y,window=owin(c(min(tree.simu$X),max(tree.simu$X)),c(min(tree.simu$Y),max(tree.simu$Y))))
plot(tree.simu.ppp)
With this function I am considering the plot area as the max and min valeu of the coordinates. I would like to put the polygon boundary as the plot.
Ploting the irregular polygon area
area <- readShapePoly("Area/Fragment.shp")
plot(area)
plot(tree.simu.ppp, add=T)
or
points(tree.simu.ppp)
The package accept the last function but, when I try to plot both files together, seems like that the .shp file it is fill the whole area. I can't visualize the coordinates data.
Thank you, I really appreciate your help!
ps.: If you know any material with those question, please I would be happy to take a look
This is indeed due to inconsistent bounding boxes as conjectured in the comment by #jlhoward. Your points are in [273663.9, 275091.45] x [7718635, 7719267] while the polygon is contained in [-41.17483, -41.15588] x [-20.619647, -20.610134].
Assuming the coordinates were indeed consistent with the window the correct way way of getting it into a ppp object would be:
library(spatstat)
library(sp)
library(maptools)
area <- readShapePoly("Area/Fragment.shp")
area <- as(area, "owin")
tree.simu <- read.table("simulation.txt", h=T)
tree.simu.ppp <-ppp(x=tree.simu$X,y=tree.simu$Y,window=area)
However, you will get a warning about your points being rejected since they are outside the window, and the object will contain no points.

How to write new shapefiles from exisiting GPS coordinates in R?

I am a novice with R maps and I would like to graphically display plant species distributions as polygons on a map. I have a csv file of UTM coordinates. I am using the maps and maptools programs in R and I have been able to read in existing ESRI shapefiles. The discussion posts that I have seen all seem to deal with reading in existing shapefiles and editing them somewhat and "rewriting" them back out. Is there a simple way to convert my csv file into a shapefile for mapping purposes? Thank you for your help, Eric
You should have more info about your coordinate reference system. And I'm not sure about what you mean with 'polygon' : i think that .csv can only contain coordinates points.
Here is an exemple of workflow : create a simple data.frame; set coordinates ; write shapefile ; read shapefile back; and plot simple map. Hope it helps. Instead of creating a data.frame, you could juste import your .csv.
# Load libraries
library(sp)
library(rgdal)
# set CRS . For epsg code, see http://spatialreference.org/
projTest<-CRS("+init=epsg:28992")
# create a temp dir for shapefiles
trashMap<-tempdir()
# create dumb data:
occData<-data.frame(x=c(12,13,14,20),y=c(0,1,4,9),obs=c("a","b","c","d"))
# set x and y as coordinates
coordinates(occData)<-c('x','y')
# assign CRS
proj4string(occData)<-projTest
# write simple shapefile in trashMap dir
writeOGR(occData, trashMap, layer='testLayer', driver="ESRI Shapefile")
# get list of Layers
layers<-ogrListLayers(trashMap)
# read shapefile and selected layer
occData2<-readOGR(trashMap, layers)
# Modify data
occData2$newColumn<-c('no','stress','','dude')
occData2
# verify that is projected
is.projected(occData2)
# display projection type
proj4string(occData2)
# print maps before and after export in shapefile.
print(spplot(occData, auto.key=F, col.regions='black', scales=list(draw=T), pch=2, cex=1))
print(spplot(occData2, auto.key=F, col.regions='black', scales=list(draw=T), pch=2, cex=1))

How to plot contours on a map with ggplot2 when data is on an irregular grid?

Sorry for the wall of text, but I explain the question, include the data, and provide some code :)
QUESTION:
I have some climate data that I want to plot using R. I am working with data that is on an irregular, 277x349 grid, where (x=longitude, y=latitude, z=observation). Say z is a measure of pressure (500 hPa height (m)). I tried to plot contours (or isobars) on top of a map using the package ggplot2, but I am having some trouble due to the structure of the data.
The data comes from a regular, evenly spaced out 277x349 grid on a Lambert conformal projection, and for each grid point we have the actual longitude, latitude, and pressure measurement. It is a regular grid on the projection, but if I plot the data as points on a map using the actual longitude and latitude where the observations were recorded, I get the following:
I can make it look a little nicer by translating the rightmost piece to the left (maybe this can be done with some function, but I did this manually) or by ignoring the rightmost piece. Here is the plot with the right piece translated to the left:
(An aside) Just for fun, I tried my best to re-apply the original projection. I have some of the parameters for applying the projection from the data source, but I do not know what these parameters mean. Also, I do not know how R handles projections (I did read the help files...), so this plot was produced through some trial and error:
I tried to add the contour lines using the geom_contour function in ggplot2, but it froze my R. After trying it on a very small subset of the data, I found that out after some googling that ggplot was complaining because the data was on an irregular grid. I also found out that that is the reason geom_tile was not working. I am guessing that I have to make my grid of points evenly spaced out - probably by projecting it back into the original projection (?), or by evenly spacing out my data by either sampling a regular grid (?) or by extrapolating between points (?).
My questions are:
How can I draw contours on top of the map (preferably using ggplot2) for my data?
Bonus questions:
How do I transform my data back to a regular grid on the Lambert conformal projection? The parameters of the projection according to the data file include (mpLambertParallel1F=50, mpLambertParallel2F=50, mpLambertMeridianF=253, corners, La1=1, Lo1=214.5, Lov=253). I have no idea what these are.
How do I center my maps so that one side is not clipped (like in the first map)?
How do I make the projected plot of the map look nice (without the unnecessary parts of the map hanging around)? I tried adjusting the xlim and ylim, but it seems to apply the axes limits before projecting.
DATA:
I uploaded the data as rds files on Google drive. You can read in the files using the readRDS function in R.
lat2d: The actual latitude for the observations on the 2d grid
lon2d: The actual longitude for the observations on the 2d grid
z500: The observed height (m) where pressure is 500 millibars
dat: The data arranged in a nice data frame (for ggplot2)
I am told that the data is from the North American Regional Reanalysis data base.
MY CODE (THUS FAR):
library(ggplot2)
library(ggmap)
library(maps)
library(mapdata)
library(maptools)
gpclibPermit()
library(mapproj)
lat2d <- readRDS('lat2d.rds')
lon2d <- readRDS('lon2d.rds')
z500 <- readRDS('z500.rds')
dat <- readRDS('dat.rds')
# Get the map outlines
outlines <- as.data.frame(map("world", plot = FALSE,
xlim = c(min(lon2d), max(lon2d)),
ylim = c(min(lat2d), max(lat2d)))[c("x","y")])
worldmap <-geom_path(aes(x, y), inherit.aes = FALSE,
data = outlines, alpha = 0.8, show_guide = FALSE)
# The layer for the observed variable
z500map <- geom_point(aes(x=lon, y=lat, colour=z500), data=dat)
# Plot the first map
ggplot() + z500map + worldmap
# Fix the wrapping issue
dat2 <- dat
dat2$lon <- ifelse(dat2$lon>0, dat2$lon-max(dat2$lon)+min(dat2$lon), dat2$lon)
# Remake the outlines
outlines2 <- as.data.frame(map("world", plot = FALSE,
xlim = c(max(min(dat2$lon)), max(dat2$lon)),
ylim = c(min(dat2$lat), max(dat2$lat)))[c("x","y")])
worldmap2 <- geom_path(aes(x, y), inherit.aes = FALSE,
data = outlines2, alpha = 0.8, show_guide = FALSE)
# Remake the variable layer
ggp <- ggplot(aes(x=lon, y=lat), data=dat2)
z500map2 <- geom_point(aes(colour=z500), shape=15)
# Try a projection
projection <- coord_map(projection="lambert", lat0=30, lat1=60,
orientation=c(87.5,0,255))
# Plot
# Without projection
ggp + z500map2 + worldmap2
# With projection
ggp + z500map + worldmap + projection
Thanks!
UPDATE 1
Thanks to Spacedman's suggestions, I think I have made some progress. Using the raster package, I can directly read from an netcdf file and plot the contours:
library(raster)
# Note: ncdf4 may be a pain to install on windows.
# Try installing package 'ncdf' if this doesn't work
library(ncdf4)
# band=13 corresponds to the layer of interest, the 500 millibar height (m)
r <- raster(filename, band=13)
plot(r)
contour(r, add=TRUE)
Now all I need to do is get the map outlines to show under the contours! It sounds easy, but I'm guessing that the parameters for the projection need to be inputted correctly to do things properly.
The file in netcdf format, for those that are interested.
UPDATE 2
After much sleuthing, I made some more progress. I think I have the proper PROJ4 parameters now. I also found the proper values for the bounding box (I think). At the very least, I am able to roughly plot the same area as I did in ggplot.
# From running proj +proj=lcc +lat_1=50.0 +lat_2=50.0 +units=km +lon_0=-107
# in the command line and inputting the lat/lon corners of the grid
x2 <- c(-5628.21, -5648.71, 5680.72, 5660.14)
y2 <- c( 1481.40, 10430.58,10430.62, 1481.52)
plot(x2,y2)
# Read in the data as a raster
p4 <- "+proj=lcc +lat_1=50.0 +lat_2=50.0 +units=km +lon_0=-107 +lat_0=1.0"
r <- raster(nc.file.list[1], band=13, crs=CRS(p4))
r
# For some reason the coordinate system is not set properly
projection(r) <- CRS(p4)
extent(r) <- c(range(x2), range(y2))
r
# The contour map on the original Lambert grid
plot(r)
# Project to the lon/lat
p <- projectRaster(r, crs=CRS("+proj=longlat"))
p
extent(p)
str(p)
plot(p)
contour(p, add=TRUE)
Thanks to Spacedman for his help. I will probably start a new question about overlaying shapefiles if I can't figure things out!
Ditch the maps and ggplot packages for now.
Use package:raster and package:sp. Work in the projected coordinate system where everything is nicely on a grid. Use the standard contouring functions.
For map background, get a shapefile and read into a SpatialPolygonsDataFrame.
The names of the parameters for the projection don't match up with any standard names, and I can only find them in NCL code such as this
whereas the standard projection library, PROJ.4, wants these
So I think:
p4 = "+proj=lcc +lat_1=50 +lat_2=50 +lat_0=0 +lon_0=253 +x_0=0 +y_0=0"
is a good stab at a PROJ4 string for your data.
Now if I use that string to reproject your coordinates back (using rgdal:spTransform) I get a pretty regular grid, but not quite regular enough to transform to a SpatialPixelsDataFrame. Without knowing the original regular grid or the exact parameters that NCL uses we're a bit stuck for absolute precision here. But we can blunder on a bit with a good guess - basically just take the transformed bounding box and assume a regular grid in that:
coordinates(dat)=~lon+lat
proj4string(dat)=CRS("+init=epsg:4326")
dat2=spTransform(dat,CRS(p4))
bb=bbox(dat2)
lonx=seq(bb[1,1], bb[1,2],len=277)
laty=seq(bb[2,1], bb[2,2],len=349)
r=raster(list(x=laty,y=lonx,z=md))
plot(r)
contour(r,add=TRUE)
Now if you get a shapefile of your area you can transform it to this CRS to do a country overlay... But I would definitely try and get the original coordinates first.

Resources