Skip to content

Commit

Permalink
feat: implement distribution map function
Browse files Browse the repository at this point in the history
  • Loading branch information
ahasverus committed Feb 11, 2024
1 parent dac4942 commit ff6cfb2
Show file tree
Hide file tree
Showing 6 changed files with 122 additions and 6 deletions.
1 change: 1 addition & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ Depends:
R (>= 4.1.0)
Imports:
dplyr,
ggplot2,
jsonlite,
rlang,
sf,
Expand Down
6 changes: 6 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export(get_pump_data)
export(get_required_columns)
export(get_sediment_trap_data)
export(get_species_names)
export(map_distribution)
export(reshape_forcis)
export(select_columns)
export(select_taxonomy)
Expand All @@ -28,3 +29,8 @@ import(dplyr)
import(rlang)
import(tidyr)
import(vroom)
importFrom(ggplot2,element_text)
importFrom(ggplot2,geom_sf)
importFrom(ggplot2,ggplot)
importFrom(ggplot2,theme)
importFrom(ggplot2,theme_void)
1 change: 1 addition & 0 deletions R/forcis-package.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#' @import rlang
#' @import tidyr
#' @import vroom
#' @importFrom ggplot2 ggplot geom_sf theme theme_void element_text
# Imports: end ----

NULL
71 changes: 71 additions & 0 deletions R/map_distribution.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
#' Plot a distribution map of FORCIS data
#'
#' @description
#' This function can be used to map FORCIS data.
#'
#' @param data a `data.frame`, i.e. a FORCIS dataset or the output of a
#' `filter_*()` function.
#'
#' @param col a `character` of length 1. The color of data on the map.
#'
#' @param ... other graphical parameters passed on to `geom_sf()`.
#'
#' @return A `ggplot` object.
#'
#' @export
#'
#' @examples
#' ## ADD EXAMPLE ----

map_distribution <- function(data, col = "red", ...) {

## Check data object ----

check_if_not_df(data)

check_field_in_data(data, "site_lon_start_decimal")
check_field_in_data(data, "site_lat_start_decimal")


## Convert data into sf object -----

data <- data[!is.na(data$"site_lon_start_decimal"), ]
data <- data[!is.na(data$"site_lat_start_decimal"), ]

data_sf <- sf::st_as_sf(data,
coords = c("site_lon_start_decimal",
"site_lat_start_decimal"),
crs = sf::st_crs(4326))


## Project spatial objects into Robinson system ----

data_sf <- sf::st_transform(data_sf, sf::st_crs(crs_robinson()))


## Map ----

ggplot() +

### Basemap ----

geom_sf(data = ne_oceans, fill = "#cdeafc", col = "#cdeafc",
linewidth = 0.10) +
geom_sf(data = ne_graticules, col = "#bae2fb",
linewidth = 0.10) +
geom_sf(data = ne_countries, fill = "#a6a6a6", col = "#b1b1b1",
linewidth = 0.10) +
geom_sf(data = ne_bbox, fill = NA, col = "#a6a6a6",
linewidth = 0.75) +


### Data ---

geom_sf(data = data_sf, col = col, ...) +


### Theme ----

theme_void() +
theme(plot.title = element_text(hjust = 0.5))
}
24 changes: 18 additions & 6 deletions codemeta.json
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,18 @@
"sameAs": "https://CRAN.R-project.org/package=dplyr"
},
"3": {
"@type": "SoftwareApplication",
"identifier": "ggplot2",
"name": "ggplot2",
"provider": {
"@id": "https://cran.r-project.org",
"@type": "Organization",
"name": "Comprehensive R Archive Network (CRAN)",
"url": "https://cran.r-project.org"
},
"sameAs": "https://CRAN.R-project.org/package=ggplot2"
},
"4": {
"@type": "SoftwareApplication",
"identifier": "jsonlite",
"name": "jsonlite",
Expand All @@ -119,7 +131,7 @@
},
"sameAs": "https://CRAN.R-project.org/package=jsonlite"
},
"4": {
"5": {
"@type": "SoftwareApplication",
"identifier": "rlang",
"name": "rlang",
Expand All @@ -131,7 +143,7 @@
},
"sameAs": "https://CRAN.R-project.org/package=rlang"
},
"5": {
"6": {
"@type": "SoftwareApplication",
"identifier": "sf",
"name": "sf",
Expand All @@ -143,7 +155,7 @@
},
"sameAs": "https://CRAN.R-project.org/package=sf"
},
"6": {
"7": {
"@type": "SoftwareApplication",
"identifier": "tidyr",
"name": "tidyr",
Expand All @@ -155,12 +167,12 @@
},
"sameAs": "https://CRAN.R-project.org/package=tidyr"
},
"7": {
"8": {
"@type": "SoftwareApplication",
"identifier": "utils",
"name": "utils"
},
"8": {
"9": {
"@type": "SoftwareApplication",
"identifier": "vroom",
"name": "vroom",
Expand All @@ -174,7 +186,7 @@
},
"SystemRequirements": null
},
"fileSize": "1057.776KB",
"fileSize": "1059.228KB",
"citation": [
{
"@type": "SoftwareSourceCode",
Expand Down
25 changes: 25 additions & 0 deletions man/map_distribution.Rd

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

0 comments on commit ff6cfb2

Please sign in to comment.