## ----setup, include = FALSE---------------------------------------------------
knitr::opts_chunk$set(
  collapse = TRUE,
  message = FALSE,
  warning = FALSE,
  eval = FALSE,
  comment = "#>"
)

library(Rglottography)
stopifnot(
  requireNamespace("sf", quietly = TRUE),
  requireNamespace("dplyr", quietly = TRUE),
  requireNamespace("ggplot2", quietly = TRUE),
  requireNamespace("rnaturalearth", quietly = TRUE)
)

## ----load-glottography, eval=FALSE--------------------------------------------
# 
# glottography <- load_datasets("matsumae2021exploring")
# languages <- glottography$languages
# 
# arctic_proj <- paste0(
#   "+proj=aea +lon_0=159.609375 +lat_1=51.7903305 ",
#   "+lat_2=74.6587299 +lat_0=63.2245302 +datum=WGS84 ",
#   "+units=m +no_defs")
# 
# even_kory_chuk <- languages |>
#   dplyr::filter(glottocode %in% c("even1260", "kory1246", "chuk1273")) |>
#   sf::st_transform(arctic_proj)

## ----map-extent, eval=FALSE---------------------------------------------------
# clip_extent <- even_kory_chuk |>
#   sf::st_bbox() |>
#   sf::st_as_sfc () |>
#   sf::st_buffer(dist = 700000)  # buffer in metres

## ----natural-earth, eval=FALSE------------------------------------------------
# land <- rnaturalearth::ne_download(scale = "large", type = "land",
#                                    category = "physical") |>
#   sf::st_transform(arctic_proj) |>
#   sf::st_make_valid() |>
#   sf::st_intersection(clip_extent)
# 
# ocean <- rnaturalearth::ne_download(scale = "large", type = "ocean",
#                                    category = "physical") |>
#   sf::st_transform(arctic_proj) |>
#   sf::st_make_valid()|>
#   sf::st_intersection(clip_extent)
# 
# rivers <- rnaturalearth::ne_download(scale = "large",
#                                      type = "rivers_lake_centerlines",
#                                      category = "physical") |>
#   dplyr::filter(scalerank < 7) |>  # retain only relatively large rivers
#   sf::st_transform(arctic_proj) |>
#   sf::st_make_valid()|>
#   sf::st_intersection(clip_extent)
# 
# lakes <- rnaturalearth::ne_download(scale = "large",
#                                     type = "lakes",
#                                     category = "physical") |>
#   dplyr::filter(scalerank < 7) |>  # retain only relatively large lakes
#   sf::st_transform(arctic_proj) |>
#   sf::st_make_valid()|>
#   sf::st_intersection(clip_extent)
# 
# places <- rnaturalearth::ne_download(scale = "large",
#                                      type = "populated_places",
#                                      category = "cultural") |>
#   dplyr::filter(SCALERANK < 4) |>  # retain only relatively large places
#   sf::st_transform(arctic_proj) |>
#   sf::st_make_valid()|>
#   sf::st_intersection(clip_extent)

## ----base-map, eval=FALSE-----------------------------------------------------
# language_map <- ggplot2::ggplot() +
#   ggplot2::geom_sf(data = land, fill = "white", color = NA) +
#   ggplot2::geom_sf(data = ocean, fill = "#D0E1F2", color = NA) +
#   ggplot2::geom_sf(data = lakes, fill = "#7FB4D6", color = NA)

## ----graticules, eval=FALSE---------------------------------------------------
# graticules <- sf::st_graticule(lons = c(150, 180),
#                            lats = c(50, 80)) |>
#   sf::st_transform(arctic_proj) |>
#   sf::st_geometry() |>
#   sf::st_intersection(clip_extent)
# 
# language_map <- language_map +
#   ggplot2::geom_sf(data = graticules, color = "grey70", linetype = "dashed", linewidth = 0.3)

## ----languages, eval=FALSE----------------------------------------------------
# language_map <- language_map +
#   ggplot2::geom_sf(data = even_kory_chuk,
#                    ggplot2::aes(fill = name), color = NA, alpha = 0.5)

## ----populated-places, eval=FALSE---------------------------------------------
# language_map <- language_map +
#     ggplot2::geom_sf(data = places) +
#     ggplot2::geom_sf_text(
#       data = places,
#       ggplot2::aes(label = NAME),
#       size = 3,
#       nudge_y = 70000 #vertical offset in metres
#     )

## ----legend, eval=FALSE-------------------------------------------------------
# color <-c("#CC79A7", "#E69F00", "#009E73")
# names(color) <- even_kory_chuk$name
# 
# language_map <- language_map +
#     ggplot2::scale_fill_manual(
#       values = color,
#       labels = names(color),
#       guide = ggplot2::guide_legend(title = NULL)
#     )

## ----map extent, eval=FALSE---------------------------------------------------
# bbox <- sf::st_bbox(clip_extent)
# offset <- 200000
# 
# language_map <- language_map +
#   ggplot2::coord_sf(crs = arctic_proj,
#            xlim = c(bbox["xmin"] + offset, bbox["xmax"] - offset),
#            ylim = c(bbox["ymin"] + offset, bbox["ymax"] - offset),
#            expand = FALSE, datum = NA)

## ----theme, eval=FALSE--------------------------------------------------------
# language_map <- language_map +
#   ggplot2::theme_void() +
#   ggplot2::theme(
#     panel.border = ggplot2::element_rect(
#       color = "grey", fill = NA, linewidth = 0.5),
#     legend.position = "inside",
#     legend.position.inside = c(0.95, 0.05),
#     legend.justification = c(1, 0),
#     legend.background = ggplot2::element_rect(fill = "white", color = NA),
#     legend.box.background = ggplot2::element_rect(fill = "white", color = NA),
#     legend.margin = ggplot2::margin(10, 10, 10, 10)
#   )

## ----map-prerendered, echo = FALSE, eval=TRUE, out.width="100%", fig.align="center"----
knitr::include_graphics("../inst/vignettes/mapping_languages_map.png")

## ----collect-sources, eval = FALSE--------------------------------------------
# sources <- collect_sources(even_kory_chuk)

