-6

I have x,y coordinates of car accidents within a city. I would like to create hotspots map using kernel density estimation in R. anyone can help with the code ?

  • If you want anybody to really help you with this please [provide a reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). For example update your question with the code you have tried with the geyser data from MASS, so we can see what the error is. – Ege Rubak Aug 17 '17 at 08:30

1 Answers1

2

The spatstat package makes this easy. Since I don't know what your data looks like, I'll start by creating some demo data.

x <- rnorm(327)
y <- runif(327)

Now convert the data into a geospatial format using the ppp() function from spatstat.

library(spatstat)
dta <- ppp(x, y, window = owin(c(-5, 5), c(-5, 5)))

Finally, compute the density and then plot the result.

# Compute the density function
dta <- density(dta)

# Plot the density
plot(dta, main = "Density plot of sample dataset")

enter image description here

Andrew Brēza
  • 7,705
  • 3
  • 34
  • 40
  • Thank u very much. I am trying to use a different data which has few hotspots and not only one as in your example but i always get 1 hotspot. I tried "geyser" data from MASS library which has 3 hotspots. why it doesnt work? – yoni cohen Aug 15 '17 at 15:55