Search code examples
rmappingkernel-density

hotspots map using kernel density estimation in R


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 ?


Solution

  • 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