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-tp6196265p6196265.html Sent from the R-sig-geo mailing list archive at Nabble.com.
Kernel density plot on a US map
5 messages · Ashton Shortridge, vioravis, Rolf Turner
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
Hi Ashton, Thank you. This is exactly what I was looking for!! It works fine with the test data you have created but when I try it with the actual data it throws the following error: Error in `marks<-.ppp`(`*tmp*`, value = c(51L, 14L, 26L, 2L, 108L, 40L, : number of points != number of marks In addition: Warning message: In ppp(X[, 1], X[, 2], window = win, check = check) : 7 points were rejected as lying outside the specified window Could you please let me know what is causing this error??? Also, could you please let me know the interpretation of smooth.ppp plot. Thank you. Ravi -- View this message in context: http://r-sig-geo.2731867.n2.nabble.com/Kernel-density-plot-on-a-US-map-tp6196265p6197393.html Sent from the R-sig-geo mailing list archive at Nabble.com.
After a few trials it appears that the cause of the errors is the points lying outside the specified window. 1. As a quick fix, I could remove the points (only a few) and do the analysis. How do I check whether a point lies within the window or not??? 2. I have a few points in Alaska and Hawaii that do not appear in the map. How do I add those regions to the US map??? Please let me know. Thank you. Ravi -- View this message in context: http://r-sig-geo.2731867.n2.nabble.com/Kernel-density-plot-on-a-US-map-tp6196265p6199778.html Sent from the R-sig-geo mailing list archive at Nabble.com.
On 24/03/11 01:20, vioravis wrote:
After a few trials it appears that the cause of the errors is the points lying outside the specified window. 1. As a quick fix, I could remove the points (only a few) and do the analysis. How do I check whether a point lies within the window or not???
?inside.owin
cheers,
Rolf Turner