Geospatial

1 minute read

Published:

This lesson is from datacamp Visualizing Geospatial data in R and https://eriqande.github.io/rep-res-eeb-2017/map-making-in-R.html

library(maps)
library(ggplot2)
library(tidyverse)
library(dplyr)

# US Plot
# US Plot
map_data("usa") %>% # library(ggplot2)
  ggplot() + 
  geom_polygon(aes(x=long, y=lat, group=group)) + 
  coord_quickmap()

# US Plot with color and no fill
map_data("usa") %>% # library(ggplot2)
  ggplot() + 
  geom_polygon(aes(x=long, y=lat, group=group),
               fill=NA, color="red") + 
  coord_quickmap()

# US Plot with color and fill
map_data("usa") %>% # library(ggplot2)
  ggplot() + 
  geom_polygon(aes(x=long, y=lat, group=group),
               fill="violet", color="blue") + 
  coord_quickmap()


labs <- tibble( # type of data frame
  long = c(-122.064873, -122.306417),
  lat = c(36.951968, 47.644855),
  names = c("A", "B"))  

usa <- map_data("usa") 
ggplot() + 
  geom_polygon(data = usa, 
               aes(x = long, y = lat, group = group), 
               fill = "violet", color = "blue") + 
  coord_quickmap() +
  geom_point(data = labs, 
             aes(x = long, y = lat), 
             shape = 21, 
             color = "black", 
             fill = "yellow", 
             size = 5) +
  geom_text(data = labs, 
            aes(x = long, y = lat, label = names), 
            hjust = 0, 
            nudge_x = 1)


# States Map
states <- map_data("state")
dim(states)

map_data("state") %>%
  ggplot() + 
  geom_polygon(aes(x = long, y = lat, 
                   fill = region, 
                   group = group
                   ), 
               color = "white") + 
  coord_quickmap() +
  guides(fill = FALSE)  # no color legend

# BN Map
map_data("world") %>%
  filter(region == "Brunei") %>%
  ggplot() + 
  geom_polygon(aes(x = long, 
                   y = lat, 
                   group = group)
               ) + 
  coord_quickmap()


map_data("world") %>%
  filter(region == "Brunei") %>%
  ggplot() + 
  geom_polygon(aes(x = long, 
                   y = lat, 
                   group = group),
               fill = "violet", color = "blue"
               ) + 
  coord_quickmap()

# Mapping Points
labs <- tibble(
  long = c(114.5, 114.8),
  lat = c(4.5, 4.85),
  names = c("A", "B"))

map_data("world") %>%
  filter(region == "Brunei") %>%
  ggplot() + 
  geom_polygon(aes(x = long, 
                   y = lat, 
                   group = group),
               fill = "violet", color = "blue"
  ) + 
  coord_quickmap() +
  geom_point(data=labs, 
             aes(x=long, y=lat), 
             shape = 21, 
             color = "black", 
             fill = "yellow", 
             size = 10) +
  geom_text(data=labs, 
            aes(x=long, y=lat, label=names))


map_data("world")


map.cities(x = world.cities, 
           country = "", label = NULL, 
           minpop = 0,
           maxpop = Inf, 
           capitals = 0, cex = par("cex"), 
           projection = FALSE,
           parameters = NULL, 
           orientation = NULL, 
           pch = 1)

map("world", "Brunei") + 
  map.axes()
points(114.6,4.4, pch=21, col="red", lwd=2, bg="blue")

Tags: