How to create a weighted heatmap using R Leaflet? - r

I am attempting to create a heatmap using a data set that has only one value per coordinate, with that value being a continuous variable. All of the examples I have found using leaflet.extras::addHeatmap() use data that can have multiple values per coordinate, and create the heatmap based on the density of counts in an area. There doesn't seem to be a way to pass a weight instead.
My ultimate goal is to have something that looks like a raster based on these values:
However I don't want to use a raster due to the pixelation along the coasts.
When I pass the data to addHeatmap() and include the argument intensity = ~my_weighted_value, I get something like this:
And at increased zoom levels, it just ends up being a bunch of circles:
What is the proper way to take weighted spatial data and add a heatmap that looks like the raster?

Try to scale my_weighted_value back by *.00001 or something. Your weighted value appears to be exceeding the max.

Related

Overlapping data contour on a map

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.

How to create bins in a reliability diagram

I created a logistic/logit model with a binomial response variable using
model <- glm(response~predictor1+predictor2+...)
and then I used the predict function to create a new data frame
outcome <-data.frame(predict(model,newdata=IndependentDataSet,type="response"),as.numeric(as.character(Independent$ResponseVariable)))
names(outcome) <- c("Pr","Obs")
I can use one of the following functions
plot(verify(data$obs,data$pr),CI=TRUE)
attribute(verify(data$obs,data$pr))
to create a plot that looks like this
or
reliability.plot(verify(data$obs,data$pr))
from
library(verification)
to create a reliability diagram. I am wondering how I can separate the bins based on specific values. For example, the model that I am evaluating is based around a climatology of 19% (0.19) and I want there to be a bin at (1/3)*climatology, climatology, and go up by (2/3) of climatology for the proceeding bins. How can I do this?
Additionally, I have seen the bins represented as circles that are proportional in size to the percent of the data that is at that bin. Does anyone know how to make a more aesthetically pleasing reliability diagram in R? Any recommendations are welcome.
This is how I would like my diagrams to appear
The easiest could be using
trace("attribute.default",edit=TRUE)
or whichever other function.
In this way, you access the source code and edit it. These changes affect only the current R session.

Using a 3rd variable rather than count in hexbin

I'm afraid this is a slightly abstract question, but is there a way to use a metric other than 'count' in hexbin. My code to produce a hexbin chart would be something like:
library(hexbin)
hexbinplot(latitude~longitude,data=X)
Given that my output is essentially a map, I was wanting to replace 'count'and make the colours of the hexbins relate to a 3rd variable at the aggregate level, eg say I were looking at demographic data, then the 3rd variable might be aggregate or average consumer spending power of all those people belonging to a geographical region defined by the hexbin.
I understand that this would be possible by conditioning on a third variable in 2 windows in the normal lattice/trellis way, but I'm not sure it is possible to have a representation of something other than 'count' in the chart. Thanks for any help.

Plot map of points spread into grid sub-samples

I'm trying to generate a map that looks like this in R:
The boxes represent individual observations, while the colors represent data pertaining to those individual observations. Anyone have any idea how this might be accomplished?

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