Pumps Data

This is a demonstration of basic point plotting from a CSV file and reading data from DBF files.

CSV Format Notes

Data are read from a spreadsheet format (CSV).

The latitude and longitude are not correct. The wrong translation of units was done.

Much of the code here sets up the use of sitemaps, the package for simple data point plotting.

## Load necessary libraries
library(ggplot2)
library(ggmap)
library(dplyr)
library(gt)

## Load sitemaps
## library(devtools)
## install_github("kimbridges/sitemaps") ## Do this just once
library(sitemaps)

## Use two functions from sitemaps to initialize parameters
column <- site_styles()
hide   <- site_google_hides()

column$margin = 2.0

## Register the Google Maps Key
googleKey <- Sys.getenv("GGMAP_GOOGLE_API_KEY")
register_google(key = googleKey, account_type = "standard")

## Establish a theme that improves the appearance of a map.
## This theme removes the axis labels and 
## puts a border around the map. No legend.
simple_black_box <- theme_void() +
              theme(panel.border = element_rect(
                color     = "black", 
                fill      = NA, 
                linewidth = 2),
                legend.position = "none")

## Load the data
data <- read.csv("Pump_Data_Table.csv")

## Rename the ID field to text
data <- data |>
  dplyr::rename(text = ID) |>
  dplyr::rename(lat = Latitude) |>
  dplyr::rename(lon = Longitude) 

gt(data) |>
  ## Title on the table
  tab_caption(caption = "CSV Input Example") |>
  ## Source Information
  tab_source_note(
    source_note = "Source: Pump_Data_Table.csv") |>
  tab_footnote(
    footnote  = "Likely a bad conversion to latitude, longitude",
    locations = cells_column_labels(columns=c(lon, lat)))
CSV Input Example
text Name Address lon1 lat1
1 Oxford Market Oxford Market 6.98502 57.11404
6 Little Marborough St. Little Marblborough St. 6.98497 57.11042
2 Hanover Square Hanover Sq 6.98239 57.11044
4 Berners St. Berner's St. 6.98777 57.11340
5 Newman St. Newman St. 6.98863 57.11366
7 Broad St. Broad St. 6.98707 57.10973
3 Castle St. E. Castle St. E. 6.98612 57.11405
12 Picadilly Circus Tichbone St. 6.98894 57.10546
8 Bridle St. Bridle St. 6.98764 57.10733
9 Rupert St. Rupert St. 6.98928 57.10816
10 Warwick St. Warwick St. 6.98600 57.10702
11 Dean St. King St. 6.99074 57.10846
14 Vigo St. Vigo St. 6.98498 57.10538
13 Marlborough Mews Marlborough Mews 6.98500 57.11181
0 Oxford St. Smithy 365 Oxford St. 6.98497 57.11234
1 Likely a bad conversion to latitude, longitude
Source: Pump_Data_Table.csv
## Create the base map
basemap <- site_google_basemap(datatable = data)

## Plot the data points on the map
ggmap(basemap) +
  site_points(datatable = data) +
  site_labels(datatable = data) +
  theme_minimal()

DBF Format Notes

This appears to the a primary data source.

It’s always a good idea to look at the data.

## Install package "foreign" to read the dbf format data
library(foreign)
library(gt)

## Read the data
pumpsAll_data <- read.dbf("pumpsAll.dbf", as.is = FALSE)

## Get a data check
gt(pumpsAll_data)|>
  ## Title on the table
  tab_caption(caption = "DBF Input Example") |>
  ## Source Information
  tab_source_note(
    source_note = "Source: PumpAll.dbf") |>
  ## Positioning data in the cells
  cols_align(
    align = "left",
    columns = c(NAME, ADDRESS, Author)) |>
  tab_style(
    style = "vertical-align:top",
    locations = cells_body()) # Targets all data cells in the table body
DBF Input Example
Id NAME ADDRESS Number_ Author POINT_X POINT_Y cooper percentage
1 Oxford Market Oxford Market 10 Snow'55b 698501.8 5711404 no NA
6 Little Marborough St. Little Marblborough St. 2 cdc, Snow'55b, rW 698496.8 5711042 yes 4
2 Hanover Square Hanover Sq 11 Snow'55b 698238.8 5711044 no NA
4 Berners St. Berner's St. 3 cdc, Snow'55b, W 698776.7 5711340 no 2
5 Newman St. Newman St. 8 cdc, Snow55b, W 698862.9 5711366 no NA
7 Broad St. Broad St. 4 cdc, Snow'55b, W 698707.2 5710973 yes 17
3 Castle St. E. Castle St. E. 14 Snow'55b 698612.5 5711405 no NA
12 Picadilly Circus Tichbone St. 12 cdc, Snow'55b, revW 698893.9 5710546 no NA
8 Bridle St. Bridle St. 5 cdc, Snow'55b, W 698763.9 5710733 yes 2
9 Rupert St. Rupert St. 9 cdc, Snow'55b, W 698928.2 5710816 yes 4
10 Warwick St. Warwick St. 6 cdc, Snow'55b, W 698599.7 5710702 no NA
11 Dean St. King St. 7 cdc, Snow'55b, W 699073.8 5710846 no NA
14 Vigo St. Vigo St. 1 cdc, Snow'55b 698498.0 5710538 no NA
13 Marlborough Mews Marlborough Mews 13 cdc, Snow'55b, W 698499.7 5711181 yes NA
0 Oxford St. Smithy 365 Oxford St. 0 W 698496.8 5711234 yes NA
Source: PumpAll.dbf

Coordinate conversion

The coordinates are in UTM values, so a primary task is converting them to latitude and longitude coordinates.

library(sf)
library(dplyr)
library(gt)

## Convert the data
new_data <- pumpsAll_data |>
  
  ## treat existing lon/lat as UTM coordinates
  st_as_sf(coords = c("POINT_X", "POINT_Y"), crs = 32630) |>
  
  ## transform to geographic coordinates
  st_transform(4326)  |>
  
  ## extract and name the lon/lat
  mutate(
    lon = st_coordinates(geometry)[, 1],
    lat = st_coordinates(geometry)[, 2]) |>
  dplyr::rename(text = Id) |>
  
  ## drop sf geometry to get a plain data frame
  st_drop_geometry()

## Data check
gt(new_data)|>
  ## Title on the table
  tab_header(
    title = "DBF Input",
    subtitle = "Coordinates converted to Latitude, Longitude")|>
  ## Source Information
  tab_source_note(
    source_note = "Source: PumpAll.dbf") |>
  ## Positioning data in the cells
  cols_align(
    align = "left",
    columns = c(NAME, ADDRESS, Author)) |>
  tab_style(
    style = "vertical-align:top",
    locations = cells_body()) |> # All data cells in the table body
  ## Format the geographic coordinates
  fmt_number(columns = c(lat,lon), decimals = 5) 
DBF Input
Coordinates converted to Latitude, Longitude
text NAME ADDRESS Number_ Author cooper percentage lon lat
1 Oxford Market Oxford Market 10 Snow'55b no NA −0.13882 51.51883
6 Little Marborough St. Little Marblborough St. 2 cdc, Snow'55b, rW yes 4 −0.13910 51.51557
2 Hanover Square Hanover Sq 11 Snow'55b no NA −0.14281 51.51568
4 Berners St. Berner's St. 3 cdc, Snow'55b, W no 2 −0.13490 51.51815
5 Newman St. Newman St. 8 cdc, Snow55b, W no NA −0.13365 51.51835
7 Broad St. Broad St. 4 cdc, Snow'55b, W yes 17 −0.13611 51.51488
3 Castle St. E. Castle St. E. 14 Snow'55b no NA −0.13723 51.51880
12 Picadilly Circus Tichbone St. 12 cdc, Snow'55b, revW no NA −0.13366 51.51098
8 Bridle St. Bridle St. 5 cdc, Snow'55b, W yes 2 −0.13543 51.51271
9 Rupert St. Rupert St. 9 cdc, Snow'55b, W yes 4 −0.13302 51.51340
10 Warwick St. Warwick St. 6 cdc, Snow'55b, W no NA −0.13781 51.51249
11 Dean St. King St. 7 cdc, Snow'55b, W no NA −0.13090 51.51361
14 Vigo St. Vigo St. 1 cdc, Snow'55b no NA −0.13937 51.51105
13 Marlborough Mews Marlborough Mews 13 cdc, Snow'55b, W yes NA −0.13898 51.51682
0 Oxford St. Smithy 365 Oxford St. 0 W yes NA −0.13899 51.51730
Source: PumpAll.dbf
########## MAPPING ##################

## Adjust the basemap coverage 
## Without this, the basemap shows too large an area
column$margin <- 0.4

## Adjust the size of the labels (pump Id)
new_data$label_text_size <- 3

## Create the base map
basemap <- site_google_basemap(datatable = new_data)

## Plot the data points on the map
ggmap(basemap) +
  site_points(datatable = new_data) +
  site_labels(datatable = new_data) +
  theme_minimal()