Skip to content

Commit

Permalink
Country polygons now downloaded to extdata directory
Browse files Browse the repository at this point in the history
The world country polygons are downloaded from Natural Earth and placed in the extdata directory. The mapmixture() function checks the data is present before execution.
  • Loading branch information
Tom-Jenkins committed Apr 15, 2024
1 parent d41de10 commit c8709b3
Show file tree
Hide file tree
Showing 9 changed files with 75 additions and 56 deletions.
2 changes: 1 addition & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
# mapmixture 1.1.0
# mapmixture 1.1.1

* Initial CRAN submission.
71 changes: 45 additions & 26 deletions R/func_mapmixture.R
Original file line number Diff line number Diff line change
Expand Up @@ -59,31 +59,31 @@
#' mapmixture(admixture1, coordinates)
#'
#' # Plot using the ETRS89-extended / LAEA Europe coordinate reference system
#' mapmixture(admixture1, coordinates, crs = 3035)
#' # mapmixture(admixture1, coordinates, crs = 3035)
#'
#' # Plot using custom parameters
#' mapmixture(admixture1, coordinates,
#' cluster_cols = c("blue","green"),
#' cluster_names = c("Group 1","Group 2"),
#' crs = "+proj=merc +a=6378137 +b=6378137 +lat_ts=0 +lon_0=0 +x_0=0 +y_0=0 +units=m",
#' boundary = c(xmin=-15, xmax=16, ymin=40, ymax=62),
#' pie_size = 1.5,
#' pie_border = 0.2,
#' pie_opacity = 1,
#' land_colour = "#d9d9d9",
#' sea_colour = "#deebf7",
#' expand = FALSE,
#' arrow = TRUE,
#' arrow_size = 1,
#' arrow_position = "tl",
#' scalebar = TRUE,
#' scalebar_size = 1,
#' scalebar_position = "tl",
#' plot_title = "Mapmixture Figure",
#' plot_title_size = 15,
#' axis_title_size = 12,
#' axis_text_size = 10
#' )
#' # mapmixture(admixture1, coordinates,
#' # cluster_cols = c("blue","green"),
#' # cluster_names = c("Group 1","Group 2"),
#' # crs = "+proj=merc +a=6378137 +b=6378137 +lat_ts=0 +lon_0=0 +x_0=0 +y_0=0 +units=m",
#' # boundary = c(xmin=-15, xmax=16, ymin=40, ymax=62),
#' # pie_size = 1.5,
#' # pie_border = 0.2,
#' # pie_opacity = 1,
#' # land_colour = "#d9d9d9",
#' # sea_colour = "#deebf7",
#' # expand = FALSE,
#' # arrow = TRUE,
#' # arrow_size = 1,
#' # arrow_position = "tl",
#' # scalebar = TRUE,
#' # scalebar_size = 1,
#' # scalebar_position = "tl",
#' # plot_title = "Mapmixture Figure",
#' # plot_title_size = 15,
#' # axis_title_size = 12,
#' # axis_text_size = 10
#' # )
mapmixture <- function(
# Data input
admixture_df, coords_df,
Expand All @@ -98,7 +98,25 @@ mapmixture <- function(
plot_title = "", plot_title_size = 12,
axis_title_size = 10, axis_text_size = 8) {

# Standarise input data ----

# Download countries10.rda file to mapmixture extdata directory if not present
# https://github.com/ropensci/rnaturalearthhires
tryCatch({
filepath <- system.file("extdata", "countries10.rda", package = "mapmixture")
if (filepath == "") {
message("Downloading countries10 data from Natural Earth...")
url <- "https://github.com/ropensci/rnaturalearthhires/raw/master/data/countries10.rda"
desfile <- paste0(system.file("extdata", package = "mapmixture"), "/countries10.rda")
utils::download.file(url, destfile = desfile, mode = "wb")
message("Data downloaded to 'mapmixture/inst/extdata' folder.")
}
}, error = function(err) {
# Print error message if an error occurs from downloading
stop("Error downloading data from Natural Earth. Please check internet connection.")
})


# Standardise input data ----
tryCatch({
admixture_df <- standardise_data(admixture_df, type = "admixture")
coords_df <- standardise_data(coords_df, type = "coordinates")
Expand All @@ -119,10 +137,11 @@ mapmixture <- function(
admix_coords <- merge_coords_data(coords_df, admixture_df)

# Read in world boundaries
world_boundaries <- sf::st_read(system.file("extdata", "world.gpkg", package = "mapmixture"), quiet = TRUE)
# world_boundaries <- sf::st_read(system.file("extdata", "world.gpkg", package = "mapmixture"), quiet = TRUE)
load(system.file("extdata", "countries10.rda", package = "mapmixture"))

# Transform world boundaries to CRS
world_boundaries <- sf::st_transform(world_boundaries, crs = crs)
world_boundaries <- sf::st_transform(get("countries10"), crs = crs)

# Transform coordinates in admix_coords object to CRS
admix_coords <- transform_df_coords(admix_coords, crs = crs)
Expand Down
6 changes: 3 additions & 3 deletions README.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -359,9 +359,9 @@ coordinates <- read.csv(file)
crs <- 3035
boundary <- c(xmin=-11, xmax=13, ymin=50, ymax=60) |> transform_bbox(bbox = _, crs)
# Read in world coastlines and transform to CRS
file <- system.file("extdata", "world.gpkg", package = "mapmixture")
world <- st_read(file, quiet = TRUE) |> st_transform(x = _, crs = crs)
# Read in world countries from Natural Earth and transform to CRS
load(system.file("extdata", "countries10.rda", package = "mapmixture"))
world <- st_transform(get("countries10"), crs = crs)
# Read in Marine Conservation Zones shapefile
# Extract polygons for Western Channel, Offshore Brighton and Swallow Sand
Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -380,9 +380,9 @@ coordinates <- read.csv(file)
crs <- 3035
boundary <- c(xmin=-11, xmax=13, ymin=50, ymax=60) |> transform_bbox(bbox = _, crs)

# Read in world coastlines and transform to CRS
file <- system.file("extdata", "world.gpkg", package = "mapmixture")
world <- st_read(file, quiet = TRUE) |> st_transform(x = _, crs = crs)
# Read in world countries from Natural Earth and transform to CRS
load(system.file("extdata", "countries10.rda", package = "mapmixture"))
world <- st_transform(get("countries10"), crs = crs)

# Read in Marine Conservation Zones shapefile
# Extract polygons for Western Channel, Offshore Brighton and Swallow Sand
Expand Down
Binary file removed inst/extdata/world.gpkg
Binary file not shown.
Binary file modified man/figures/README-unnamed-chunk-11-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified man/figures/README-unnamed-chunk-3-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified man/figures/README-unnamed-chunk-9-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
46 changes: 23 additions & 23 deletions man/mapmixture.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit c8709b3

Please sign in to comment.