22 July 2016
install.packages("maps")
library(maps) library(ggplot2) library(dplyr)
ggplot2 has function map_data()
world_map <- map_data("world")
Let's check it out
head(world_map)
How big is the world map?
dim(world_map)
How much information is there for each region?
table(world_map$region)
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?
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
table(aus_map$group) %>% sort()
aus_map %>% filter(group == 40) %>% tbl_df()
Any thoughts on what the group column does?
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()
ggplot(aus_map, aes(x = long, y = lat,
group = group, fill = subregion)) +
geom_polygon() +
guides(fill = FALSE) +
theme_bw()
How can we zoom 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…
This is at:
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")
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)
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)
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)
Useful packages might be:mapdata, maptools
-maptools can import Esri shapefiles.