[R] Plotting shapefiles on existing maps
Roger.Bivand at nhh.no
Thu Mar 30 11:56:59 CEST 2006
On Thu, 30 Mar 2006, Ray Brownrigg wrote:
> > From: nhy303 at abdn.ac.uk
> > I have plotted a map of the Barents Sea and surrounding coastline using:
> > map('worldHires',ylim=c(50,85),xlim=c(5,65),fill=T,resolution=0)
> > map.axes()
> > map.scale(x=30,metric=T)
> > Next, I imported a shapefile with depth contours for the sea:
> > contours<-read.shape("D://My Documents/BarentsSea.shp",dbf.data=T)
> > (This is in mercator projection).
> > Despite extensive searches of the help files and R site, I cannot find a
> > way to plot the contours onto the map. Does anyone have any suggestions?
> It is not clear to me exactly what you are having difficulty with, but
> if you are able to produce a contour plot from that data, then note that
> both contour() and map() have an "add" argument, so as long as the axis
> limits are conformant (i.e. both longitude/latitude) you can add one
> plot to the other.
You cannot add projected data to a map() plot - as Ray suggests, the
input shapefile has to be in geographical, not projected coordinates.
If you know the projection of the shapefile (or if there is a *.prj file
accompanying the *.shp), try using the transform() method in the rgdal
package to do this.
If there is a *.prj file, this should work:
my_lines <- readOGR(<directory>, <shapefile_without_extension>)
my_lines1 <- transform(my_lines, CRS("+proj=longlat +ellps=WGS84"))
If not, you'll need to work out exactly what the projection is, and set it
in PROJ.4 notation by:
proj4string(my_lines) <- CRS("<what you found out>")
before transform(). Please in both cases do bbox(my_lines1) to see if you
are in the right ballpark.
> Perhaps the trick is in converting your contours data into
> longitude/latitude form for the x and y components.
> Hope this helps,
> Ray Brownrigg
> R-help at stat.math.ethz.ch mailing list
> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Economic Geography Section, Department of Economics, Norwegian School of
Economics and Business Administration, Helleveien 30, N-5045 Bergen,
Norway. voice: +47 55 95 93 55; fax +47 55 95 95 43
e-mail: Roger.Bivand at nhh.no
More information about the R-help