Recenter world map and shapefiles in R - r

I'm new to spatial data analyses in R and am stuck on a particular issue. I have overlaid multiple shapefiles on a world map which is centered on the Atlantic Ocean (default centering). All of my layers import fine in this view, but what I would really like to do is re-center everything over the Indo-Pacific. I know how to re-center just the world map, but when I add my shapefiles to the re-centered map they no longer plot properly (they do not wrap and are cut off beyond a certain longitude). I'm assuming I may need to reformat the coordinates specified within each shapefile? Not sure how to resolve this issue.
How everything looks centered over the Atlantic (default):
How I want my map centered, but shapefile extents are cut off/do not wrap:
Here is my code:
map("worldHires") #Used for default centering, works fine with
shapefiles
map("worldHires", wrap=c(0,360), ylim=c(-60,60)) #Used for
reformatted centering, does not work well with shapefiles
shapefile1 <- rgdal::readOGR("shapefile1.shp")
plot(shapefile1, add=TRUE, col=scales::alpha("darkblue", 0.6),
border=FALSE)
shapefile2 <- rgdal::readOGR("shapefile2.shp")
plot(shapefile2, add=TRUE, col=scales::alpha("darkgreen", 0.6),
border=FALSE)
shapefile3 <- rgdal::readOGR("shapefile3.shp")
plot(shapefile3, add=TRUE, col=scales::alpha("darkred", 0.6),
border=FALSE)
map.axes()
Does anyone have any suggestions for fixing this issue? Thanks ahead of time for the help!

Related

Mapping Antarctica in R

I'm trying to plot some coordinates that stretches across Antarctica, and since I really don't care about the rest of the world, I'm trying to find a way to plot just the Antarctic continent with R (i.e. showing a view from the south pole). Problem is, nobody seems to care about Antarctica and most of the stuff I'm able to find about how to plot coordinates in R straight up cut away the continent because it gets warped in the Mercator projection...
and now I'm completely stuck. Any advices on how to approach the problem? Or helpful sources?
Have you tried using a UTM projection in conjunction with leaflet?
You need to transform to a polar stereographic projection system.
require(maptools)
shp = wrld_simpl[which(wrld_simpl$NAME=="Antarctica"),]
shp = spTransform(shp,"+init=EPSG:3031")
plot(shp)

overlay flux footprint over a map

I need to overlay a plot over a map (it can be from ggmap or from a file in my pc - not a shapefile).
### Here is the map
library(ggmap)
library(RgoogleMaps)
lat = 49.12978
lon = -122.985
center = c(lat, lon)
bbmap <- get_map(location = c(lon, lat), zoom = 18, maptype = "satellite")
And here! is the data and the code to make the plot that I want to overlay.
library(fields)
image.plot(FFP$x_2d[1,],FFP$y_2d[,1],FFP$fclim_2d)
The main problem is that the plot uses distance to a point to generate a flux density plot, so there are no coordinates (I only have the center coordinates which I used to get the map)
This question is pretty much this! that hasn't been answered, but it seems that the solution is georeferencing the data. However, I don't know how to do that in R. I have tried using the "raster" function to create a 3D raster of the map and the image, using the extent of the map for both, but the plot seems not right because it looks like its rotated.
Ultimately, what I want is to have only the 90% or 80% contour line over the map, not the whole thing.
The final image should look something like this but showing only the most external line, reference image: from this webpage
Hope someone can help!
Thanks!

R: cropping/zooming a map

I am trying to overlay data on top of a map of Canada, however I can't adjust the zoom as I would like. In the map, I want to be able to see the lines for each province anong with its name (so using map("world", "Canada") isn't desirable)
I have tried altering the zoom, but one is too zoomed out and the other is too zoomed in:
qmap(location = "Canada", zoom = 3)
qmap(location = "Canada", zoom = 4)
I have tried researching on how to crop the image but have been unsuccessful
Thank you!
You can approach this using either the maps and mapsdata packages or continue with ggmap. It just depends on how much detail you want.
library(raster)
library(maps)
library(mapdata)
canada<-getData("GADM",country="CAN",level=1)
plot(canada,xlim=c(-141,-53),ylim=c(40,85),col="gray80",border="gray40",axes=T,las=1)
invisible(text(getSpPPolygonsLabptSlots(canada),labels=as.character(substr(canada$HASC_1,4,5)),
cex=0.75, col="black",font=2))
Using ggmap, you can specify a bounding box when grabbing your spatial data. It still overplots some of the area you are interested in (i.e., plots most of the US). Therefore, reapplying the bounding box values to the ggmap function cuts down the viewable area.
library(ggmap)
lats<-c(40,85)
lons<-c(-141,-53)
bb<-make_bbox(lon=lons,lat=lats,f=0.05)
cda<-get_map(bb,zoom=3,maptype="terrain")
ggmap(cda)+xlim(lons)+ylim(lats)+theme_bw()+labs(x="Longitude",y="Latitude")

Plotting a Map within a Map in R

I'm brand new to any programming and I'm starting right in the deep end with R!
So, I'm trying to plot a map of some dive sites from which me and a buddy collected some data, which are off the west coast of the Isla de Juventud. I have plotted the map of Cuba and a more zoomed in one of the Island in R, and what I'm hoping to achieve is something along the lines of the following, but I just chucked this together with paint so obviously it would be better quality produced in R.
https://www.dropbox.com/sh/xve973hvih1vw77/OZ-gGpDgsJ <- this link
The code I have for the cuba map and the island map is this, respectively.
map("worldHires", xlim=c(-85.3198254, -73.718262), ylim=c(190663280, 23.5), fill=TRUE, col="darkseagreen3", bgcol="grey80") map.axes()
and
map("worldHires", xlim=c(-83.25, -82.5), ylim=c(21.4, 22), fill=TRUE, col="darkseagreen3", bg="gray80") rect(-83.2, 21.5, -83.15, 21.65, border="red")
I'd rather learn how to do it or where I'm going wrong, if anywhere, here as I really want to improve my skills with this program.
I think you are seeing this the wrong way.
I read the manual for R package Maps in (http://cran.r-project.org/web/packages/maps/maps.pdf), and I couldn't find a map database called "worldHires"
To have a better understanding, try the commands
map('world2')
map.axes()
This should give a plot of the world map; however, to highlight certain cities, read about map.cities command in the package manual.
To get a map of Cuba for example try this
map("world2", region='Cuba',fill=TRUE, col="darkseagreen3")
map.axes()
I hope this helps
Have you loaded maps, mapdata, and maptools? That fixed the same problem for me!

how to plot a part of map but with limitation arround it

how to plot something like this:
see image here, I didn't have 10 reputation so I can not post image, so paste a image URL here.
http://www.flickr.com/photos/97751721#N07/9089566951
or
http://www.flickr.com/photos/97751721#N07/9091786734
right part of this map is a zoom in map with limitation, that is what I want
if it is use R code or other program language will be more great!
If your have everything as Spatial object you could simply plot with standard plot() and set lim to what ever you like.
require(maptools)
shape <- readShapePoints("shapefile.shp")
plot(shape, xlim=c(minXcoordinate, maxXcoordinate) ylim=c(minYcoordinate, maxYcoordinate))
It is a little unclear to me what exactly you want to do. Do you just want to make a map with a specific set of lon/lat boundaries? Do you need to plot data on top of it? Do you need to control the appearance to make it look like the example you give (with line boundaries & minimal geographic information)?
The ggmap package may get you started on this. The syntax goes like this:
require(ggmap)
# The location argument defines the center of the map
exampleMap <- get_map(location = c(lon = -95, lat = 30), zoom=5)
ggmap(exampleMap)
You can then add data to the map if you like (exactly how is left as an exercise to the motivated student!)

Resources