Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to plot a heat map on a spatial map

I am new to spatial data analysis in R and would like to do something easy, I am still having difficulties... I have a big table with latitudes and longitudes

sample = structure(list(Longitude = c(-0.19117, -0.211708, -0.206458, 
-0.173862, -0.156618), Latitude = c(51.489096, 51.520075, 51.525301, 
51.482442, 51.495752), Location_Easting_OSGR = c(525680L, 524170L, 
524520L, 526900L, 528060L), Location_Northing_OSGR = c(178240L, 
181650L, 182240L, 177530L, 179040L)), .Names = c("Longitude", 
"Latitude", "Location_Easting_OSGR", "Location_Northing_OSGR"
), row.names = c(NA, -5L), class = c("data.table", "data.frame"
))

I got a map of the UK from GADM (level 2 of UK map).

enter image description here

I would like to be able to

  1. plot points defined by longitude/latitude on the map
  2. build a heat map that shows where the points are more concentrated...

Is it easy ? If not do you have some pointers (only UK please) Cheers

like image 309
statquant Avatar asked Feb 15 '14 19:02

statquant


People also ask

Can I plot my own Heatmaps on the map?

Heatmaps are a great way of visualization, but plotting them on a map can be a challenge. Learn how to create immersive maps using geojsoncountour to create custom geojson data.

What is a geographical heatmap?

A geographical heatmap is a spatial map to visualize data according to geographical location. This can be done to show the phenomenon’s intensity, such as weather trends or demographic information. The heatmap quality will be determined by the density of the dots in the map, and the color spread.

How do I create a heat map in Excel?

Drag a point dataset to the page and drop it on the Map drop zone. You can also create a map by selecting a field and clicking the Map button above the data pane. Expand the legend to display the Layer options pane. Browse to the Options tab . Change Symbol type to Heat Map.

What are geographic heat maps and choropleth maps?

Geographic Heat Maps and Choropleth maps are part of Excel charts. You can create the heat maps in excel and embed them in your presentation as any other kind of chart. Heatmaps, be they spatial or grid heatmaps, can be extremely powerful visualizations to represent the intensity of a phenomenon.


1 Answers

Is this what you had in mind?

Your sample was too small to demonstrate a heat map, so I created a bigger sample with artificial clusters at (long,lat) = (-1,52), (-2,54) and (-4.5,56). IMO the map would be more informative without the points.

Also, I downloaded the shapefile, not the .Rdata, and imported that. The reason is that you are much more likely to find shapefiles in other projects, and it is easy to import them into R.

setwd("< directory with all your files>")
library(rgdal)         # for readOGR(...)
library(ggplot2)
library(RColorBrewer)  # for brewer.pal(...)

sample <- data.frame(Longitude=c(-1+rnorm(50,0,.5),-2+rnorm(50,0,0.5),-4.5+rnorm(50,0,.5)),
                     Latitude =c(52+rnorm(50,0,.5),54+rnorm(50,0,0.5),56+rnorm(50,0,.5)))
UKmap  <- readOGR(dsn=".",layer="GBR_adm2")
map.df <- fortify(UKmap)

ggplot(sample, aes(x=Longitude, y=Latitude)) + 
  stat_density2d(aes(fill = ..level..), alpha=0.5, geom="polygon")+
  geom_point(colour="red")+
  geom_path(data=map.df,aes(x=long, y=lat,group=group), colour="grey50")+
  scale_fill_gradientn(colours=rev(brewer.pal(7,"Spectral")))+
  xlim(-10,+2.5) +
  coord_fixed()

Explanation:

This approach uses the ggplot package, which allows you to create layers and then render the map. The calls do the following:

ggplot -         establish `sample` as the default dataset and define (Longitude,Latitude) as (x,y)
stat_density2d - heat map layer; polygons with fill color based on relative frequency of points
geom_point -     the points
geom_path -      the map (boundaries of the admin regions)
scale_fill_gradientn - defines which colors to use for the fill
xlim -           x-axis limits
coord_fixed -    force aspect ratio = 1, so map is not distorted
like image 192
jlhoward Avatar answered Sep 30 '22 02:09

jlhoward