Making a dot density map with tmap package - dot

I am trying to make a map using the tmap package, but i don't know the code to do a dot density map. The dataset i have consists of births, and i want to plot the number of births on each region of my map as dots (regions with higher number of births would have higher number of dots). I am not sure if tmap has a function like this, I only know tm_bubble. The map would look like this:

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.

Plot points on map R maps package

I have a dataset of all powerplants and I've got their locations down to the format the maps package in R likes c("arkansas,clay", "arkansas,conway", ...).
Some counties have more than one powerplant, and there are 7+ types of powerplants, so I'd like to plot them as points on a map and not just color the counties, as I can see the maps package mainly doing. Was thinking to jitter their position a bit. But I don't know how to go from state/county name to location, or plot straight up points in the maps package.
Does anyone have any suggestions?
So I couldn't figure out how to do it with the maps package, but with ggplot, it's almost trivial. The first few lines of this answer made it really easy to construct a plot I needed.
Plotting bar charts on map using ggplot2?
One trick I did use was to create R's version of a hastable from the map_data in ggplot2.
usaMap=maps_data("county")
usaMap$locCode=paste(usaMap$region,",",usaMap$subregion,sep="")
usaMap2 = usaMap[!duplicated(usaMap$locCode),]
row.names(usaMap2)=usaMap2$locCode
currentGen$long = usaMap2[currentGen$locCode,"long"]+rnorm(nrow(currentGen),0,.05)
currentGen$lat = usaMap2[currentGen$locCode,"lat"]+rnorm(nrow(currentGen),0,.05)
where currentGen is my powerplants data frame and the format of the region matches exactly the format of usaMap$locCode.

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

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

R: How to overlay pie charts on 'dots' in a scatterplot in R

Using R I would like to replace the points in a 2d scatter plot by a pie chart displaying additional values.
The rational behind is that I have time series data for hundreds of elements (proteins) derived from a biological experiment monitored for 4 conditions. I would like to plot the elements (categorial data) on the y axis and occurrence of a event in time on the x axis. To visualize the relative occurrence between the 4 conditions I would like to visualize this in form of a pie chart or doughnut chart overplayed onto the respective point in the scatter plot.
The overall data density is low so overlapping won't be an issue.
Is this possible in R?
I was thinking of using a manual scale in ggplot2 but could not figure out how to define a pie chart as a scale.
Also of interest would be how to best cluster this data and sort it accordingly.
Yes. pieGlyph() is one ready-to-go function from the Rgraphviz package.
Also, I would check out this Q/A for how to do things like this more generally:
How to fill a single 'pch' point on the plot with two-colours?
Especially check out ?my.symbols from the TeachingDemos package.
Lastly, in regards to ggplot2, you should check out this blog post about possible upcoming features:
http://blog.revolutionanalytics.com/2011/10/ggplot2-for-big-data.html
See also Paul Murrell. Integrating grid graphics output with base graphics output. R News, 3(2):7-12, October 2003. http://www.r-project.org/doc/Rnews/Rnews_2003-2.pdf
The code on pp 10-11 sets up the main plot axes, labels and legend, and then opens a series of smaller windows centered at each individual point on the plot and plots a small graph in each window. I've tried pie charts, mosaics and barplots, but the method is not limited to these types.

Resources