How to plot a map in R without geographic coordinates? - r

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.

Related

Extract values from geom_bin2d

I have taken photos of a bird nesting area and have marked positions of earch bird on the photo. Resulting data is a list of X and Y positions. I transformed pixel data to meters data.
I want to calculate how many of counts are there in squares of 1m2. I was able to get what I looked for graphically with geom_bin2d but I would like to extract the value of each of the squares.
Any functions that would do this? or methods to extract data from geom_bin2d?
Thank you very much!
I have found few functions (density, bkde2D) but they are related to Kernel density estimate, which doesn't seem to fit the same values with geom_bin2d.

R: spatial interpolation with akima package on irregular grid with void data

I have an irregular grid of points in the form (X,Y,Z), where (X,Y) are coordinates on a plane (can be geographical longitude/latitude), Z is some property to interpolate between points. I use akima interpolation package in R. The data set can contain missed values, and akima package does not like it. This can be remedied by a complete.cases() directive reorganizing the data set. But there is a following issue. Certain points contain no data in the sense that the interpolated quality is absent there (NA in R). As a closest example, Z is a depth of a stratigraphic interval, for example, Quaternary deposits. In these places I need to have a "hole" in the interpolated grid, showing that this layer is absent here; meanwhile the algorithm simply interpolates between available points with data.
#small extract from data
mydf<-read.csv(text="lon, lat, Q
411,362,1300
377,395.5,1425
427,370,1800
435.5,352,
428,357,
390,423,1700")
library("akima")
bbb<-data.frame(lon=mydf$lon,lat=mydf$lat,H=mydf$Q)
ccc<-bbb[complete.cases(bbb),]
Q.int<-interp(ccc$lon,ccc$lat,ccc$H,linear=TRUE,
extrap=FALSE,nx=240,ny=240,duplicate="mean")
Then it can be visualized, for example, with image.plot() from fields package
library("fields")
image.plot(Q.int)
In this data set, points 4,5 are lacking. This can be either 1) lack of data on these points, or 2) indication, that the deposits are absent here. I can note it in data set explicitly, for example, with NA symbol. But I need the interpolation, which distinguishes these two cases.
My solution was to interpolate "as is", and then to use a trick: declare that all interpolated values of a property Z on a grid <30 meters are effectively NA's, and then plot it:
Q.int$z.cut<-ifelse(Q.int$z<30,NA,Q.int$z)
This could reflect the geological situation, since layers with decreasing thickness indeed "fade out", and can be stripped on a map, but would it be possible to arrange this problem in a more elegant solution?

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

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

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.

I want to plot a point pattern that is marked by a function (in R)

I have a time series dataset with spatial data (x,y coordinates). Each point is static in location, but its value varies over time, ie. each point has its own unique function. I want to assign these functions as a mark, so I can plot the point pattern with each individual time series as a plotting symbol.
This is an exploratory step to eventually perform some spatial functional data analysis.
As an example, I want something like Figure 2 published in this article:
*Delicado,P., R. Giraldo, C. Comas, and J. Mateu. 2010. Spatial Functional Data: Some Recent Contibutions. Environmetrics 21:224-239
I'm having trouble posting an image of the figure
1) Working in R with ggplot2, I can plot a line of change in quant of each id over time:
(Fake example dataset, where x and y are Carteian coordinates, id is an individual observation, and quant are values of id at each year):
x<-c(1,1,1,2,2,2,3,3,3)
y<-c(1,1,1,2,2,2,3,3,3)
year<-c(1,2,3,1,2,3,1,2,3)
id<-c("a","a","a","b","b","b","c","c","c")
quant<-c(5,2,4,2,4,2,4,4,6)
allData<-data.frame(x,y,year,id,quant)
ggplot(allData,aes(x=year,y=quant, group=id))+geom_line()
2) Or I can plot the geographic point pattern of id:
ggplot(allData,aes(x=x,y=y,color=id))+geom_point()
I want to plot the graph from (2), but use the line plots from (1) as the point symbols (marks). Any suggestions?

Resources