Overlapping data contour on a map - r

I have gone through few tutorials and answers here in stackoverflow such as:
Overlap image plot on a Google Map background in R or
Plotting contours on an irregular grid or Geographical heat map of a custom property in R with ggmap or How to overlay global map on filled contour in R language or https://blog.dominodatalab.com/geographic-visualization-with-rs-ggmaps/
They either don't serve my purpose or consider the density of the data to create the image.
I am looking for a way to plot contour on a map of a certain data, and would expect the image to look something like this:
or something like this taken from https://dsparks.wordpress.com/2012/07/18/mapping-public-opinion-a-tutorial/:
I have a data here that gives a contour plot like this in plot_ly but i want this over the map given by latitudes and longitudes.
Please guide me on how this can be done. Any links to potential answers or codes would be helpful.

Ok I did some digging and figured that to plot the data -which in this case are point values randomly distributed across the Latitude and Longitude, one has to make it continuous instead of the discreetly distributed one. To do this I interpolated the data to fill in the gaps, this method is given in Plotting contours on an irregular grid and then take it from there. Now the interpolation here is done using a linear regression, one can use other methods such as IDW, Kriging, Nearest Neighbourhood etc for which R-packages are easily available. These methods are widely used in climatology and topographic analysis. To read more about interpolation methods see this paper.

Related

cluster: :clusplot axis in wrong direction

I'm trying to plot the cluster obtained from fuzzy c-means clustering.
The plot should look like this.
code for the plot
plot(data$Longitude, data$Latitude, main="Fuzzy C-Means",col=data$Revised, pch=16, cex=.6,
xlab="Longitude",ylab="Latitude")
library(maps)
map("state", add=T)
However, when I tried to use clusplot the plot is displaying in opposite direction(both top and bottom and left and right) as below.
I wanna know if there's a way to reverse the plot to show in the order as the above picture.
Also, for the very dense area, it's hard to find the ellipse label. I wanna know if there's a way to show the label inside the ellipse instead of outside.
code for 2nd pic
library(cluster)
clusplot(cbind(Geocode$Longitude, Geocode$Latitude), cluster, color=TRUE,shade=TRUE,
labels=4, lines=0,col.p=cluster,
xlab="Longitude",ylab="Latitude",cex=1)
clusplot is a function that performs a lot of magic for you. In particular it projects the data set - which happens in a way you don't like, unfortunately. (Also note the scales - it centered and scaled the data, too)
clusplot.default: Creates a bivariate plot visualizing a partition (clustering) of the data. All observation are represented by points in the plot, using principal components or multidimensional scaling.
As far as I can tell, clusplot doesn't have map support, but you will want such a map I guess...
While maybe you can use the s.x.2d parameter to specify the exact projection (and this way disable automatic scaling), it probably is still difficult to add the map. Maybe look at the source of clusplot instead, and take only the parts you want?

How to create variable sized square polygons to use for a choropleth map?

I have asked this question in the GIS part of stack exchange https://gis.stackexchange.com/questions/95265/r-how-to-create-a-pre-determined-number-of-identical-square-polygons-to-use-fo - I am asking it here as well as it has also topics of wider interest (e.g. calculation of density) - I hope not to be penalised for this! :)
I am trying to plot crime data density (again!) over a city map, say of NY. As a well known problem there are plenty of examples on this (http://www.obscureanalytics.com/2012/12/07/visualizing-baltimore-with-r-and-ggplot2-crime-data/). These methods plot the crime density through isoclines, while I need to represent it through identical density squares of a pre-determined area (and the area / side length may change from one iteration to the other). This is actually done in commercially available COTS packages like PredPol (see http://www.predpol.com). The reason for representing crime density through squares is that the square are the hotspot areas to be patrolled. The size will influence the overall amount of police people required.
This is what I am trying to achieve:
I would like to be able to create identical square polygons with pre-determined size to overimpose to the map (is it a raster? apologies but I've just started to learn to spell GIS!)
I would like to use the above squares as items to colour as in a choropleth map (i.e. different colouring in relation to frequency of crime in the area), probably using ggplot2 or similar.
This should allow me to see how the density of crimes per square kilometre varies changing the size (i.e. the area) of the square, proposing different patrolling areas.
I do not have a clue if it is possible to use R to create regularly shaped squares polygons of a pre-defined size to use for this (as the code snipped below attests). Any help or links to examples are welcome.
I would be glad to get some indication on alternative ways to calculate the density. I have used the stat_density2 (part of ggplot2) but maybe there are better / faster ways?
(
In hindsight, do I need a density function at all? I just need to count the crimes in a cell and colour-plot it accordingly...)
This is where I got to:
library(rgdal)
library(raster)
library(sp)
#NY boroughs shapefile downloaded from NY website
shp <- readOGR(dsn = "nybb_14a_av", layer = "nybb")
r <- raster(extent(shp))
res(r)=0.05
# using BoroCode as an experiment...
r <- rasterize(shp, field="BoroCode", r)
plot(r)
plot(shp,lwd=10,add=TRUE)
#don't know the result of the above: the laptop basically hangs processing
#plot(r) :)

R non gridded filled contour

I have a set of data that I'm trying to create a surface plot of. I have an x,y point and a to colour by.
I can create a xy plot with the points coloured but I can't find a way to create a surface plot with my data. The data isn't on a normal grid and I would prefer to not normalize it if possible (or I could just use a very fine grid).
The data won't be outside the a radius=1 circle so this part would need to be blank.
The code and the plot is shown below.
I've tried using contour, filled.contour as well as surface3d (not what I wanted). I'm not real familiar with many packages in R so I'm not even sure where to begin looking for this info.
Any help in creating this plot would be appreciated.
thanks,
Gordon
dip<-data.frame(dip=seq(0,90,10))
ddr<-data.frame(ddr=seq(0,350,10))
a<-merge(dip,ddr)
a$colour<-hsv(h=runif(nrow(a)))
degrees.to.radians<-function(degrees){
radians=degrees*pi/180
radians
}
a$equal_angle_x<-sin(degrees.to.radians(a$ddr))*tan(degrees.to.radians((90-a$dip)/2))
a$equal_angle_y<-cos(degrees.to.radians(a$ddr))*tan(degrees.to.radians((90-a$dip)/2))
plot(a$equal_angle_x,a$equal_angle_y,col=a$colour,lwd=10)
With regards to the plot I was trying to create is below. I believe the link in the first comment should get me where I'm trying to go.

Spacing of map scatter plot elements

I have a dense scatter plot on a map (produced using Python, matplotlib, and basemap). Here is a part of the image:
I'd like to solve the overlap problem. I think the way to do this is to combine this simple lat/lon coordinate mapping with the technique I often see implemented in those "spring-loaded" network (social, not computer) graphs.
Is there a simple existing algorithm to auto-magically move these points so that they are not overlapping? If so, I can easily than add a small line from each point to its the correct lat/lon coordinate where it is currently located.
Note: Hexbin and heatmap is not a solution since the discrete values are important and should not be compromised.

Simple Contour map

I just discovered ggmap and I've been playing around with plotting earthquake data from the USGS. I get the data in the form of Lat and Lon, depth and magnitude. I can easily plot the earthquakes as points with different colors based on depth but what I would like to do is take that depth data (just a single number) and generate contours to overlay on the map.
This seems like it should be MUCH more simple than the "Houston Crime" example I keep coming up on since I'm not doing any statistical "density" calculation or anything like that. Basically it's just a contour map on top of the google map of an area.
How do I do this (Presumably) simple, simple thing?
Thanks!
The problem of plotting a 3D surface using only a small sample of unequally spaced lat/long points and a height z (or equivalent) variable is non-trivial -- you have to estimate the values of z for all of the lat-long grid coordinates you do not have, for example using loess() or kriging to create a smooth surface.
Take a look at Methods for doing heatmaps, level / contour plots, and hexagonal binning, case #5. For a geoR example see http://www4.stat.ncsu.edu/~reich/CUSP/Ordinary_Kriging_in_R.pdf

Resources