22 July 2016

Plotting Maps

Let's Setup The Workspace

install.packages("maps")
library(maps)
library(ggplot2)
library(dplyr)

Getting the crude data

ggplot2 has function map_data()

world_map <- map_data("world")

Let's check it out

head(world_map)

Getting the crude data

How big is the world map?

dim(world_map)

How much information is there for each region?

table(world_map$region)

Getting Australian Data

aus_map <- map_data("world", region = "Australia")

Now how much data do we have?

dim(aus_map)

So this may be low resolution…

table(aus_map$subregion)

Is that every region?

Getting Australian Data

aus_map %>%
  mutate(naRegion = is.na(subregion)) %>%
  group_by(naRegion) %>%
  count()

So we have more data points without a subregion than with

Here,the group column corresponds to the subregion

The mainland is group == 40

Getting Australian Data

table(aus_map$group) %>% sort()
aus_map %>%
  filter(group == 40) %>%
  tbl_df()

Any thoughts on what the group column does?

Plotting the data

Without Groups

ggplot(aus_map, aes(x = long, y = lat)) +
  geom_polygon() 

With Groups

ggplot(aus_map, aes(x = long, y = lat, group  = group)) +
  geom_polygon() 

Plotting the data

ggplot(aus_map, aes(x = long, y = lat, 
                    group  = group, fill = subregion)) +
  geom_polygon() +
  guides(fill = FALSE) + 
  theme_bw()

How can we zoom in?

Zooming In

aus_map %>%
  filter(subregion == "Tasmania") %>%
  ggplot(aes(x = long, y = lat, 
                    group  = group, fill = as.factor(group))) +
  geom_polygon() +
  guides(fill = FALSE) + 
  theme_bw()

It's low resolution…

Adding points

Arthur's Lake

This is at:

  • 146\(^{\circ}\) 54' 36" (Longitude)
  • 41\(^{\circ}\) 59' 24" (Latitude)
arthur <- data_frame(long = 146 + 54/60 + 36/3600,
                     lat = -41 - 59/60 - 24/3600, 
                     group = NA,
                     order = NA, 
                     region = NA, 
                     subregion = "Arthur's Lake")

Adding points

Arthur's Lake

aus_map %>%
  filter(subregion == "Tasmania") %>%
  ggplot(aes(x = long, y = lat, 
                    group  = group, fill = as.factor(group))) +
  geom_polygon() +
  guides(fill = FALSE) + 
  theme_bw() +
  geom_point(aes(x = long,y = lat), data = arthur) +
  geom_text(aes(x = long, y = lat, label = subregion), 
            data = arthur,
            nudge_y = 0.1) 

Adding "Roads"

Let's make a pretend road

road <- data_frame(long = c(146, 146.5, 146.8),
                   lat = c(-43, -42.5, -42.3),
                   group = NA,
                   order = NA, 
                   region = NA, 
                   subregion = NA)

Adding Roads

aus_map %>%
  filter(subregion == "Tasmania") %>%
  ggplot(aes(x = long, y = lat, 
                    group  = group, fill = as.factor(group))) +
  geom_polygon() +
  # coord_map("mercator") +
  guides(fill = FALSE) + 
  theme_bw() +
  geom_point(aes(x = long,y = lat), data = arthur) +
  geom_text(aes(x = long, y = lat, label = subregion), 
            data = arthur,
            nudge_y = 0.1) +
  geom_line(aes(x = long,y = lat), data = road)

Going Forward

  • The key is finding high resolution co-ordinates
  • Layering can be much more sophisticated
  • States can be defined as groups (then fill them)

Useful packages might be:
mapdata, maptools

-maptools can import Esri shapefiles.