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

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

Related

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!

Leaflet Polylines do not show up

I have a very small SpatialLinesDataFrame which I need to plot with Leaflet in R.
Unfortunately, for some reason I'm not able to do so.
This is the data (it's 12KB).
I try to do:
library(leaflet)
load("mylines.Rdata")
leaflet() %>% addTiles() %>% addPolylines(data=mylines)
However the resulting map does not make sense, I can only see a line in the top of the screen and this is not what should be plotted.
This is the result:
Instead, If I do:
library(mapview)
mapview(mylines)
Result:
It works perfectly, despite mapview using leaflet underneath.
What am I doing wrong in the Leaflet syntax?
I am used to working with leaflet providing rasters, so I usually use the addRasterImage function, which needs data projected over leaflet's display projection (EPSG:3857). However, for polygons and lines, as #TimSalabim correctly pointed out, this is not the case.
So the solution in this case was to just not reproject the data to the leaflet projection beforehand, and provide it in lat-lon coordinates (EPSG:4326).
mapview worked since it does this automagically.

Google Maps vs. ggplot2/MarMap

Below is a JavaScript page I have created that allows me add and freely move markers on the map. From this map I can figure out the regions I am interested in.
Basically what I want to do is show the same map using ggplot2/MarMap with coastline indicators + bathymetry data. I am really just interested in getting bathymetry data per GPS location, basically getting negative/positive elevation per Lat+Long, so I was thinking if I can plot it then I should be able to export data to a Database. I am also interested in coastline data, so I want to know how close I am (Lat/Long) to coastline, so with plot data I was also going to augment in DB.
Here is the R script that I am using:
library(marmap);
library(ggplot2);
a_lon1 = -79.89836596313478;
a_lon2 = -79.97179329675288;
a_lat1 = 32.76506070891712;
a_lat2 = 32.803624214389615;
dat <- getNOAA.bathy(a_lon1,a_lon2,a_lat1,a_lat2, keep=FALSE);
autoplot(dat, geom=c("r", "c"), colour="white", size=0.1) + scale_fill_etopo();
Here is the output of above R script:
Questions:
Why do both images not match?
In google-maps I am using zoom value 13. How does that translate in ggplot2/MarMap?
Is it possible to zoom in ggplot2/MarMap into a (Lat/Long)-(Lat/Long) region?
Is it possible to plot what I am asking for?
I don't know how you got this result. When I use your script, I get an error since the area your are trying to fetch from the ETOPO1 database using getNOAA.bathy() is too small. However, adding resolution=1 (this gives the highest possible resolution for the ETOPO1 database), here is what I get:
To answer your questions:
Why do both images not match?
Probably because getNOAA.bathy() returned an error and the object dat you're using has been created before, using another set of coordinates
In google-maps I am using zoom value 13. How does that translate in ggplot2/MarMap?
I have no clue!
Is it possible to zoom in ggplot2/MarMap into a (Lat/Long)-(Lat/Long) region?
I urge you to take a look at section 4 of the marmap-DataAnalysis vignette. This section is dedicated to working with big files. You will find there that you can zoom in any area of a bathy object by using (for instance) the subsetBathy() function that will allow you to click on a map to define the desired area
Is it possible to plot what I am asking for? Yes, but it would be much easier to use base graphics and not ggplot2. Once again, you should read the package vignettes.
Finally, regarding the coastline data, you can use the dist2isobath() function to compute the distance between any gps point and any isobath, including the coastline. Guess where you can learn more about this function and how to use it...

R: world maps with ggplot2/ggmap - How to load a png image as a map

I am trying to find a workaround for ggmap's missing support of world maps (i.e. you can't create any maps that show latitudes > 80°, due to idiosyncrasies in the mapproj package).
To a limited extend however, it seems possible to create empty world maps and save them as an image (png etc.), even if you can't use the ggmap object directly as one normally would in ggmap(get_map(...)).
That's why I'd like to load a png (ideally, one I created with ggmap) into ggplot2 and use that as a map instead. How exactly can I do that?
I am aware that you can load background images in ggplot2 (see this stackoverflow question). But I'd also like to plot points on my map - it's important that the latitude/longitude values are mapped correctly.
(Notice: The code in this answer to World map with ggmap provides some code that, in terms of the output, comes close to what I had in mind.)
Here is an example without ggmap that you can use.
require(ggplot2)
require(cshapes)
world <- cshp(date=as.Date("2012-01-1"))
world.points <- fortify(world, region='COWCODE')
world.points2 <- merge(world.points,world#data,by.x="id",by.y="COWCODE",all.x=TRUE )
# Add a variable 'size' per country
world.points2$size <- factor(ifelse(world.points2$AREA < 121600,"small",ifelse(world.points2$AREA > 515000, "large", "medium")))
# Coord_fixed fixes the aspect ratio.
p <- ggplot(world.points2,aes(long,lat,group=group,fill=size)) + geom_polygon(colour="grey50") + coord_fixed()
p

where could we get such a landscape GIS layer

Here, I found a landscape GIS layer is really attractive, especially for presenting species/samples distributions. I would like to know if it can be reached in R or any other resources?
The GIS layer were used in Fig 1. in this article (http://onlinelibrary.wiley.com/doi/10.1111/j.1469-8137.2010.03479.x/full).
This Fig 1 image is here:
http://onlinelibrary.wiley.com/store/10.1111/j.1469-8137.2010.03479.x/asset/image_t/NPH_3479_f1_thumb.gif?v=1&t=gsk5sbhs&s=e5e2e4bbb194f799f7ab9bec85a416e295405784
I have ever tried to submit this question in R-sig-geo. But, I failed. I expect to get some helps/directions here.
Thanks a lots for any directions.
Best wishes,
It is very possible to download this file and read it in with R, configure it to have the correct geo-coordinates so that overplotting works easily, and showing the image with the right colour scheme and so on. But, automating getting all of the data you need is not so easy.
You need the colour table from the GIF file so that you can plot the correct set of RGB values for each pixel (the information is in the file, but I'm not sure if this can be obtained directly with R, I will check - it certainly can be with GDAL, but extracting those values in an automated way depends on various tools being available).
UPDATE: It turns out that the raster package gets hold of the colour information correctly and plots it, see below.
You also need the geo-spatial information, i.e. the coordinates of a reference pixel (say, the top left pixel corner), and the scale (the geographic width and height of the pixels) and this information is not stored in the file. Also, the coordinate system of the file is not in the file, and very likely not provided explicitly with the image data.
If the colours and the coordinate system were stored with the file, then it would all be easy and something like the following would be enough.
(Note this worked for me once, but then I think subsequent requests are blocked by the server, so try to only download the file one time).
u <- "http://onlinelibrary.wiley.com/store/10.1111/j.1469-8137.2010.03479.x/asset/image_n/NPH_3479_f1.gif?v=1&t=gskxvi17&s=0f13fa9dae78bd6837aeee594065c6ca112864d2"
imfile <- paste(tempfile(), ".gif", sep = "")
download.file(u, imfile, mode = "wb")
library(raster) ## rgdal also required for this file format
library(rgdal)
im <- raster(imfile)
plot(im)
This looks fine but now see that there is no "real-world" coordinate system, this is just an axis from pixel 1 to the number in the X dimension (and same for Y).
axis(1, pos = 2)
So, still we need manually work to discover appropriate reference coordinates for the image - and guesses here can work fine, but still they are only guesses and you may end up creating a lot of pain for something seemingly simple.
If plot points interactively is enough for you, then you might use locator in conjunction with points and lines and text, and related plotting functions.
Feng,
if I read the Google docs correctly, you can modify the labels and displayed features with the extra parameters style and element.
I did not include custom parameters for these in the RgoogleMaps package, however, you can easily pass ANY addition parameters via the path argument !
If you read the help file for GetMap carefully, you will note the following example:
note that since the path string is just appended to the URL you can "abuse" the path argument to pass anything to the query, e.g. the style parameter:
#The following example displays a map of Brooklyn where local roads have been changed to bright green and the residential areas have been changed to black:
## Not run: GetMap(center='Brooklyn', zoom=12, maptype = "roadmap", path = "&style=feature:road.local|element:geometry|hue:0x00ff00|saturation:100&style=feature:landscape|element:geometry|lightness:-100", sensor='false', destfile = "MyTile4.png", RETURNIMAGE = FALSE);
Hope this helps,
Markus Loecher
If you just want data like this image, then there are packages to access imagery directly, again utilizing the tools in sp and rgdal. This example is close using gmap in the dismo package.
library(dismo)
e <- extent(-7, 5, 38, 44)
gm <- gmap(e, type = "terrain")
plot(gm)
Note that while we specify the extents in "longlat" the image comes back in its native (Google) Mercator.
print(gm)
See ?gmap for more options on transforming your own data to match the image's projection, or the broader function set in raster, rgdal and sp for other options. There are other imagery providers that might be preferable, and quite a few options in the R suite of contributed packages.

Resources