Mapping and Geographical Patterns

Overview


  • Themes
    • Interactive Data Analysis
    • Exploratory Spatial Data Analysis
    • Time Series
    • Maps with Backdrops

Initial load up of some libraries and data…


# %>% and stuff
library(tidyverse)  
# handle spatial data
library(sf)
# 'nice' color palettes
library(RColorBrewer)
# plotting libraries
library(ggplot2)
library(plotly)
# The rainfall data - as in last lecture
load("rainfall.RData")

Working with spatial data - 1

  • The stations data frame gives geographical information about the weather stations:
stations
## # A tibble: 25 x 9
##    Station Elevation Easting Northing   Lat  Long County Abbreviation
##    <chr>       <int>   <dbl>    <dbl> <dbl> <dbl> <chr>  <chr>       
##  1 Athboy         87 270400   261700   53.6 -6.93 Meath  AB          
##  2 Foulks…        71 284100   118400   52.3 -6.77 Wexfo… F           
##  3 Mullin…       112 241780   247765   53.5 -7.37 Westm… M           
##  4 Portlaw         8 246600   115200   52.3 -7.31 Water… P           
##  5 Rathdr…       131 319700   186000   52.9 -6.22 Wickl… RD          
##  6 Stroke…        49 194500   279100   53.8 -8.1  Rosco… S           
##  7 Univer…        14 129000   225600   53.3 -9.06 Galway UCG         
##  8 Drumsna        45 200000   295800   53.9 -8    Leitr… DAL         
##  9 Ardara         15 180788.  394679.  54.8 -8.29 Doneg… AR          
## 10 Armagh         62 287831.  345772.  54.4 -6.64 Armagh A           
## # … with 15 more rows, and 1 more variable: Source <chr>

Working with spatial data - 2

  • Make this into a simplefeatures spatial object
  • This is like a data frame but has geographical structure
  • Here each station is represented as a point with a latitude and longitude
stations.sf <- st_as_sf(stations,coords = c("Long","Lat"),crs=4326)
head(stations.sf,n=4)
## Simple feature collection with 4 features and 7 fields
## geometry type:  POINT
## dimension:      XY
## bbox:           xmin: -7.37 ymin: 52.28 xmax: -6.77 ymax: 53.6
## epsg (SRID):    4326
## proj4string:    +proj=longlat +datum=WGS84 +no_defs
## # A tibble: 4 x 8
##   Station Elevation Easting Northing County Abbreviation Source
##   <chr>       <int>   <dbl>    <dbl> <chr>  <chr>        <chr> 
## 1 Athboy         87  270400   261700 Meath  AB           Met E…
## 2 Foulks…        71  284100   118400 Wexfo… F            Met E…
## 3 Mullin…       112  241780   247765 Westm… M            Met E…
## 4 Portlaw         8  246600   115200 Water… P            Met E…
## # … with 1 more variable: geometry <POINT [°]>

Working with spatial data - 3

Using Mapbox

  • To create mapbox maps with Plotly, you’ll need a Mapbox account and a Mapbox Access Token that you can add to your Plotly Settings.
  • Follow the links above, then in R enter:
library(plotly)
Sys.setenv('MAPBOX_TOKEN' = 'your_mapbox_token_here')
  • Obviously, put your actual token!
  • NB You can use mapbox for free.

Working with spatial data - 4

  • Finally you can create a map
plot_mapbox(stations.sf,color=~Source,text=~Station) -> sources 
sources

Maps with size values (proportional symbols)

  • Here we use a summarise operation on the rain data, and then join it to the stations sf object
rain %>% group_by(Station) %>% summarise(Rainfall=mean(Rainfall)) -> mean_rain
stations.sf %>% left_join(mean_rain) -> stations2.sf
plot_mapbox(stations2.sf,color=I('black' ),text=~Station, size=~Rainfall) 

Maps with colour values

plot_mapbox(stations2.sf,color=~Rainfall,text=~Station, size=80, hoverinfo="text") %>%
  colorbar(title="Mean Monthly \n Rainfall (mm)") %>% layout(mapbox = list(style = "light"))

Working with spatial data - Option 2

  • Create a map without backdrop
  • This is possible with ggplot and geom_sf
  • Note You will need to download All_Ireland.RData from the Moodle site for this module in order to use the sf data to create the counties.
load('All_Ireland.RData')
ggplot(all_counties.sf) + 
  geom_sf(fill='grey40') + 
  geom_sf(data=stations2.sf,
          aes(color=Rainfall,text=Station),size=3) +  
  scale_color_viridis_c() -> rain_map
ggplotly(rain_map,tooltip="text")

Working with spatial data - dynamic plots

  • Create a map without backdrop
  • This is possible with ggplot and geom_sf
  • Note You will need to download All_Ireland.RData from the Moodle site for this module in order to use the sf data to create the counties.
stations2.sf %>% highlight_key(~Station,"Station") -> stations2.hk
ggplot(all_counties.sf) + 
  geom_sf(fill='grey40') + 
  geom_sf(data=stations2.hk,
          aes(text=Station),size=3) +  
  scale_color_viridis_c() -> rain_map2
plt1 <- ggplotly(rain_map2,tooltip="text",source="P1") %>% 
   highlight(on = "plotly_selected", off = "plotly_deselect", color='yellow') 
scatter <- ggplot(stations2.hk,aes(x=Elevation,y=Rainfall,text=Station)) + geom_point() 
plt2 <- ggplotly(scatter,tooltip="text",source="P2") %>% 
   highlight(on = "plotly_selected", off = "plotly_deselect", color = 'yellow') 
subplot(plt1,plt2)

Conclusion

💡 New ideas


  • New R ideas
    • plotly for cartography
    • geom_sf for cartography
  • Next lecture
    • Reproducible research
    • Using RMarkdown