3

Please help the R newbie

I have a list of UK postcodes - 3m observations in fact. What is the best way of plotting them on to a map using R?

Thanks

epo3
  • 2,991
  • 2
  • 33
  • 60
JVT
  • 63
  • 1
  • 1
  • 3
  • What does your list look like? Have you just got a text file? What does that look like? When you say "on a map", do you mean "on a base map" or just on an outline of the UK? What do you expect to get from 3million points on a plot? – Spacedman Mar 13 '15 at 16:10
  • The postcodes are in a csv file. It's customer data for a major retailer which I'd like to plot on a map to see how close online customers are to physical stores. – JVT Mar 13 '15 at 17:23
  • Read the docs: http://cran.r-project.org/web/views/Spatial.html – Spacedman Mar 13 '15 at 17:28

1 Answers1

13

Step 1 - Download Uk PostalCode geographic information: For example http://www.doogal.co.uk/UKPostcodes.php

https://data.gov.uk/search?q=postcodes

http://www.freemaptools.com/download-uk-postcode-lat-lng.htm

You may choose the format of your choice. CSV is easy to work with. Import this table into R.

Step 2 - Create a Subset with your list

#You have now a data.frame Df_UK containing geoinfo.
#Your initial list is in Df_JVT with variable PostCodes.
list <- as.list(unique(Df_JVT$PostCodes))
#Select your postcodes from Df_UK and choose variable to display on the map
datamap <- subset(Df_UK, Df_UK$POSTNR %in% list, select= c("POSTNR","CITY", "COUNTY", "LAT",  "LON"))  
row.names(datamap) <- 1:nrow(datamap)

Step 3 - Create a spatial object and plot the map

#Transform data.frame in spatial object
require(rgdal)
require(sp)
require(plotGoogleMaps)

datamap_mat<- cbind(datamap$LON,datamap$LAT)
row.names(datamap_mat) <- 1:nrow(datamap_mat)
AACRS <- CRS("+proj=longlat +ellps=WGS84")

UK_Map <- SpatialPointsDataFrame(datamap_mat, datamap, proj4string = AACRS, match.ID = TRUE) 


#Map Points on Googlemaps
m <- plotGoogleMaps(UK_Map , filename='MAP_UK.html')
luchonacho
  • 6,759
  • 4
  • 35
  • 52
Kvasir EnDevenir
  • 907
  • 1
  • 10
  • 25