# [R-sig-Geo] Kernel density plot on a US map

Ashton Shortridge ashton at msu.edu
Tue Mar 22 16:05:07 CET 2011

```Hi Ravi,

> generating the kernel density plots but unable to superimpose the density
> plot on the US map???
Check out the following R code.

library(spatstat)
library(maps)
library(maptools)

# Make a random set of lat-lon pairs with values
usdat <- data.frame(x=runif(50, -115, -85), y=runif(50, 33, 41), z=runif(50,
0, 100))

map('usa')
points(usdat\$x, usdat\$y)  # do they fall in there?

# Create an owin object from the USA outline(s)
usmap <- map('usa', fill=TRUE, col="transparent", plot=FALSE)
uspoly <- map2SpatialPolygons(usmap, IDs=usmap\$names,
proj4string=CRS("+proj=longlat +datum=wgs84"))
spatstat.options(checkpolygons=FALSE)
usowin <- as.owin.SpatialPolygons(uspoly)
spatstat.options(checkpolygons=TRUE)

# Create a spatstat ppp object
pts <- as.ppp(usdat, W=usowin)
plot(pts)

# Plot a a density surface
plot(density(pts))

# Plot a smoothed surface using the weights
plot(smooth.ppp(pts, 3))

On 2011-03-22, vioravis, wrote:
> I am trying to plot the kernel density plot superimposed on a US map. The
> data consists of the the latitude and the longitude of a few locations in
> the US and a number (such  as count) corresponding to each location. I
> would like to generate a kernel density plot with the count as the
> variable superimposed on US map so that I can visualize how the count
> varies across the US. I have looked at packages such as spatstat about
> generating the kernel density plots but unable to superimpose the density
> plot on the US map???
>
> Can someone help me on how to do this in R?
>
> Thank you.
>
> Ravi
>
> --
> View this message in context:
> http://r-sig-geo.2731867.n2.nabble.com/Kernel-density-plot-on-a-US-map-tp6
> 196265p6196265.html Sent from the R-sig-geo mailing list archive at
> Nabble.com.
>
> _______________________________________________
> R-sig-Geo mailing list
> R-sig-Geo at r-project.org
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo

-----
Ashton Shortridge
Associate Professor			ashton at msu.edu
Dept of Geography			http://www.msu.edu/~ashton
235 Geography Building		ph (517) 432-3561
Michigan State University		fx (517) 432-1671

```