R: maps package: Able to extend map boundary to custom longitude range? - r

I'm using the map_data('world2') data in ggplot2 with geom_map to plot some data, but I've run into a bit of an issue. I've divided up my regions into 3 longitude ranges (40E-140E, 140E-100W,100W-40E). As you can see, it doesn't fit neatly either into the (180W,180E) or (0,360) longitude ranges of the two world maps, so when I try to plot the data with a longitude axis that has a range encompassing the data, I get a blank portion of map on the right.
Is there a way to plot an extra slice of the map (i.e. 0->40E if using 'world2') adjacent to longitude 359? I tried manually adjusting the longitude values by doing the following:
m<-map_data('world2')
m$long<-ifelse((m$long>=0 & m$long<=40),m$long+360,m$long)
ggplot(data=longdata)+
coord_fixed(xlim=c(40,400),ylim=c(-90,90)) +
geom_map(data= m, map = m, aes(map_id=region))
However, the resulting map is an ungodly mess, likely because the polygons are now all screwed up. Any thoughts?

AHA!
I just needed to modify m to the following:
m<-map_data('world2',wrap=c(40,400))

Related

geom_hex and ggmap lat/long misallignment

I am new to geo-mapping and wanted to produce a hexagonal heatmap over a geographic map of Canada.
I have latiude and longitude within my original data set, and I even have a variable I wish to use to produce the "heat" of each hexagonal tile placed on the map, but I am encountering 2 problems.
1) it appears as if there is a vertical translation applied to each coordinate.
2) I do not know how to show "heat" of another variable and not show density for the heatmap.
My data is given as follows:
map<-get_map(location='Canada', zoom=3, maptype = "terrain",
source='google',color='color')
dat<-data.frame(cbind(c(47.00735, 47.36228, 47.40399, 48.08666,47.57196, 47.63742),
c(-52.95892, -53.29399, -52.79987, -52.89429, -53.27813, -52.92653),
c(86301.14, 1017390.34, 2662332.67, 473139.73, 8251899.99, 167512.52)))
names(dat)<-c('Longitude','Latitude','Rating')
Now the interesting thing is, when I run the following code, I get a hexagonal heat map with respect to density:
ggplot(dat, aes(x=Longitude,y=Latitude,color=Rating))+
stat_binhex(na.rm=TRUE)+
scale_fill_gradientn(colours=c("red","blue"),na.value=NA)
I get this output:
What I would like is a hexagonal heatmap where heat is shown by the Rating field.
Next, when I add a google map layer with the following code:
ggmap(map) +
ggplot(dat, aes(x=Longitude,y=Latitude,color=Rating))+
stat_binhex(na.rm=TRUE)+
scale_fill_gradientn(colours=c("red","blue"),na.value=NA)
I get the following error: Error: Don't know how to add o to a plot
If anyone know hows to remedy these issues, it would be greatly appreciated. Thank you!

How to generate vector arrows that conforms to a raster slope layer?

I would like to generate vector arrows that conform to the topography/slope of a raster dataset of a river catchment area.
I have created a Fishnet grid of points in ArcGIS and I would like to create a single arrow for each point of a set length that will follow the shape of the slope i.e. follow the path of least resistance, the line will follow progressively small numbers in a 3 x 3 grid.
I think I can generate the vector arrows using vector plot. Is it possible to achieve the lines conforming to the raster?
UPDATE: I have ~200,000 lines that I generated from a grid of points. I am going to turn these into a raster using R and set it to the same resolution as my slope raster.
Any ideas on how to layer the raster lines on the slope so I can get the lines to follow the lowest values of the slope?
This is for display/mapping purposes only? Use a DEM or TIN and display your arrow lines in ArcScene.
EDIT: given your update about your data and software not working-
Try this:
1) Make a raster surface covering the extent of your data with a cell size of 100m (or smaller or larger if that doesn't suit)
2) Convert that raster to a polygon layer e.g. 'area_grid100m'
3) Do a spatial join and assign all points a polygon cell id from one of the unique id fields in 'area_grid100m'
4) Use Summarize to get the mean lat/long of the start points and mean lat/long of the end points for each polygon. Summarize on the polygon id field and get select mean for both the lat and long fields
5) Add summary table to ArcMap, right click and select Display XY Data (set X Field as longitude and y Field as latitude). Right right the result and select Data > Export Data to make it permanent. You will now have two points per 'area_grid100m' cell.
5) Recreate your lines using this new file, which will give you one line per cell
If the res is not small enough, make the 'area_grid' cells smaller.

How do I scale worldHires to plot coastlines correctly on R?

I'm trying to overlay the coastlines and country borders onto the R map I've drawn.
I'm trying the simple worldHires function to do this but it's not scaling correctly to my map. I downloaded my data from https://www.esrl.noaa.gov/psd/data/gridded/data.UDel_AirT_Precip.html and the file name is precip.mon.total.v401
This is my code when I try to plot my map (excluding the first few lines of reading data and variables into R):
>image.plot(lon,lat,precip[,ncol(precip):1,8],
main="Precipitation (August, 2001)",
xlab=expression(paste("Longitude(",degree,"E)")),
ylab=expression(paste("Latitude(",degree,"N)")),
zlim=c(0,15),xlim = c(min(lon),max(lon)),
ylim = c(min(lat),max(lat)),cex=0.5)
> map('world2Hires', fill=FALSE, add=T)
The boundary layer from world2Hires is very small, scaling from -90 to 90 for Latitude and 0 to 360 for Longitude.
My data's range is Latitude -180 to 180 and Longitude 0 to 720. So how do I scale the world2Hires to match my data? Or is that not possible? See image in link
In your question, you seem to be mixing the latitude values (-90:90) degrees with the matrix indices. Since the map is of resolution 0.5 degrees, these run 0:360. The same for the longitudes.
So from your question, I am not sure whether the problem is that your vectors "lon" and "lat" are incorrect (they should probably be something like lon=seq(0,360,by=0.5); lat=seq(-90,90,by=0.5)). Obviously, the map() command expects the co-ordinates to be in degrees.
But the illustration on the link you provided, shows that the data is on longitude [0,360] while the data in worldHires is on [-180,180].
I think the simplest solution is to use "world2Hires", which is the same map but with longitudes shifted to [0,360], which should fit your map.
As a side remark, using worldHires is possibibly not the best choice of world map. The country borders in some parts of the world are outdated, and the high resolution is not so important at a global scale. Just using "world2" (the "2" again is for the shift to [0,360]) would probably look just as good on a global map.
I may add that in the very near future, the next version of "map" will make such shifting of longitudes much easier, whithout the need for an extra data set.

How can I get the area of each Voronoi Polygon in R?

I have a set of coordinates X and Y for my points and used the deldir to create determine and plot the Voronoi Polygons. (I've used this tutorial here)
This is my plot: (sorry that its so small, but you get the idea).
I need to determine the area of each polygon. How can I do that?
I looked up in the deldirpackage page and couldnt find anything related to the Voronoi polygons, only about other
Based on the reference manual (https://cran.r-project.org/web/packages/deldir/index.html), the output of the deldir function is a list. One of the list element, summary, is a data frame, which contains a column called dir.area. This is the the area of the Dirichlet tile surrounding the point, which could be what you are looking for.
Below I am using the example from the reference manual. Use $ to access the summary data frame.
library(deldir)
x <- c(2.3,3.0,7.0,1.0,3.0,8.0)
y <- c(2.3,3.0,2.0,5.0,8.0,9.0)
dxy1 <- deldir(x,y)
dxy1$summary

How to plot a map in R without geographic coordinates?

I have a data set with some meteorological stations. I have to locations of these stations given as locations in a grid that is used for estimation of some models, i.e., they have x and y values with ranges of around (0,600) for x and (0,500) for y. If I plot them, the "map" looks like this:
.
Now, I would like to have a real map of Europe under these points. How can I do this?
I have some sort of IDs that I could partially match to a data base from the World Meteorological Organization which allowed me to get the longitudes and latitudes for a subset of these stations (around 15 % are missing, however). If I plot those on a real map, using for example the maps package for R, I get the locations on a map which looks like this:
.
However, for some stations in my data set, I do not have IDs given, so I can not match them to the true coordinates. The second plot thus contains ca 15% less stations than the first one, but I would like to show all points from the first plot in a real map (as given by the second plot). As mentioned, I thus need a way to get from the x-y-locations to the true geographical locations, or a way to transform a map such that it matches the first plot.
It is impossible to get the missing true locations, so my question is how I can get a map under the points given by the x and y locations in the grid.

Resources