R: cropping/zooming a map - r

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")

Related

Choropleth maps (plotly) with detailed borders

I am trying to plot a world map, where the colors correspond to a specific variable. However, it does not show small states like "Andorra" or "Liechtenstein", even though they are in the data frame (see df$COUNTRY). I think the problem is, that the borders are not drawn with much detail. Hence one can not see the small states. Is there a way draw the borders with more detail like with the packs, maps, and mapdata (e.g wolrd_detailed <- map_data ("worldHires")? Or any other way to make the small states visible? Here is the example from the plotly website. It does not show small states:
library(plotly)
df <- read.csv("https://raw.githubusercontent.com/plotly/datasets/master/2014_world_gdp_with_codes.csv")
fig <- plot_ly(df, type='choropleth', locations=df$CODE, z=df$GDP..BILLIONS., text=df$COUNTRY, colorscale="Blues")
fig
Thanks for the help!

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!

Correct Use of nowrapRecenter()

I'm using R 3.1.2, trying to create a choropleth using a world map. The quirk is that, because my audience is in Asia I need to recenter the map. From the documentation it seems like nowrapRecenter() is perfect for this, but I find that it doesn't seem to work as advertised. For example, start without any recentering:
library(maps)
library(maptools)
library(rgdal)
data(wrld_simpl)
plot(wrld_simpl)
Now try to recenter at 148E longitude, in order to move Asia closer to the centre of the map while splitting as few land masses as possible at the left/right margins:
library(maps)
library(maptools)
library(rgdal)
data(wrld_simpl)
world <- nowrapRecenter(wrld_simpl,offset=148,avoidGEOS=TRUE)
plot(world)
What you get is a bit messy. Not only is the map centred at 180E longitude, but there are scratches all across the map where polygons which nowrapRecenter() should have been divided and re-closed at the left/right are extending across the full width of the map. In fact recentering does not appear to work cleanly for any chosen offset.
A similar question came up before, and the final comment there provided an example using nowrapRecenter(), but it no longer seems to work. What's the best way to recenter a world map (using SpatialPolygons) and have the polygons at the left/right margins be properly divided?
Thanks!
This is only a partial answer, so if it's not adequate let me know and I'll delete it.
The problem appears to be that transforming a Mercator projection fails near the poles. If you are willing to exclude Greenland and Antarctica, this works.
library(maptools)
data(wrld_simpl)
wrld <- wrld_simpl[!(wrld_simpl$NAME %in% c("Greenland","Antarctica")),]
library(ggplot2)
ggplot(wrld,aes(x=long,y=lat,group=group))+
geom_polygon(fill="white",color="grey30")+
coord_map(orientation=c(90,0,148))+
scale_x_continuous(breaks=c(0,60,120,180,-120,-60))+
theme_bw()
Even with this restriction, nowrapRecenter(...) failed.

shade between two latitudes on country map

I want to plot a simple outline of a country map and have an area between two latitudes shaded grey.
I think I need to use the rworldmap library but I'm not exactly sure how to achieve what I want.
Good suggestions by others. I initially thought that you might want the latitudinal shading to extend beyond the country borders. If that's the case here's a quick and dirty solution using rworldmap for the map, sp to plot it and polygon from base graphics to add the latitude shading. You could then modify using spTransform from rgdal if you want to project, or the suggestion from Sam if you want to crop to country borders.
library(rworldmap)
library(sp)
sPDFworld <- getMap(resolution="low")
#getMap() maps have ADMIN & NAME fields with country names formatted differently
#use sPDFworld$ADMIN sPDFworld$NAME to check
country <- "United Kingdom"
#to plot just this country (uses sp)
plot(sPDFworld[ sPDFworld$ADMIN==country, ] )
#shade a latitude region
polygon(x=c(-180,-180,180,180),y=c(55,60,60,55),col=adjustcolor('grey',alpha.f=0.5),border=NA)
I'd do something as in https://stackoverflow.com/a/13986029/1358308 . You can crop down to your "rectangle" as needed and then just plot the interiour of the resulting polygon as "grey", and don't print the borders by setting them to NA.
If you plot this "after" the larger outline (using the same polygon, before doing the intersection) then you should just get the country filled in. If you want the "water" filled in using a different color, you can do this "first". The later plot(x, add=TRUE) will plot the new things "on top" so you won't see the working behind.
Hope that makes sense!

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