Intensity plot in map using googleVis in R - r

I have a long list (around 900 thousand) of latitude and longitude (please note that these coordinates vary up to 13th decimal places) with category of crime done in this coordinates.
This is snapshot of data
Problem: I want to plot a heat map irrespective of type of crime done using only latitude and longitude. something like this
This is what I have tried using googleVis
M1 <- gvisMap(df_latlong, "LatLong" , "Category",
options=list(showTip=TRUE, showLine=TRUE, enableScrollWheel=TRUE,
mapType='hybrid', useMapTypeControl=TRUE,
width=800,height=600))
plot(M1)
But this is something else and it is ignoring most of the data. I have no clue how to do it.
Thanks a lot!

Related

plotting polygons in python using geopandas

I have a data frame with latitude, longitude and annual consumption data. I am trying to plot the annual consumption data using GeoPandas with a quantile scheme by making a polygon column. I followed the following tutorial.
enter link description here
I used the latitude and longitude data to make polygon shapes using the following code.
full_dataframe["geometry"] = Polygon(list(zip(full_dataframe["long"], full_dataframe["lat"])))
merged2 = GeoDataFrame(full_dataframe)
merged2.plot(column='annual_consume', scheme='quantiles', k=4, edgecolor='k',
cmap='OrRd', legend=True,
legend_kwds={'loc': 'center left', 'bbox_to_anchor':(1,0.5)})
My data frame looks like below
The issue is, when I plot it, it looks like a big mess of interconnecting points. The boundaries are not seperated.
I want a similar boundary separation like the tutorial based on longitude and latitude data.

Trying to convert Numerical Values of Lat/Long into Spatial Data

I am working with a dataset that features chemical analyses from different locations within a cave, with each analysis ordered by a site number and that sites latitude and longitude. This first image is what I had done originally simply using ggplot.
Concentrations mapped by color over map
But what I want to do is use the shapefile of the cave system from which the data is sourced from and do something similar by plotting the points over the system and then coloring them by concentration. This below is the shapefile that I uploaded
Cave system shapefile
So basically I want to be able to map the chemical data from my dataset used to map the first figure, but on the map of the shapefile. Initially it kept on saying that it could not plot on top of it. So I figured I had to convert the latitude and longitude into spatial coordinates that could then be mapped on the shapefile.
Master_Cave_data <- Master_cave_data %>%
st_as_sf(MastMaster_cave_data, agr = "identity", coord = Lat_DD)
This was what I had thought to use in order to convert the numerical Latitude cooridnates into spatial data.

Interactive plot for spatial data in R markdown (to html)

So basically I have some spatial data, which I've found weighted matrix by distance dnearneigh in R and I am wondering if I can generate an interactive plot of the link distribution by changing the distance variable for the weighted matrix.
The data set:
CA.poly <- readShapePoly('CaliforniaCounty.shp')
This is a shapefile for California county and using this I can generate a weight matrix based on the distance of each county.
coords<-coordinates(CA.poly)
W_dist<-dnearneigh(coords,0,1.5,longlat = FALSE)
And after generating the matrix I can plot the link distribution by using:
plot(W_dist,coordinates(CA.poly))
This will show a network of counties where two counties are connected if their distance (between centroids) are less than 1.5 km.
All the codes are in a Rmd file and I am wondering is there a way to output in html an interactive plot where you (user) can change the distance parameter (change 1.5 km to 1 km for example) and the graph will change.
I looked up methods like using shiny and plotly but I don't think they suit my goal. Any suggestions?

Creating a raster object from a gridded data set in R

I have a dataset with dimensions 360x180x720 (lon x lat x time). I would like to change its resolution from 1 deg to 0.25 deg using the disaggregate function that's part of R's raster package. I haven't been able to figure out how to create a raster object from this dataset. I think I need to do something involving stacking the data corresponding to each time step in layers, but I what I've tried so far has been unsuccessful. I apologize if this is a trivial question, but most of the online guidance I've seen has been directed towards data formatted spreadsheet style in (lat, lon) pairs (rather than a gridded dataset).
I appreciate any suggestions!

R Converting contour lines to elevation plot

I would like to be able to create an elevation plot from contour lines in R. I am very new to using shape files
At the moment I have downloaded data from here
which provides .shp files for all of the UK.
It also provides the contour lines, summarising the topology of the UK.
For the elevation plot I would like a data.frame or data.table of evenly spaced points (100m apart from each other) to produce a data output giving an x, y and z value. Where x and y represent the latitude and longitude (or Eastings and Northings), and z represent the height (in meters above sea-level).
I think there are probably some tools that will automatically carry out the interpolation for you, but am unsure how it would work with geo-spatial data.
This is my basic start...
require(maptools)
xx <- readShapeSpatial("HP40_line.shp")
Choose "ASCII Grid and GML (Grid)" as download format for the "OS Terrain 50" product, and download the file. This will give you a zip file containing many directories of zip files, each of which contains portions of a 50 m elevation grid of the UK (the portion I looked at had 200 x 200 cells, meaning 10 km x 10 km). I went into the directory data/su, unzipped the zip file there, and did
library(raster)
r = raster("SU99.asc")
plot(r)
to aggregate this to a 100 m grid, I did
r100 = aggregate(r) # default is factor 2: 50 -> 100 m
As mentioned above, the advice is to work on the grids as contour lines are derived from grids, working the other way around is a painful and a great loss of information.
Getting grid values in longitude latitude as a data.frame can be done in two ways:
df = as.data.frame(projectRaster(r, crs = CRS("+proj=longlat")), xy = TRUE)
unprojects the grid to a new grid in longitude / latitude. As these grids cannot coincide, it minimally moves points (see ?projectRaster).
The second option is to convert the grid to points, and unproject these to longitude latitude, by
df2 = as.data.frame(spTransform(as(r, "SpatialPointsDataFrame"), CRS("+proj=longlat")))
This does not move points, and as a consequence does not result in a grid.

Resources