├── .Rbuildignore ├── .github ├── CONTRIBUTING.md ├── issue_template.md ├── pull_request_template.md └── workflows │ └── R-check.yaml ├── .gitignore ├── DESCRIPTION ├── LICENSE ├── LICENSE.md ├── Makefile ├── NAMESPACE ├── NEWS.md ├── R ├── colors.R ├── hull.R ├── latlong_guesser.R ├── map_ggmap.R ├── map_ggplot.R ├── map_gist.R ├── map_leaflet.R ├── map_plot.R ├── mapr-package.R ├── occ2sp.R ├── rename.R ├── util_github.r └── zzz.r ├── README-not.md ├── README.Rmd ├── README.md ├── codemeta.json ├── cran-comments.md ├── data ├── gbif_eg1.rda └── occdat_eg1.rda ├── inst ├── ignore │ ├── as.occkey.R │ ├── clean_spocc_workingon.R │ ├── datasets.R │ ├── gbifxmlfxn.R │ ├── latlongstuff.R │ ├── leaflet.R │ ├── map_shiny.R │ └── occdat_vignette.Rmd ├── shiny │ ├── egsmodal.html │ ├── global.r │ ├── server.r │ └── ui.r ├── shinydumb │ ├── server.r │ └── ui.r └── shinysingle │ ├── app.R │ └── dat.csv ├── man-roxygen └── args.R ├── man ├── figures │ ├── gist.png │ ├── leaflet.png │ ├── unnamed-chunk-5-1.png │ ├── unnamed-chunk-6-1.png │ └── unnamed-chunk-7-1.png ├── gbif_eg1.Rd ├── hull.Rd ├── map_ggmap-defunct.Rd ├── map_ggplot.Rd ├── map_gist.Rd ├── map_leaflet.Rd ├── map_plot.Rd ├── mapr-package.Rd ├── occ2sp.Rd ├── occdat_eg1.Rd └── style_geojson.Rd ├── tests ├── test-that.R └── testthat │ ├── spocc_df_1.rda │ ├── test-map_ggplot.R │ ├── test-map_gist.R │ ├── test-map_leaflet.R │ └── test-utils.R └── vignettes ├── mapr.Rmd └── mapr.Rmd.og /.Rbuildignore: -------------------------------------------------------------------------------- 1 | .travis.yml 2 | kr_local.R 3 | Makefile 4 | man-roxygen 5 | man-roxygen/^.*\.Rproj$ 6 | mapr.Rproj 7 | ^\.Rproj\.user$ 8 | vignettes/margins.sty 9 | inst/shinydumb 10 | inst/shiny 11 | inst/shinysingle 12 | inst/vign 13 | inst/ignore 14 | CONTRIBUTING.md 15 | cran-comments.md 16 | appveyor.yml 17 | README.Rmd 18 | ^\.httr-oauth$ 19 | gistmap* 20 | ^CODE_OF_CONDUCT\.md$ 21 | ^cran-comments\.md$ 22 | ^codemeta\.json$ 23 | \.github 24 | ^LICENSE\.md$ 25 | vignettes/mapr.Rmd 26 | -------------------------------------------------------------------------------- /.github/CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | # CONTRIBUTING # 2 | 3 | ### Bugs? 4 | 5 | * Submit an issue on the [Issues page](https://github.com/ropensci/mapr/issues) 6 | 7 | ### Issues and Pull Requests 8 | 9 | If you are considering a pull request, you may want to open an issue first to discuss with the maintainer(s). 10 | 11 | ### Code contributions 12 | 13 | * Fork this repo to your GitHub account 14 | * Clone your version on your account down to your machine from your account, e.g,. `git clone https://github.com//mapr.git` 15 | * Make sure to track progress upstream (i.e., on our version of `mapr` at `ropensci/mapr`) by doing `git remote add upstream https://github.com/ropensci/mapr.git`. Before making changes make sure to pull changes in from upstream by doing either `git fetch upstream` then merge later or `git pull upstream` to fetch and merge in one step 16 | * Make your changes (bonus points for making changes on a new feature branch - see for how to contribute by branching, making changes, then submitting a pull request) 17 | * Push up to your account 18 | * Submit a pull request to home base (likely master branch, but check to make sure) at `ropensci/mapr` 19 | 20 | ### Vignette changes 21 | 22 | If you want to contribute to the vignette, or add a new one, those are kept in the `inst/vign/` directory. The main vignette is in `inst/vign/mapr_vignette.Rmd`. 23 | 24 | ### Discussion forum 25 | 26 | Check out our [discussion forum](https://discuss.ropensci.org) if you think your issue requires a longer form discussion. 27 | -------------------------------------------------------------------------------- /.github/issue_template.md: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 |
Session Info 6 | 7 | ```r 8 | 9 | ``` 10 | 11 |
12 | -------------------------------------------------------------------------------- /.github/pull_request_template.md: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | ## Description 8 | 9 | 10 | ## Related Issue 11 | 14 | 15 | ## Example 16 | 18 | 19 | 21 | 22 | -------------------------------------------------------------------------------- /.github/workflows/R-check.yaml: -------------------------------------------------------------------------------- 1 | on: [push, pull_request] 2 | 3 | name: R-check 4 | 5 | jobs: 6 | R-check: 7 | runs-on: ${{ matrix.config.os }} 8 | 9 | name: ${{ matrix.config.os }} (${{ matrix.config.r }}) 10 | 11 | strategy: 12 | fail-fast: false 13 | matrix: 14 | config: 15 | - { os: windows-latest, r: 'release'} 16 | - { os: windows-latest, r: 'devel'} 17 | - { os: macOS-latest, r: 'release'} 18 | # - { os: macOS-latest, r: 'devel'} 19 | - { os: ubuntu-16.04, r: 'release', rspm: "https://packagemanager.rstudio.com/cran/__linux__/xenial/latest"} 20 | 21 | env: 22 | R_REMOTES_NO_ERRORS_FROM_WARNINGS: true 23 | CRAN: ${{ matrix.config.rspm }} 24 | GITHUB_PAT: ${{ secrets.MY_GITHUB_PAT }} 25 | 26 | steps: 27 | - uses: actions/checkout@v2 28 | 29 | - uses: r-lib/actions/setup-r@master 30 | with: 31 | r-version: ${{ matrix.config.r }} 32 | 33 | - uses: r-lib/actions/setup-pandoc@master 34 | 35 | - name: Query dependencies 36 | run: | 37 | install.packages('remotes') 38 | saveRDS(remotes::dev_package_deps(dependencies = TRUE), ".github/depends.Rds", version = 2) 39 | writeLines(sprintf("R-%i.%i", getRversion()$major, getRversion()$minor), ".github/R-version") 40 | shell: Rscript {0} 41 | 42 | - name: Cache R packages 43 | if: runner.os != 'Windows' 44 | uses: actions/cache@v1 45 | with: 46 | path: ${{ env.R_LIBS_USER }} 47 | key: ${{ runner.os }}-${{ hashFiles('.github/R-version') }}-1-${{ hashFiles('.github/depends.Rds') }} 48 | restore-keys: ${{ runner.os }}-${{ hashFiles('.github/R-version') }}-1- 49 | 50 | - name: install macOS system dependencies 51 | if: runner.os == 'macOS' 52 | continue-on-error: true 53 | run: | 54 | brew install pkg-config gdal geos openssl udunits proj v8 netcdf 55 | 56 | - name: Install system dependencies 57 | if: runner.os == 'Linux' 58 | env: 59 | RHUB_PLATFORM: linux-x86_64-ubuntu-gcc 60 | run: | 61 | Rscript -e "remotes::install_github('r-hub/sysreqs')" 62 | sysreqs=$(Rscript -e "cat(sysreqs::sysreq_commands('DESCRIPTION'))") 63 | sudo add-apt-repository -y ppa:ubuntugis/ppa 64 | sudo -s eval "$sysreqs" 65 | sudo apt update 66 | sudo apt install \ 67 | libudunits2-dev \ 68 | libv8-dev \ 69 | libproj-dev \ 70 | libgeos-dev \ 71 | libgdal-dev \ 72 | netcdf-bin 73 | 74 | - name: Install dependencies 75 | run: | 76 | remotes::install_deps(dependencies = TRUE) 77 | remotes::install_cran(c("rcmdcheck", "scrubr")) 78 | shell: Rscript {0} 79 | 80 | - name: Session info 81 | run: | 82 | options(width = 100) 83 | pkgs <- installed.packages()[, "Package"] 84 | sessioninfo::session_info(pkgs, include_base = TRUE) 85 | shell: Rscript {0} 86 | 87 | - name: Check 88 | env: 89 | _R_CHECK_CRAN_INCOMING_: false 90 | run: rcmdcheck::rcmdcheck(args = c("--no-manual", "--as-cran"), error_on = "error", check_dir = "check") 91 | shell: Rscript {0} 92 | 93 | - name: Show testthat output 94 | if: always() 95 | run: find check -name 'testthat.Rout*' -exec cat '{}' \; || true 96 | shell: bash 97 | 98 | - name: Upload check results 99 | if: failure() 100 | uses: actions/upload-artifact@main 101 | with: 102 | name: ${{ runner.os }}-r${{ matrix.config.r }}-results 103 | path: check 104 | 105 | - name: Test coverage 106 | if: matrix.config.os == 'macOS-latest' && matrix.config.r == 'release' 107 | run: | 108 | Rscript -e 'install.packages("covr")' -e 'covr::codecov(token = "${{secrets.CODECOV_TOKEN}}")' 109 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | /.Rapp.history 2 | .Rproj.user 3 | kr_local.R 4 | .Rapp.history 5 | spocc.Rproj 6 | .Rhistory 7 | .httr-oauth 8 | notes.txt 9 | -------------------------------------------------------------------------------- /DESCRIPTION: -------------------------------------------------------------------------------- 1 | Package: mapr 2 | Title: Visualize Species Occurrence Data 3 | Description: Utilities for visualizing species occurrence data. Includes 4 | functions to visualize occurrence data from 'spocc', 'rgbif', 5 | and other packages. Mapping options included for base R plots, 'ggplot2', 6 | 'leaflet' and 'GitHub' 'gists'. 7 | Version: 0.5.2 8 | License: MIT + file LICENSE 9 | Authors@R: person("Scott", "Chamberlain", role = c("aut", "cre"), 10 | email = "myrmecocystus@gmail.com", 11 | comment = c(ORCID = "0000-0003-1444-9135")) 12 | URL: https://docs.ropensci.org/mapr/, https://github.com/ropensci/mapr 13 | BugReports: https://github.com/ropensci/mapr/issues 14 | LazyData: true 15 | LazyLoad: true 16 | Roxygen: list(markdown = TRUE) 17 | Encoding: UTF-8 18 | Language: en-US 19 | Imports: 20 | ggplot2, 21 | leaflet, 22 | spocc (>= 0.6.0), 23 | sp, 24 | maps, 25 | RColorBrewer, 26 | jsonlite, 27 | gistr, 28 | data.table 29 | Suggests: 30 | testthat, 31 | knitr, 32 | taxize, 33 | maptools, 34 | rgbif, 35 | scrubr 36 | RoxygenNote: 7.1.1 37 | X-schema.org-applicationCategory: Geospatial 38 | X-schema.org-keywords: maps, mapping, ggplot, leaflet, interactive, biodiversity, occurrences 39 | X-schema.org-isPartOf: https://ropensci.org/ 40 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | YEAR: 2020 2 | COPYRIGHT HOLDER: Scott Chamberlain 3 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | # MIT License 2 | 3 | Copyright (c) 2020 Scott Chamberlain 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | PACKAGE := $(shell grep '^Package:' DESCRIPTION | sed -E 's/^Package:[[:space:]]+//') 2 | RSCRIPT = Rscript --no-init-file 3 | 4 | vign: 5 | cd vignettes;\ 6 | ${RSCRIPT} -e "Sys.setenv(NOT_CRAN='true'); knitr::knit('mapr.Rmd.og', output = 'mapr.Rmd')";\ 7 | cd .. 8 | 9 | test: 10 | ${RSCRIPT} -e 'library(methods); devtools::test()' 11 | 12 | doc: 13 | ${RSCRIPT} -e "devtools::document()" 14 | 15 | 16 | egs: 17 | ${RSCRIPT} -e "devtools::run_examples(run = TRUE)" 18 | 19 | install: doc build 20 | R CMD INSTALL . && rm *.tar.gz 21 | 22 | build: 23 | R CMD build . 24 | 25 | check: build 26 | _R_CHECK_CRAN_INCOMING_=FALSE R CMD check --as-cran --no-manual `ls -1tr ${PACKAGE}*gz | tail -n1` 27 | @rm -f `ls -1tr ${PACKAGE}*gz | tail -n1` 28 | @rm -rf ${PACKAGE}.Rcheck 29 | 30 | 31 | check_windows: 32 | ${RSCRIPT} -e "devtools::check_win_devel(); devtools::check_win_release()" 33 | 34 | readme: README.Rmd 35 | ${RSCRIPT} -e "knitr::knit('README.Rmd')" 36 | -------------------------------------------------------------------------------- /NAMESPACE: -------------------------------------------------------------------------------- 1 | # Generated by roxygen2: do not edit by hand 2 | 3 | S3method(hull,gg) 4 | S3method(hull,leaflet) 5 | S3method(map_ggplot,SpatialPoints) 6 | S3method(map_ggplot,SpatialPointsDataFrame) 7 | S3method(map_ggplot,data.frame) 8 | S3method(map_ggplot,default) 9 | S3method(map_ggplot,gbif) 10 | S3method(map_ggplot,gbif_data) 11 | S3method(map_ggplot,occdat) 12 | S3method(map_ggplot,occdatind) 13 | S3method(map_gist,SpatialPoints) 14 | S3method(map_gist,SpatialPointsDataFrame) 15 | S3method(map_gist,data.frame) 16 | S3method(map_gist,default) 17 | S3method(map_gist,gbif) 18 | S3method(map_gist,gbif_data) 19 | S3method(map_gist,occdat) 20 | S3method(map_gist,occdatind) 21 | S3method(map_leaflet,SpatialPoints) 22 | S3method(map_leaflet,SpatialPointsDataFrame) 23 | S3method(map_leaflet,data.frame) 24 | S3method(map_leaflet,default) 25 | S3method(map_leaflet,gbif) 26 | S3method(map_leaflet,gbif_data) 27 | S3method(map_leaflet,occdat) 28 | S3method(map_leaflet,occdatind) 29 | S3method(map_plot,SpatialPoints) 30 | S3method(map_plot,SpatialPointsDataFrame) 31 | S3method(map_plot,data.frame) 32 | S3method(map_plot,default) 33 | S3method(map_plot,gbif) 34 | S3method(map_plot,gbif_data) 35 | S3method(map_plot,occdat) 36 | S3method(map_plot,occdatind) 37 | export(hull) 38 | export(map_ggmap) 39 | export(map_ggplot) 40 | export(map_gist) 41 | export(map_leaflet) 42 | export(map_plot) 43 | export(occ2sp) 44 | export(style_geojson) 45 | import(leaflet) 46 | importFrom(ggplot2,aes) 47 | importFrom(ggplot2,element_blank) 48 | importFrom(ggplot2,geom_point) 49 | importFrom(ggplot2,geom_polygon) 50 | importFrom(ggplot2,ggplot) 51 | importFrom(ggplot2,ggtitle) 52 | importFrom(ggplot2,labs) 53 | importFrom(ggplot2,map_data) 54 | importFrom(ggplot2,scale_color_brewer) 55 | importFrom(ggplot2,scale_color_manual) 56 | importFrom(ggplot2,theme) 57 | importFrom(maps,map) 58 | importFrom(sp,SpatialPoints) 59 | importFrom(sp,SpatialPointsDataFrame) 60 | importFrom(sp,plot) 61 | -------------------------------------------------------------------------------- /NEWS.md: -------------------------------------------------------------------------------- 1 | mapr 0.5.2 2 | ========== 3 | 4 | ### MINOR IMPROVEMENTS 5 | 6 | * fix vignette issue (#40) 7 | 8 | 9 | mapr 0.5.0 10 | ========== 11 | 12 | ### DEFUNCT 13 | 14 | * `map_ggmap()` is defunct. authentication has changed, more trouble than its worth (#34) 15 | 16 | ### NEW FEATURES 17 | 18 | * all mapping functions gain the `name` parameter to specify the column that holds the taxon name - if not given, we look for a column "name" (#32) 19 | 20 | ### MINOR IMPROVEMENTS 21 | 22 | * fix vignette missing title (#37) 23 | 24 | ### BUG FIXES 25 | 26 | * fix non-ascii strings in the two package datasets - and script added to make those datasets reproducible, including fixing non-ascii strings (#39) 27 | * remove linked references to pkgs in docs that are not imported/suggested (#38) 28 | * `map_plot()` speed up: using `maps::map()` instead of `rworldmap::getMap()`, faster (#35) 29 | * improve internal handling of `name` parameter users can pass down through mapping functions (#36) 30 | * `rgbif` added to Suggests - was used in examples but wasn't in Suggests - used conditionally in examples now 31 | 32 | 33 | mapr 0.4.0 34 | ========== 35 | 36 | ### MINOR IMPROVEMENTS 37 | 38 | * All `map_*()` functions now support `gbif_data` class from the `rgbif` package, which is the output of `rgbif::occ_data()` (#29) 39 | * Added `.github` files for contributing, issue and PR templates (#30) 40 | 41 | 42 | mapr 0.3.4 43 | ========== 44 | 45 | ### MINOR IMPROVEMENTS 46 | 47 | * Now using markdown for docs (#27) 48 | * Replaced `httr` with `crul` as http client (#26) 49 | 50 | ### Problem with ggmap 51 | 52 | * Note that there is a problem with `map_ggmap` due to a bug in 53 | `ggmap`. It is fixed in the `ggmap` dev version, so should be fixed 54 | in the CRAN version soon, hopefully. 55 | 56 | 57 | mapr 0.3.0 58 | ========== 59 | 60 | ### NEW FEATURES 61 | 62 | * Now in all functions, when there's more than 1 taxon, we'll do a separate 63 | color for each taxon and draw a legend if applicable (#21) (#22) 64 | * Added support for adding convex hulls to some of the plot types (#23) 65 | thanks to @rossmounce for the feature request 66 | * `map_leaflet()` now adds metadata as a popup to each marker (#18) (#25) 67 | 68 | 69 | mapr 0.2.0 70 | ========== 71 | 72 | ### NEW FEATURES 73 | 74 | * Released to CRAN. 75 | -------------------------------------------------------------------------------- /R/colors.R: -------------------------------------------------------------------------------- 1 | warning_color_vec <- 2 | "length of taxa names not equal to color vector, generating colors for you" 3 | 4 | check_colors <- function(x, color) { 5 | if (!is.null(color)) { 6 | if (length(unique(x$name)) != length(color)) { 7 | warning(warning_color_vec, call. = FALSE) 8 | ref <- data.frame( 9 | name = unique(x$name), 10 | color = RColorBrewer::brewer.pal(length(unique(x$name)), 11 | name = "Set1"), 12 | stringsAsFactors = FALSE) 13 | } else { 14 | ref <- data.frame(name = unique(x$name), color = color, 15 | stringsAsFactors = FALSE) 16 | } 17 | merge(x, ref, by = "name") 18 | } else { 19 | # x$color <- "#F7766D" 20 | # x 21 | ref <- data.frame( 22 | name = unique(x$name), 23 | color = { 24 | if (length(unique(x$name)) >= 3) { 25 | if (length(unique(x$name)) > 9) { 26 | message( 27 | "no. taxa > 9, using single color - consider passing in colors") 28 | "#F7766D" 29 | } else { 30 | RColorBrewer::brewer.pal(length(unique(x$name)), name = "Set1") 31 | } 32 | } else { 33 | sample( 34 | suppressWarnings( 35 | RColorBrewer::brewer.pal(length(unique(x$name)), name = "Set1") 36 | ), 37 | length(unique(x$name)) 38 | ) 39 | } 40 | }, 41 | stringsAsFactors = FALSE) 42 | merge(x, ref, by = "name") 43 | } 44 | } 45 | 46 | pick_colors <- function(x, color) { 47 | if (!is.null(color)) { 48 | if (length(unique(x$name)) != length(color)) { 49 | warning(warning_color_vec, call. = FALSE) 50 | ggplot2::scale_color_brewer(type = "qual", palette = 6) 51 | } else { 52 | ggplot2::scale_color_manual(values = color) 53 | } 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /R/hull.R: -------------------------------------------------------------------------------- 1 | #' Add a convex hull to a map 2 | #' 3 | #' @export 4 | #' @param x input 5 | #' @param ... ignored 6 | #' @details Can be used with [map_leaflet()], [map_plot()], 7 | #' and [map_ggplot()]. Other methods in this package may be supported 8 | #' in the future. 9 | #' 10 | #' @return Adds a convex hull to the plot. See [grDevices::chull()] 11 | #' for info. 12 | #' 13 | #' @examples 14 | #' # map spocc output, here using a built in object 15 | #' data(occdat_eg1) 16 | #' map_plot(occdat_eg1, hull = TRUE) 17 | #' 18 | #' # map rgbif output, here using a built in object 19 | #' hull(map_ggplot(occdat_eg1)) 20 | #' 21 | #' @examples \dontrun{ 22 | #' # leaflet 23 | #' library("spocc") 24 | #' spp <- c('Danaus plexippus', 'Accipiter striatus', 'Pinus contorta') 25 | #' dat <- occ(spp, from = 'gbif', limit = 30, has_coords = TRUE) 26 | #' hull(map_leaflet(dat)) 27 | #' 28 | #' # ggplot 29 | #' if (requireNamespace("rgbif")) { 30 | #' library("rgbif") 31 | #' res <- occ_search(scientificName = "Puma concolor", limit = 100) 32 | #' hull(map_ggplot(res)) 33 | #' } 34 | #' 35 | #' # base plots 36 | #' library("spocc") 37 | #' out <- occ(query='Accipiter striatus', from='gbif', limit=25, 38 | #' has_coords=TRUE) 39 | #' map_plot(out, hull = TRUE) 40 | #' } 41 | hull <- function(x, ...) { 42 | UseMethod("hull") 43 | } 44 | 45 | #' @export 46 | hull.leaflet <- function(x, ...) { 47 | dat <- Filter(function(z) z$method == "addMarkers" || 48 | z$method == "addCircleMarkers", x$x$calls)[[1]] 49 | dat <- data.frame(long = dat$args[[2]], lat = dat$args[[1]], 50 | stringsAsFactors = FALSE) 51 | outline <- dat[grDevices::chull(dat$long, dat$lat), ] 52 | x %>% 53 | addPolygons(data = outline, lng = ~long, lat = ~lat, 54 | fill = FALSE, weight = 2, color = "#FFFFCC", group = "Outline") 55 | } 56 | 57 | #' @export 58 | hull.gg <- function(x, ...) { 59 | longitude <- latitude <- NA 60 | outline <- x$data[grDevices::chull(x$data$longitude, x$data$latitude), ] 61 | x + geom_polygon(data = outline, aes(x = longitude, y = latitude), 62 | fill = NA, size = 1, colour = "black") 63 | } 64 | -------------------------------------------------------------------------------- /R/latlong_guesser.R: -------------------------------------------------------------------------------- 1 | ###### code adapted from the leaflet package - 2 | ###### source at github.com/rstudio/leaflet 3 | guess_latlon <- function(x, lat = NULL, lon = NULL) { 4 | nms <- names(x) 5 | 6 | if (is.null(lat)) { 7 | lats <- nms[grep(sprintf("^(%s)$", paste0(lat_options, collapse = "|")), 8 | nms, ignore.case = TRUE)] 9 | 10 | if (length(lats) == 1) { 11 | if (length(nms) > 2) { 12 | message("Assuming '", lats, "' is latitude") 13 | } 14 | names(x)[names(x) %in% lats] <- lat_var <- "latitude" 15 | } else { 16 | stop( 17 | "Couldn't infer latitude column, please specify with the 'lat' parameter", 18 | call. = FALSE) 19 | } 20 | } else { 21 | if (!any(names(x) %in% lat)) stop("'", lat, "' not found in your data", 22 | call. = FALSE) 23 | names(x)[names(x) %in% lat] <- lat_var <- "latitude" 24 | } 25 | 26 | if (is.null(lon)) { 27 | lngs <- nms[grep(sprintf("^(%s)$", paste0(lon_options, collapse = "|")), 28 | nms, ignore.case = TRUE)] 29 | 30 | if (length(lngs) == 1) { 31 | if (length(nms) > 2) { 32 | message("Assuming '", lngs, "' is longitude") 33 | } 34 | names(x)[names(x) %in% lngs] <- lon_var <- "longitude" 35 | } else { 36 | stop( 37 | "Couldn't infer longitude column, please specify with 'lon' parameter", 38 | call. = FALSE) 39 | } 40 | } else { 41 | if (!any(names(x) %in% lon)) stop("'", lon, "' not found in your data", 42 | call. = FALSE) 43 | names(x)[names(x) %in% lon] <- lon_var <- "longitude" 44 | } 45 | 46 | structure(x, lat_var = lat_var, lon_var = lon_var) 47 | } 48 | 49 | lat_options <- c("lat", "latitude", "decimallatitude", "y") 50 | lon_options <- c("lon", "lng", "long", "longitude", "decimallongitude", "x") 51 | -------------------------------------------------------------------------------- /R/map_ggmap.R: -------------------------------------------------------------------------------- 1 | #' ggpmap visualization of species occurences 2 | #' 3 | #' THIS FUNCTION IS DEFUNCT 4 | #' 5 | #' @export 6 | #' @keywords internal 7 | #' @rdname map_ggmap-defunct 8 | #' @param ... Ignored 9 | map_ggmap <- function(...) .Defunct() 10 | -------------------------------------------------------------------------------- /R/map_ggplot.R: -------------------------------------------------------------------------------- 1 | #' ggplot2 mapping 2 | #' 3 | #' @export 4 | #' @template args 5 | #' @param map (character) One of world, world2, state, usa, county, france, 6 | #' italy, or nz 7 | #' @param color Default color of your points. 8 | #' @param size point size, Default: 3 9 | #' @param point_color Default color of your points. Deprecated, use 10 | #' `color` 11 | #' @param name (character) the column name that contains the name to use in 12 | #' creating the map. If left `NULL` we look for a "name" column. 13 | #' default: `NULL` 14 | #' @param ... Ignored 15 | #' @return A ggplot2 map, of class `gg/ggplot` 16 | #' 17 | #' @examples 18 | #' # map spocc output, here using a built in object 19 | #' data(occdat_eg1) 20 | #' map_ggplot(occdat_eg1) 21 | #' 22 | #' # map rgbif output, here using a built in object 23 | #' data(gbif_eg1) 24 | #' map_ggplot(gbif_eg1) 25 | #' 26 | #' @examples \dontrun{ 27 | #' ## spocc 28 | #' library("spocc") 29 | #' ddat <- occ(query = 'Lynx rufus californicus', from = 'gbif', limit=100) 30 | #' map_ggplot(ddat) 31 | #' map_ggplot(ddat$gbif) 32 | #' map_ggplot(ddat$gbif, "usa") 33 | #' map_ggplot(ddat, "county") 34 | #' 35 | #' ### usage of occ2sp() 36 | #' #### SpatialPoints 37 | #' spdat <- occ2sp(ddat) 38 | #' map_ggplot(spdat) 39 | #' #### SpatialPointsDataFrame 40 | #' spdatdf <- as(spdat, "SpatialPointsDataFrame") 41 | #' map_ggplot(spdatdf) 42 | #' 43 | #' ## rgbif 44 | #' if (requireNamespace("rgbif")) { 45 | #' library("rgbif") 46 | #' library("ggplot2") 47 | #' ### occ_search() output 48 | #' res <- occ_search(scientificName = "Puma concolor", limit = 100) 49 | #' map_ggplot(res) 50 | #' 51 | #' ### occ_data() output 52 | #' res <- occ_data(scientificName = "Puma concolor", limit = 100) 53 | #' map_ggplot(res) 54 | #' 55 | #' #### many taxa 56 | #' res <- occ_data(scientificName = c("Puma concolor", "Quercus lobata"), 57 | #' limit = 30) 58 | #' map_ggplot(res) 59 | #' 60 | #' ### add a convex hull 61 | #' hull(map_ggplot(res)) 62 | #' } 63 | #' 64 | #' ## data.frame 65 | #' df <- data.frame(name = c('Poa annua', 'Puma concolor', 'Foo bar'), 66 | #' longitude = c(-120, -121, -121), 67 | #' latitude = c(41, 42, 45), stringsAsFactors = FALSE) 68 | #' map_ggplot(df) 69 | #' 70 | #' # many species, each gets a different color 71 | #' library("spocc") 72 | #' spp <- c('Danaus plexippus', 'Accipiter striatus', 'Pinus contorta') 73 | #' dat <- occ(spp, from = 'gbif', limit = 30, has_coords = TRUE) 74 | #' map_ggplot(dat, color = c('#976AAE', '#6B944D', '#BD5945')) 75 | #'} 76 | 77 | map_ggplot <- function(x, map = "world", point_color = "#86161f", color = NULL, 78 | size = 3, lon = "longitude", lat = "latitude", name = NULL, ...) { 79 | UseMethod("map_ggplot") 80 | } 81 | 82 | #' @export 83 | map_ggplot.occdat <- function(x, map = "world", point_color = "#86161f", 84 | color = NULL, size = 3, lon = "longitude", lat = "latitude", 85 | name = NULL, ...) { 86 | 87 | check_inputs(match.call()) 88 | x <- spocc::occ2df(x) 89 | make_amap(dat_cleaner(x, lon = lon, lat = lat, name = name), map, 90 | color, size) 91 | } 92 | 93 | #' @export 94 | map_ggplot.occdatind <- function(x, map = "world", point_color = "#86161f", 95 | color = NULL, size = 3, lon = "longitude", lat = "latitude", 96 | name = NULL, ...) { 97 | 98 | check_inputs(match.call()) 99 | x <- spocc::occ2df(x) 100 | make_amap(dat_cleaner(x, lon = lon, lat = lat, name = name), map, 101 | color, size) 102 | } 103 | 104 | #' @export 105 | map_ggplot.gbif <- function(x, map = "world", point_color = "#86161f", 106 | color = NULL, size = 3, lon = "longitude", lat = "latitude", 107 | name = NULL, ...) { 108 | 109 | check_inputs(match.call()) 110 | x <- if ("data" %in% names(x)) x$data else bdt(lapply(x, function(z) z$data)) 111 | make_amap(dat_cleaner(x, lon = "decimalLongitude", 112 | lat = "decimalLatitude", name = name), map, color, size) 113 | } 114 | 115 | #' @export 116 | map_ggplot.gbif_data <- function(x, map = "world", point_color = "#86161f", 117 | color = NULL, size = 3, lon = "longitude", lat = "latitude", 118 | name = NULL, ...) { 119 | 120 | check_inputs(match.call()) 121 | x <- if ("data" %in% names(x)) x$data else bdt(lapply(x, function(z) z$data)) 122 | make_amap(dat_cleaner(x, lon = "decimalLongitude", 123 | lat = "decimalLatitude", name = name), map, color, size) 124 | } 125 | 126 | #' @export 127 | map_ggplot.SpatialPoints <- function(x, map = "world", point_color = "#86161f", 128 | color = NULL, size = 3, lon = "longitude", lat = "latitude", 129 | name = NULL, ...) { 130 | 131 | check_inputs(match.call()) 132 | make_amap(data.frame(x), map, color, size) 133 | } 134 | 135 | #' @export 136 | map_ggplot.SpatialPointsDataFrame <- function(x, map = "world", 137 | point_color = "#86161f", color = NULL, size = 3, lon = "longitude", 138 | lat = "latitude", name = NULL, ...) { 139 | 140 | check_inputs(match.call()) 141 | make_amap(data.frame(x), map, color, size) 142 | } 143 | 144 | #' @export 145 | map_ggplot.data.frame <- function(x, map = "world", point_color = "#86161f", 146 | color = NULL, size = 3, lon = "longitude", lat = "latitude", name = NULL, ...) { 147 | 148 | check_inputs(match.call()) 149 | make_amap(dat_cleaner(x, lon = lon, lat = lat, name = name), map, 150 | color, size) 151 | } 152 | 153 | #' @export 154 | map_ggplot.default <- function(x, map = "world", point_color = "#86161f", 155 | color = NULL, size = 3, lon = "longitude", lat = "latitude", name = NULL, ...) { 156 | 157 | stop( 158 | sprintf("map_ggplot does not support input of class '%s'", class(x)), 159 | call. = FALSE 160 | ) 161 | } 162 | 163 | ### helpers ------------------------------------------ 164 | sutils_blank_theme <- function(){ 165 | theme(axis.line = element_blank(), 166 | axis.text.x = element_blank(), 167 | axis.text.y = element_blank(), 168 | axis.ticks = element_blank(), 169 | axis.title.x = element_blank(), 170 | axis.title.y = element_blank(), 171 | panel.background = element_blank(), 172 | panel.border = element_blank(), 173 | panel.grid.major = element_blank(), 174 | panel.grid.minor = element_blank(), 175 | plot.background = element_blank(), 176 | plot.margin = rep(ggplot2::unit(0, "null"), 4)) 177 | } 178 | 179 | make_amap <- function(x, map, color, size) { 180 | wmap <- suppressMessages(ggplot2::map_data(map)) 181 | latitude <- longitude <- lat <- long <- decimalLongitude <- 182 | decimalLatitude <- group <- name <- NA 183 | ggplot(x, aes(longitude, latitude, colour = name)) + 184 | geom_point(size = size) + 185 | pick_colors(x, color) + 186 | geom_polygon( 187 | aes(long, lat, group = group), fill = NA, colour = "black", data = wmap) + 188 | sutils_blank_theme() 189 | } 190 | -------------------------------------------------------------------------------- /R/map_gist.R: -------------------------------------------------------------------------------- 1 | #' Make an interactive map to view in the browser as a GitHub gist 2 | #' 3 | #' @export 4 | #' @template args 5 | #' @param description Description for the Github gist, or leave to 6 | #' default (=no description) 7 | #' @param public (logical) Whether gist is public (default: `TRUE`) 8 | #' @param browse If `TRUE` (default) the map opens in your default browser. 9 | #' @param name (character) the column name that contains the name to use in 10 | #' creating the map. If left `NULL` we look for a "name" column. 11 | #' @param ... Further arguments passed on to [style_geojson()] 12 | #' 13 | #' @details See [gistr::gist_auth()] for help on authentication 14 | #' 15 | #' Does not support adding a convex hull via [hull()] 16 | #' 17 | #' @examples \dontrun{ 18 | #' ## spocc 19 | #' library("spocc") 20 | #' spp <- c('Danaus plexippus', 'Accipiter striatus', 'Pinus contorta') 21 | #' dat <- spocc::occ(spp, from=c('gbif','ecoengine'), limit=30, 22 | #' gbifopts=list(hasCoordinate=TRUE)) 23 | #' 24 | #' # Define colors 25 | #' map_gist(dat, color=c('#976AAE','#6B944D','#BD5945')) 26 | #' 27 | #' # Define colors and marker size 28 | #' map_gist(dat, color=c('#976AAE','#6B944D','#BD5945'), 29 | #' size=c('small','medium','large')) 30 | #' 31 | #' # Define symbols 32 | #' map_gist(dat, symbol=c('park','zoo','garden')) 33 | #' 34 | #' ## rgbif 35 | #' if (requireNamespace("rgbif")) { 36 | #' library("rgbif") 37 | #' ### occ_search() output 38 | #' res <- occ_search(scientificName = "Puma concolor", limit = 100) 39 | #' map_gist(res) 40 | #' 41 | #' ### occ_data() output 42 | #' res <- occ_data(scientificName = "Puma concolor", limit = 100) 43 | #' map_gist(res) 44 | #' 45 | #' #### many taxa 46 | #' res <- occ_data(scientificName = c("Puma concolor", "Quercus lobata"), 47 | #' limit = 30) 48 | #' res 49 | #' map_gist(res) 50 | #' } 51 | #' 52 | #' ## data.frame 53 | #' df <- data.frame(name = c('Poa annua', 'Puma concolor', 'Foo bar'), 54 | #' longitude = c(-120, -121, -121), 55 | #' latitude = c(41, 42, 45), stringsAsFactors = FALSE) 56 | #' map_gist(df) 57 | #' 58 | #' ### usage of occ2sp() 59 | #' #### SpatialPoints 60 | #' spdat <- occ2sp(dat) 61 | #' map_gist(spdat) 62 | #' #### SpatialPointsDataFrame 63 | #' spdatdf <- as(spdat, "SpatialPointsDataFrame") 64 | #' map_gist(spdatdf) 65 | #' } 66 | map_gist <- function(x, description = "", public = TRUE, browse = TRUE, 67 | lon = 'longitude', lat = 'latitude', name = NULL, ...) { 68 | UseMethod("map_gist") 69 | } 70 | 71 | #' @export 72 | map_gist.occdat <- function(x, description = "", public = TRUE, browse = TRUE, 73 | lon = 'longitude', lat = 'latitude', 74 | name = NULL, ...) { 75 | map_gister(spocc::occ2df(x), description, public, browse, ...) 76 | } 77 | 78 | #' @export 79 | map_gist.occdatind <- function(x, description = "", public = TRUE, 80 | browse = TRUE, lon = 'longitude', lat = 'latitude', name = NULL, ...) { 81 | x <- check_name(x, name) 82 | map_gister(spocc::occ2df(x), description, public, browse, ...) 83 | } 84 | 85 | #' @export 86 | map_gist.gbif <- function(x, description = "", public = TRUE, browse = TRUE, 87 | lon = 'longitude', lat = 'latitude', name = NULL, ...) { 88 | x <- if ("data" %in% names(x)) x$data else bdt(lapply(x, function(z) z$data)) 89 | x <- re_name(x, c('decimalLatitude' = 'latitude')) 90 | x <- re_name(x, c('decimalLongitude' = 'longitude')) 91 | x <- check_name(x, name) 92 | map_gister(x, description, public, browse, ...) 93 | } 94 | 95 | #' @export 96 | map_gist.gbif_data <- function(x, description = "", public = TRUE, browse = TRUE, 97 | lon = 'longitude', lat = 'latitude', name = NULL, ...) { 98 | x <- if ("data" %in% names(x)) x$data else bdt(lapply(x, function(z) z$data)) 99 | x <- re_name(x, c('decimalLatitude' = 'latitude')) 100 | x <- re_name(x, c('decimalLongitude' = 'longitude')) 101 | x <- check_name(x, name) 102 | map_gister(x, description, public, browse, ...) 103 | } 104 | 105 | #' @export 106 | map_gist.data.frame <- function(x, description = "", public = TRUE, 107 | browse = TRUE, lon = 'longitude', lat = 'latitude', name = NULL, ...) { 108 | 109 | x <- guess_latlon(x, lat, lon) 110 | x <- check_name(x, name) 111 | map_gister(x, description, public, browse, ...) 112 | } 113 | 114 | #' @export 115 | map_gist.SpatialPoints <- function(x, description = "", public = TRUE, 116 | browse = TRUE, lon = 'longitude', lat = 'latitude', name = NULL, ...) { 117 | x <- data.frame(x) 118 | x <- guess_latlon(x, lat, lon) 119 | x <- check_name(x, name) 120 | map_gister(x, description, public, browse, ...) 121 | } 122 | 123 | #' @export 124 | map_gist.SpatialPointsDataFrame <- function(x, description = "", public = TRUE, 125 | browse = TRUE, lon = 'longitude', lat = 'latitude', name = NULL, ...) { 126 | x <- data.frame(x) 127 | x <- guess_latlon(x, lat, lon) 128 | x <- check_name(x, name) 129 | map_gister(x, description, public, browse, ...) 130 | } 131 | 132 | #' @export 133 | map_gist.default <- function(x, description = "", public = TRUE, browse = TRUE, 134 | lon = 'longitude', lat = 'latitude', name = NULL, ...) { 135 | stop(sprintf("map_gist does not support input of class '%s'", class(x)), 136 | call. = FALSE) 137 | } 138 | 139 | # helpers 140 | map_gister <- function(x, description, public, browse, ...) { 141 | x <- x[stats::complete.cases(x$latitude, x$longitude), ] 142 | datgeojson <- style_geojson(input = x, var = "name", ...) 143 | geofile <- togeojson2(datgeojson) 144 | gistr::gist_create(geofile, description = description, public = public, 145 | browse = browse) 146 | } 147 | -------------------------------------------------------------------------------- /R/map_leaflet.R: -------------------------------------------------------------------------------- 1 | #' Make interactive maps with Leaflet.js 2 | #' 3 | #' @export 4 | #' 5 | #' @template args 6 | #' @param color Default color of your points. 7 | #' @param size point size, Default: 13 8 | #' @param name (character) the column name that contains the name to use in 9 | #' creating the map. If left `NULL` we look for a "name" column. 10 | #' @param ... Ignored 11 | #' @details We add popups by default, and add all columns to the popup. The 12 | #' html is escaped with `htmltools::htmlEscape()` 13 | #' @return a Leaflet map in Viewer in Rstudio, or in your default browser 14 | #' otherwise 15 | #' @examples \dontrun{ 16 | #' ## spocc 17 | #' library("spocc") 18 | #' (out <- occ(query='Accipiter striatus', from='gbif', limit=50, 19 | #' has_coords=TRUE)) 20 | #' ### with class occdat 21 | #' map_leaflet(out) 22 | #' ### with class occdatind 23 | #' map_leaflet(out$gbif) 24 | #' ### use occ2sp 25 | #' map_leaflet(occ2sp(out)) 26 | #' 27 | #' ## rgbif 28 | #' if (requireNamespace("rgbif")) { 29 | #' library("rgbif") 30 | #' res <- occ_search(scientificName = "Puma concolor", limit = 100) 31 | #' map_leaflet(res) 32 | #' } 33 | #' 34 | #' ## SpatialPoints class 35 | #' library("sp") 36 | #' df <- data.frame(longitude = c(-120,-121), 37 | #' latitude = c(41, 42), stringsAsFactors = FALSE) 38 | #' x <- SpatialPoints(df) 39 | #' map_leaflet(x) 40 | #' 41 | #' ## SpatialPointsDataFrame class 42 | #' if (requireNamespace("rgbif")) { 43 | #' library("rgbif") 44 | #' ### occ_search() output 45 | #' res <- occ_search(scientificName = "Puma concolor", limit = 100) 46 | #' x <- res$data 47 | #' library("sp") 48 | #' x <- x[stats::complete.cases(x$decimalLatitude, x$decimalLongitude), ] 49 | #' coordinates(x) <- ~decimalLongitude+decimalLatitude 50 | #' map_leaflet(x) 51 | #' 52 | #' ### occ_data() output 53 | #' res <- occ_data(scientificName = "Puma concolor", limit = 100) 54 | #' map_leaflet(res) 55 | #' } 56 | #' 57 | #' #### many taxa 58 | #' res <- occ_data(scientificName = c("Puma concolor", "Quercus lobata"), 59 | #' limit = 30) 60 | #' res 61 | #' map_leaflet(res) 62 | #' 63 | #' 64 | #' ## data.frame 65 | #' df <- data.frame(name = c('Poa annua', 'Puma concolor'), 66 | #' longitude = c(-120,-121), 67 | #' latitude = c(41, 42), stringsAsFactors = FALSE) 68 | #' map_leaflet(df) 69 | #' 70 | #' # many species 71 | #' library("spocc") 72 | #' spp <- c('Danaus plexippus', 'Accipiter striatus', 'Pinus contorta') 73 | #' dat <- occ(spp, from = 'gbif', limit = 50, has_coords = TRUE) 74 | #' map_leaflet(dat) 75 | #' map_leaflet(dat, color = c('#AFFF71', '#AFFF71', '#AFFF71')) 76 | #' map_leaflet(dat, color = c('#976AAE', '#6B944D', '#BD5945')) 77 | #' 78 | #' # add a convex hull 79 | #' ## map_leaflet(dat) %>% hull() # using pipes 80 | #' hull(map_leaflet(dat)) 81 | #' } 82 | map_leaflet <- function(x, lon = 'longitude', lat = 'latitude', color = NULL, 83 | size = 13, name = NULL, ...) { 84 | UseMethod("map_leaflet") 85 | } 86 | 87 | #' @export 88 | map_leaflet.occdat <- function(x, lon = 'longitude', lat = 'latitude', 89 | color = NULL, size = 13, name = NULL, ...) { 90 | 91 | make_map_ll(dat_cleaner(spocc::occ2df(x), lon, lat, name), color, size) 92 | } 93 | 94 | #' @export 95 | map_leaflet.occdatind <- function(x, lon = 'longitude', lat = 'latitude', 96 | color = NULL, size = 13, name = NULL, ...) { 97 | 98 | make_map_ll(dat_cleaner(spocc::occ2df(x), lon, lat, name), color, size) 99 | } 100 | 101 | #' @export 102 | map_leaflet.SpatialPoints <- function(x, lon = 'longitude', lat = 'latitude', 103 | color = NULL, size = 13, name = NULL, ...) { 104 | 105 | make_map(x, color, size) 106 | } 107 | 108 | #' @export 109 | map_leaflet.SpatialPointsDataFrame <- function(x, lon = 'longitude', 110 | lat = 'latitude', color = NULL, size = 13, name = NULL, ...) { 111 | 112 | make_map(x, color, size) 113 | } 114 | 115 | #' @export 116 | map_leaflet.gbif <- function(x, lon = 'longitude', lat = 'latitude', 117 | color = NULL, size = 13, name = NULL, ...) { 118 | 119 | x <- if ("data" %in% names(x)) x$data else bdt(lapply(x, function(z) z$data)) 120 | make_map_ll( 121 | dat_cleaner(x, lon = 'decimalLongitude', lat = 'decimalLatitude', name), 122 | color = color, 123 | size = size 124 | ) 125 | } 126 | 127 | #' @export 128 | map_leaflet.gbif_data <- function(x, lon = 'longitude', lat = 'latitude', 129 | color = NULL, size = 13, name = NULL, ...) { 130 | 131 | x <- if ("data" %in% names(x)) x$data else bdt(lapply(x, function(z) z$data)) 132 | make_map_ll( 133 | dat_cleaner(x, lon = 'decimalLongitude', lat = 'decimalLatitude', name), 134 | color = color, 135 | size = size 136 | ) 137 | } 138 | 139 | #' @export 140 | map_leaflet.data.frame <- function(x, lon = 'longitude', lat = 'latitude', 141 | color = NULL, size = 13, name = NULL, ...) { 142 | 143 | make_map_ll(dat_cleaner(x, lon, lat, name), color, size) 144 | } 145 | 146 | #' @export 147 | map_leaflet.default <- function(x, lon = 'longitude', lat = 'latitude', 148 | color = NULL, size = 13, name = NULL, ...) { 149 | 150 | stop( 151 | sprintf("map_leaflet does not support input of class '%s'", class(x)), 152 | call. = FALSE 153 | ) 154 | } 155 | 156 | # helpers ------------------------------------ 157 | make_map <- function(x, color, size) { 158 | lf <- leaflet::leaflet(data = x) 159 | lf <- leaflet::addTiles(lf) 160 | leaflet::addMarkers(lf) 161 | } 162 | 163 | make_map_ll <- function(x, color, size) { 164 | x <- check_colors(x, color) 165 | x$popups <- make_popups(x) 166 | lf <- leaflet::leaflet(data = x) 167 | lf <- leaflet::addTiles(lf) 168 | leaflet::addCircleMarkers( 169 | lf, ~longitude, ~latitude, 170 | color = ~color, radius = size, 171 | popup = ~popups 172 | ) 173 | } 174 | 175 | make_popups <- function(x) { 176 | res <- c() 177 | for (i in seq_len(NROW(x))) { 178 | temp <- c() 179 | for (j in seq_along(x[i, ])) { 180 | temp[j] <- sprintf('%s%s', 181 | names(x)[j], x[i,j]) 182 | } 183 | res[i] <- sprintf('\n%s\n
', 184 | paste0(temp, collapse = "\n")) 185 | temp <- NULL 186 | } 187 | res 188 | } 189 | -------------------------------------------------------------------------------- /R/map_plot.R: -------------------------------------------------------------------------------- 1 | #' Base R mapping 2 | #' 3 | #' @export 4 | #' @template args 5 | #' @param color Default color of your points. 6 | #' @param size point size, passed to `cex` Default: 1 7 | #' @param pch point symbol shape, Default: 16 8 | #' @param hull (logical) whether to add a convex hull. Default: `FALSE` 9 | #' @param name (character) the column name that contains the name to use in 10 | #' creating the map. If left `NULL` we look for a "name" column. 11 | #' @param ... Further args to [graphics::points()] 12 | #' @return Plots a world scale map 13 | #' 14 | #' @examples 15 | #' # map spocc output, here using a built in object 16 | #' data(occdat_eg1) 17 | #' map_plot(occdat_eg1) 18 | #' 19 | #' # map rgbif output, here using a built in object 20 | #' data(gbif_eg1) 21 | #' map_plot(gbif_eg1) 22 | #' 23 | #' @examples \dontrun{ 24 | #' ## spocc 25 | #' library("spocc") 26 | #' (out <- occ(query='Accipiter striatus', from='gbif', limit=25, 27 | #' has_coords=TRUE)) 28 | #' ### class occdat 29 | #' map_plot(out) 30 | #' map_plot(out, hull = TRUE) 31 | #' ### class occdatind 32 | #' map_plot(out$gbif) 33 | #' map_plot(out$gbif, hull = TRUE) 34 | #' 35 | #' ## rgbif 36 | #' if (requireNamespace("rgbif")) { 37 | #' library("rgbif") 38 | #' ### occ_search() output 39 | #' res <- occ_search(scientificName = "Puma concolor", limit = 100) 40 | #' map_plot(res) 41 | #' map_plot(res, hull = TRUE) 42 | #' 43 | #' ### occ_data() output 44 | #' res <- occ_data(scientificName = "Puma concolor", limit = 100) 45 | #' map_plot(res) 46 | #' #### many taxa 47 | #' res <- occ_data(scientificName = c("Puma concolor", "Quercus lobata"), 48 | #' limit = 30) 49 | #' res 50 | #' map_plot(res) 51 | #' } 52 | #' 53 | #' 54 | #' ## data.frame 55 | #' df <- data.frame( 56 | #' name = c('Poa annua', 'Puma concolor', 'Foo bar', 'Stuff things'), 57 | #' longitude = c(-125, -123, -121, -110), 58 | #' latitude = c(41, 42, 45, 30), stringsAsFactors = FALSE) 59 | #' map_plot(df) 60 | #' map_plot(df, hull = TRUE) 61 | #' 62 | #' ### usage of occ2sp() 63 | #' #### SpatialPoints 64 | #' spdat <- occ2sp(out) 65 | #' map_plot(spdat) 66 | #' map_plot(spdat, hull = TRUE) 67 | #' 68 | #' #### SpatialPointsDataFrame 69 | #' spdatdf <- as(spdat, "SpatialPointsDataFrame") 70 | #' map_plot(spdatdf) 71 | #' map_plot(spdatdf, hull = TRUE) 72 | #' 73 | #' # many species, each gets a different color 74 | #' library("spocc") 75 | #' spp <- c('Danaus plexippus', 'Accipiter striatus', 'Pinus contorta', 76 | #' 'Ursus americanus') 77 | #' dat <- occ(spp, from = 'gbif', limit = 30, has_coords = TRUE, 78 | #' gbifopts = list(country = 'US')) 79 | #' map_plot(dat) 80 | #' map_plot(dat, hull = TRUE) 81 | #' ## diff. color for each taxon 82 | #' map_plot(dat, color = c('#976AAE', '#6B944D', '#BD5945', 'red')) 83 | #' map_plot(dat, color = c('#976AAE', '#6B944D', '#BD5945', 'red'), hull = TRUE) 84 | #' 85 | #' # add a convex hull 86 | #' if (requireNamespace("rgbif")) { 87 | #' library("rgbif") 88 | #' res <- occ_search(scientificName = "Puma concolor", limit = 100) 89 | #' map_plot(res, hull = FALSE) 90 | #' map_plot(res, hull = TRUE) 91 | #' } 92 | #' } 93 | map_plot <- function(x, lon = 'longitude', lat = 'latitude', color = NULL, 94 | size = 1, pch = 16, hull = FALSE, name = NULL, ...) { 95 | UseMethod("map_plot") 96 | } 97 | 98 | #' @export 99 | map_plot.occdat <- function(x, lon = 'longitude', lat = 'latitude', 100 | color = NULL, size = 1, pch = 16, 101 | hull = FALSE, name = NULL, ...) { 102 | df <- spocc::occ2df(x) 103 | x <- check_name(x, name) 104 | df <- check_colors(df, color) 105 | plot_er(plot_prep(df), size, hull, ...) 106 | } 107 | 108 | #' @export 109 | map_plot.occdatind <- function(x, lon = 'longitude', lat = 'latitude', 110 | color = NULL, size = 1, pch = 16, 111 | hull = FALSE, name = NULL, ...) { 112 | df <- spocc::occ2df(x) 113 | x <- check_name(x, name) 114 | df <- check_colors(df, color) 115 | plot_er(plot_prep(df), size, hull, ...) 116 | } 117 | 118 | #' @export 119 | map_plot.gbif <- function(x, lon = 'longitude', lat = 'latitude', color = NULL, 120 | size = 1, pch = 16, hull = FALSE, name = NULL, ...) { 121 | df <- if ("data" %in% names(x)) x$data else bdt(lapply(x, function(z) z$data)) 122 | df <- guess_latlon(df) 123 | x <- check_name(x, name) 124 | df <- df[stats::complete.cases(df$latitude, df$longitude), ] 125 | df <- df[df$longitude != 0, ] 126 | df <- check_colors(df, color) 127 | sp::coordinates(df) <- ~longitude + latitude 128 | plot_er(df, size, hull, ...) 129 | } 130 | 131 | #' @export 132 | map_plot.gbif_data <- function(x, lon = 'longitude', lat = 'latitude', color = NULL, 133 | size = 1, pch = 16, hull = FALSE, name = NULL, ...) { 134 | df <- if ("data" %in% names(x)) x$data else bdt(lapply(x, function(z) z$data)) 135 | df <- guess_latlon(df) 136 | x <- check_name(x, name) 137 | df <- df[stats::complete.cases(df$latitude, df$longitude), ] 138 | df <- df[df$longitude != 0, ] 139 | df <- check_colors(df, color) 140 | sp::coordinates(df) <- ~longitude + latitude 141 | plot_er(df, size, hull, ...) 142 | } 143 | 144 | #' @export 145 | map_plot.data.frame <- function(x, lon = 'longitude', lat = 'latitude', 146 | color = NULL, size = 1, pch = 16, 147 | hull = FALSE, name = NULL, ...) { 148 | x <- guess_latlon(x, lat, lon) 149 | x <- check_name(x, name) 150 | x <- check_colors(x, color) 151 | plot_er(plot_prep(x), size, hull, ...) 152 | } 153 | 154 | #' @export 155 | map_plot.SpatialPoints <- function(x, lon = 'longitude', lat = 'latitude', 156 | color = NULL, size = 1, pch = 16, 157 | hull = FALSE, name = NULL, ...) { 158 | x <- data.frame(x) 159 | x <- guess_latlon(x, lat, lon) 160 | x <- check_name(x, name) 161 | x <- check_colors(x, color) 162 | plot_er(plot_prep(x), size, hull, ...) 163 | } 164 | 165 | #' @export 166 | map_plot.SpatialPointsDataFrame <- function(x, lon = 'longitude', 167 | lat = 'latitude', color = NULL, 168 | size = 1, pch = 16, 169 | hull = FALSE, name = NULL, ...) { 170 | x <- data.frame(x) 171 | x <- guess_latlon(x, lat, lon) 172 | x <- check_name(x, name) 173 | x <- check_colors(x, color) 174 | plot_er(plot_prep(x), size, hull, ...) 175 | } 176 | 177 | #' @export 178 | map_plot.default <- function(x, lon = 'longitude', lat = 'latitude', 179 | color = NULL, size = 1, pch = 16, 180 | hull = FALSE, name = NULL, ...) { 181 | stop( 182 | sprintf("map_plot does not support input of class '%s'", class(x)), 183 | call. = FALSE 184 | ) 185 | } 186 | 187 | 188 | ##### helpers -------------------- 189 | plot_prep <- function(x) { 190 | x <- x[stats::complete.cases(x$latitude, x$longitude), ] 191 | x <- x[x$longitude != 0, ] 192 | sp::coordinates(x) <- ~longitude + latitude 193 | x 194 | } 195 | 196 | plot_er <- function(x, size, hull, pch = 16, ...) { 197 | sp::proj4string(x) <- sp::CRS("+init=epsg:4326") 198 | maps::map() 199 | graphics::points(x, pch = pch, col = x$color, cex = size, ...) 200 | if (length(unique(x$color)) > 1) { 201 | graphics::legend(x = -180, y = -20, unique(x$name), pch = 16, 202 | col = unique(x$color), bty = "n", cex = 0.8) 203 | } 204 | make_hull(x, hull) 205 | } 206 | 207 | make_hull <- function(x, hull) { 208 | if (hull) { 209 | x <- data.frame(sp::coordinates(x)) 210 | hpts <- grDevices::chull(x$longitude, x$latitude) 211 | hpts <- c(hpts, hpts[1]) 212 | graphics::lines(x[hpts, ]) 213 | } 214 | } 215 | -------------------------------------------------------------------------------- /R/mapr-package.R: -------------------------------------------------------------------------------- 1 | #' @title mapr 2 | #' @description Visualize species occurrence data 3 | #' @section Many inputs: 4 | #' All functions take the following kinds of inputs: 5 | #' 6 | #' - An object of class `occdat`, from the package \pkg{spocc}. 7 | #' An object of this class is composed of many objects of class 8 | #' `occdatind` 9 | #' - An object of class `occdatind`, from the package \pkg{spocc} 10 | #' - An object of class `gbif`, from the package \pkg{rgbif} 11 | #' - An object of class `data.frame`. This data.frame can have any 12 | #' columns, but must include a column for taxonomic names (e.g., `name`), 13 | #' and for latitude and longitude (we guess your lat/long columns, starting 14 | #' with the default `latitude` and `longitude`) 15 | #' - An object of class `SpatialPoints` 16 | #' - An object of class `SpatialPointsDatFrame` 17 | #' 18 | #' @section Package API: 19 | #' 20 | #' - [map_plot()] - static Base R plots 21 | #' - [map_ggplot()] - static ggplot2 plots 22 | #' - [map_leaflet()] - interactive Leaflet.js interactive maps 23 | #' - [map_gist()] - ineractive, shareable maps on GitHub Gists 24 | #' 25 | #' @importFrom ggplot2 geom_point aes ggtitle labs map_data 26 | #' ggplot geom_point geom_polygon element_blank theme scale_color_manual 27 | #' scale_color_brewer 28 | #' @importFrom sp SpatialPoints SpatialPointsDataFrame plot 29 | #' @importFrom maps map 30 | #' @import leaflet 31 | #' @name mapr-package 32 | #' @aliases mapr 33 | #' @docType package 34 | #' @keywords package 35 | #' @author Scott Chamberlain 36 | NULL 37 | 38 | #' Example dataset: output from call to [spocc::occ()] 39 | #' 40 | #' A dataset with 25 rows, and 62 columns, from the query: 41 | #' `occ(query='Accipiter striatus', from='gbif', limit=25, has_coords=TRUE)` 42 | #' 43 | #' See `inst/ignore/datasets.R` for the code to prepare this dataaset 44 | #' 45 | #' @docType data 46 | #' @keywords datasets 47 | #' @format A data frame with 25 rows and 62 variables 48 | #' @name occdat_eg1 49 | NULL 50 | 51 | #' Example dataset: output from call to `rgbif::occ_search()` 52 | #' 53 | #' A dataset with 50 rows, and 101 columns, from the query: 54 | #' `rgbif::occ_search(scientificName = "Puma concolor", limit = 100)` 55 | #' 56 | #' See `inst/ignore/datasets.R` for the code to prepare this dataaset 57 | #' 58 | #' @docType data 59 | #' @keywords datasets 60 | #' @format A data frame with 50 rows and 101 variables 61 | #' @name gbif_eg1 62 | NULL 63 | -------------------------------------------------------------------------------- /R/occ2sp.R: -------------------------------------------------------------------------------- 1 | #' Create a spatial points dataframe from a spocc search 2 | #' 3 | #' @export 4 | #' 5 | #' @param x The resuslts of a spocc search called by [spocc::occ()] 6 | #' @param coord_string A valid EPGS cooridate string from the sp package, 7 | #' the default is WSGS 84 8 | #' @param just_coords Return data frame with specios names and provenance or 9 | #' just a spatial points object, which is the default. 10 | #' 11 | #' @details This function will return either a spatial points dataframe or 12 | #' spatial points object. Conversion to spatial points objects allows spocc 13 | #' searches to interact with other spatial data sources. More coordinate system 14 | #' codes can be found at the EPGS registry 15 | #' 16 | #' @examples \dontrun{ 17 | #' ### See points on a map 18 | #' library("maptools") 19 | #' library("spocc") 20 | #' data(wrld_simpl) 21 | #' plot(wrld_simpl[wrld_simpl$NAME == "United States", ], xlim = c(-70, -60)) 22 | #' out <- occ(query = "Accipiter striatus", from = c("vertnet", "gbif"), 23 | #' limit = 50) 24 | #' xx <- occ2sp(out, just_coords = TRUE) 25 | #' points(xx, col = 2) 26 | #' } 27 | occ2sp <- function(x, coord_string = "+proj=longlat +datum=WGS84", 28 | just_coords = FALSE) { 29 | 30 | x <- spocc::occ2df(x) 31 | 32 | # numerics 33 | x$longitude <- as.numeric(x$longitude) 34 | x$latitude <- as.numeric(x$latitude) 35 | 36 | # remove NA rows 37 | x <- x[stats::complete.cases(x$latitude, x$longitude), ] 38 | 39 | # check valid coords 40 | index <- 1:dim(x)[1] 41 | index <- index[ 42 | (x$longitude < 180) & 43 | (x$longitude > -180) & 44 | !is.na(x$longitude)] 45 | index <- index[ 46 | (x$latitude[index] < 90) & 47 | (x$latitude[index] > -90) & 48 | !is.na(x$latitude[index])] 49 | 50 | spobj <- sp::SpatialPoints(as.matrix(x[index,c('longitude', 'latitude')]), 51 | proj4string = sp::CRS(coord_string)) 52 | 53 | sp_df <- sp::SpatialPointsDataFrame( 54 | spobj, 55 | data = data.frame(x[index, c('name', "prov")])) 56 | if (just_coords) spobj else sp_df 57 | } 58 | -------------------------------------------------------------------------------- /R/rename.R: -------------------------------------------------------------------------------- 1 | re_name <- function (x, replace, warn_missing = TRUE) { 2 | names(x) <- revalue(names(x), replace, warn_missing = warn_missing) 3 | x 4 | } 5 | 6 | revalue <- function (x, replace = NULL, warn_missing = TRUE) { 7 | if (!is.null(x) && !is.factor(x) && !is.character(x)) { 8 | stop("x is not a factor or a character vector.") 9 | } 10 | mapvalues(x, from = names(replace), to = replace, warn_missing = warn_missing) 11 | } 12 | 13 | mapvalues <- function (x, from, to, warn_missing = TRUE) { 14 | if (length(from) != length(to)) { 15 | stop("`from` and `to` vectors are not the same length.") 16 | } 17 | if (!is.atomic(x)) { 18 | stop("`x` must be an atomic vector.") 19 | } 20 | if (is.factor(x)) { 21 | levels(x) <- mapvalues(levels(x), from, to, warn_missing) 22 | return(x) 23 | } 24 | mapidx <- match(x, from) 25 | mapidxNA <- is.na(mapidx) 26 | from_found <- sort(unique(mapidx)) 27 | if (warn_missing && length(from_found) != length(from)) { 28 | message("The following `from` values were not present in `x`: ", 29 | paste(from[!(1:length(from) %in% from_found)], collapse = ", ")) 30 | } 31 | x[!mapidxNA] <- to[mapidx[!mapidxNA]] 32 | x 33 | } 34 | -------------------------------------------------------------------------------- /R/util_github.r: -------------------------------------------------------------------------------- 1 | #' Style a data.frame prior to converting to geojson. 2 | #' 3 | #' @export 4 | #' @param input A data.frame 5 | #' @param var A single variable to map colors, symbols, and/or sizes to. 6 | #' @param var_col The variable to map colors to. 7 | #' @param var_sym The variable to map symbols to. 8 | #' @param var_size The variable to map size to. 9 | #' @param color Valid RGB hex color 10 | #' @param symbol An icon ID from the Maki project 11 | #' https://labs.mapbox.com/maki-icons/ or a single alphanumeric character 12 | #' (a-z or 0-9). 13 | #' @param size One of 'small', 'medium', or 'large' 14 | style_geojson <- function(input, var = NULL, var_col = NULL, var_sym = NULL, 15 | var_size = NULL, color = NULL, symbol = NULL, size = NULL) { 16 | if (!inherits(input, "data.frame")) 17 | stop("Your input object needs to be a data.frame") 18 | if (nrow(input) == 0) 19 | stop("Your data.frame has no rows...") 20 | if (is.null(var_col) & is.null(var_sym) & is.null(var_size)) 21 | var_col <- var_sym <- var_size <- var 22 | if (!is.null(color)) { 23 | if (length(color) == 1) { 24 | color_vec <- rep(color, nrow(input)) 25 | } else { 26 | mapping <- data.frame(var = unique(input[[var_col]]), col2 = color) 27 | stuff <- input[[var_col]] 28 | color_vec <- with(mapping, col2[match(stuff, var)]) 29 | } 30 | } else { 31 | color_vec <- NULL 32 | } 33 | if (!is.null(symbol)) { 34 | if (length(symbol) == 1) { 35 | symbol_vec <- rep(symbol, nrow(input)) 36 | } else { 37 | mapping <- data.frame(var = unique(input[[var_sym]]), symb = symbol) 38 | stuff <- input[[var_sym]] 39 | symbol_vec <- with(mapping, symb[match(stuff, var)]) 40 | } 41 | } else { 42 | symbol_vec <- NULL 43 | } 44 | if (!is.null(size)) { 45 | if (length(size) == 1) { 46 | size_vec <- rep(size, nrow(input)) 47 | } else { 48 | mapping <- data.frame(var = unique(input[[var_size]]), sz = size) 49 | stuff <- input[[var_size]] 50 | size_vec <- with(mapping, sz[match(stuff, var)]) 51 | } 52 | } else { 53 | size_vec <- NULL 54 | } 55 | output <- do.call(cbind, sc(list(input, `marker-color` = color_vec, 56 | `marker-symbol` = symbol_vec, 57 | `marker-size` = size_vec))) 58 | return(output) 59 | } 60 | 61 | # param - input The file being uploaded, path to the file on your machine. 62 | togeojson2 <- function(x) { 63 | ff <- list(type = "FeatureCollection", features = Map(function(z) { 64 | list( 65 | type = "Feature", 66 | properties = as.list(z), 67 | geometry = list( 68 | type = "Point", 69 | coordinates = c(as.numeric(z$longitude), as.numeric(z$latitude)) 70 | ) 71 | ) 72 | }, unname(apply(x, 1, as.list)))) 73 | ffj <- jsonlite::toJSON(ff, auto_unbox = TRUE) 74 | geofile <- tempfile(fileext = ".geojson") 75 | geofileConn <- file(geofile) 76 | writeLines(ffj, geofileConn) 77 | return(geofile) 78 | } 79 | -------------------------------------------------------------------------------- /R/zzz.r: -------------------------------------------------------------------------------- 1 | sc <- function(l) Filter(Negate(is.null), l) 2 | 3 | pluck <- function(x, name, type) { 4 | if (missing(type)) { 5 | lapply(x, "[[", name) 6 | } else { 7 | vapply(x, "[[", name, FUN.VALUE = type) 8 | } 9 | } 10 | 11 | strextract <- function(str, pattern) regmatches(str, regexpr(pattern, str)) 12 | 13 | strtrim <- function(str) gsub("^\\s+|\\s+$", "", str) 14 | 15 | check4pkg <- function(x) { 16 | if (!requireNamespace(x, quietly = TRUE)) { 17 | stop("Please install ", x, call. = FALSE) 18 | } else { 19 | invisible(TRUE) 20 | } 21 | } 22 | 23 | check_inputs <- function(x) { 24 | calls <- names(sapply(x, deparse))[-1] 25 | calls_vec <- "point_color" %in% calls 26 | if (any(calls_vec)) { 27 | stop("The parameter 'point_color' has been replaced by 'color'", 28 | call. = FALSE) 29 | } 30 | } 31 | 32 | bdt <- function(x) { 33 | (bb <- data.table::setDF( 34 | data.table::rbindlist(x, fill = TRUE, use.names = TRUE) 35 | )) 36 | } 37 | 38 | check_name <- function(x, name = NULL) { 39 | if (!is.null(name)) { 40 | if (!name %in% names(x)) { 41 | stop(sprintf("'%s' not found in the data", name), 42 | call. = FALSE) 43 | } 44 | if ("name" %in% names(x)) { 45 | if (name != "name") { 46 | message("existing 'name' column found; setting it to 'name_old'") 47 | names(x)[which(names(x) == "name")] <- "name_old" 48 | } 49 | } 50 | names(x)[which(names(x) == name)] <- "name" 51 | } 52 | return(x) 53 | } 54 | 55 | dat_cleaner <- function(x, lon = 'longitude', lat = 'latitude', name = NULL) { 56 | x <- guess_latlon(x, lat, lon) 57 | x <- x[stats::complete.cases(x$latitude, x$longitude), ] 58 | x <- check_name(x, name) 59 | return(x) 60 | } 61 | -------------------------------------------------------------------------------- /README-not.md: -------------------------------------------------------------------------------- 1 | mapr 2 | ==== 3 | 4 | 5 | 6 | [![R-check](https://github.com/ropensci/mapr/workflows/R-check/badge.svg)](https://github.com/ropensci/mapr/actions/) 7 | [![cran checks](https://cranchecks.info/badges/worst/mapr)](https://cranchecks.info/pkgs/mapr) 8 | [![codecov](https://codecov.io/gh/ropensci/mapr/branch/master/graph/badge.svg)](https://codecov.io/gh/ropensci/mapr) 9 | [![rstudio mirror downloads](https://cranlogs.r-pkg.org/badges/mapr?color=FAB657)](https://github.com/r-hub/cranlogs.app) 10 | [![cran version](https://www.r-pkg.org/badges/version/mapr)](https://cran.r-project.org/package=mapr) 11 | 12 | 13 | Helper for making maps of species occurrence data, including for: 14 | 15 | * spocc (https://github.com/ropensci/spocc) 16 | * rgbif (https://github.com/ropensci/rgbif) 17 | * and some `sp` classes 18 | 19 | This package has utilities for making maps with: 20 | 21 | * base R 22 | * ggplot2 23 | * Leaflet - via `leaflet` pkg 24 | * GitHub Gists - via `gistr` package 25 | 26 | Get started with the docs: https://docs.ropensci.org/mapr/ 27 | 28 | ## Installation 29 | 30 | Install `mapr` 31 | 32 | 33 | ```r 34 | install.packages("mapr") 35 | ``` 36 | 37 | Or the development version from GitHub 38 | 39 | 40 | ```r 41 | remotes::install_github("ropensci/mapr") 42 | ``` 43 | 44 | 45 | ```r 46 | library("mapr") 47 | library("spocc") 48 | ``` 49 | 50 | ## Meta 51 | 52 | * Please [report any issues or bugs](https://github.com/ropensci/mapr/issues). 53 | * License: MIT 54 | * Get citation information for `mapr` in R doing `citation(package = 'mapr')` 55 | * Please note that this package is released with a [Contributor Code of Conduct](https://ropensci.org/code-of-conduct/). By contributing to this project, you agree to abide by its terms. 56 | 57 | [![ropensci_footer](https://ropensci.org/public_images/github_footer.png)](https://ropensci.org) 58 | -------------------------------------------------------------------------------- /README.Rmd: -------------------------------------------------------------------------------- 1 | mapr 2 | ==== 3 | 4 | ```{r echo=FALSE} 5 | knitr::opts_chunk$set( 6 | warning = FALSE, 7 | message = FALSE, 8 | collapse = TRUE, 9 | comment = "#>" 10 | ) 11 | ``` 12 | 13 | [![R-check](https://github.com/ropensci/mapr/workflows/R-check/badge.svg)](https://github.com/ropensci/mapr/actions/) 14 | [![cran checks](https://cranchecks.info/badges/worst/mapr)](https://cranchecks.info/pkgs/mapr) 15 | [![codecov](https://codecov.io/gh/ropensci/mapr/branch/master/graph/badge.svg)](https://codecov.io/gh/ropensci/mapr) 16 | [![rstudio mirror downloads](https://cranlogs.r-pkg.org/badges/mapr?color=FAB657)](https://github.com/r-hub/cranlogs.app) 17 | [![cran version](https://www.r-pkg.org/badges/version/mapr)](https://cran.r-project.org/package=mapr) 18 | 19 | 20 | Helper for making maps of species occurrence data, including for: 21 | 22 | * spocc (https://github.com/ropensci/spocc) 23 | * rgbif (https://github.com/ropensci/rgbif) 24 | * and some `sp` classes 25 | 26 | This package has utilities for making maps with: 27 | 28 | * base R 29 | * ggplot2 30 | * Leaflet - via `leaflet` pkg 31 | * GitHub Gists - via `gistr` package 32 | 33 | Get started with the docs: https://docs.ropensci.org/mapr/ 34 | 35 | ## Installation 36 | 37 | Install `mapr` 38 | 39 | ```{r eval=FALSE} 40 | install.packages("mapr") 41 | ``` 42 | 43 | Or the development version from GitHub 44 | 45 | ```{r eval=FALSE} 46 | remotes::install_github("ropensci/mapr") 47 | ``` 48 | 49 | ```{r} 50 | library("mapr") 51 | library("spocc") 52 | ``` 53 | 54 | ## Meta 55 | 56 | * Please [report any issues or bugs](https://github.com/ropensci/mapr/issues). 57 | * License: MIT 58 | * Get citation information for `mapr` in R doing `citation(package = 'mapr')` 59 | * Please note that this package is released with a [Contributor Code of Conduct](https://ropensci.org/code-of-conduct/). By contributing to this project, you agree to abide by its terms. 60 | 61 | [![ropensci_footer](https://ropensci.org/public_images/github_footer.png)](https://ropensci.org) 62 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | [![Project Status: Abandoned](https://www.repostatus.org/badges/latest/abandoned.svg)](https://www.repostatus.org/#abandoned) 2 | 3 | This package has been archived. The former README is now in [README-not](README-not.md).< 4 | -------------------------------------------------------------------------------- /codemeta.json: -------------------------------------------------------------------------------- 1 | { 2 | "@context": [ 3 | "https://doi.org/doi:10.5063/schema/codemeta-2.0", 4 | "http://schema.org" 5 | ], 6 | "@type": "SoftwareSourceCode", 7 | "identifier": "mapr", 8 | "description": "Utilities for visualizing species occurrence data. Includes\n functions to visualize occurrence data from 'spocc', 'rgbif',\n and other packages. Mapping options included for base R plots, 'ggplot2',\n 'ggmap', 'leaflet' and 'GitHub' 'gists'.", 9 | "name": "mapr: Visualize Species Occurrence Data", 10 | "codeRepository": "https://github.com/ropensci/mapr", 11 | "issueTracker": "https://github.com/ropensci/mapr/issues", 12 | "license": "https://spdx.org/licenses/MIT", 13 | "version": "0.5.0", 14 | "programmingLanguage": { 15 | "@type": "ComputerLanguage", 16 | "name": "R", 17 | "url": "https://r-project.org" 18 | }, 19 | "runtimePlatform": "R version 4.0.2 Patched (2020-06-30 r78761)", 20 | "provider": { 21 | "@id": "https://cran.r-project.org", 22 | "@type": "Organization", 23 | "name": "Central R Archive Network (CRAN)", 24 | "url": "https://cran.r-project.org" 25 | }, 26 | "author": [ 27 | { 28 | "@type": "Person", 29 | "givenName": "Scott", 30 | "familyName": "Chamberlain", 31 | "email": "myrmecocystus@gmail.com", 32 | "@id": "https://orcid.org/0000-0003-1444-9135" 33 | } 34 | ], 35 | "maintainer": [ 36 | { 37 | "@type": "Person", 38 | "givenName": "Scott", 39 | "familyName": "Chamberlain", 40 | "email": "myrmecocystus@gmail.com", 41 | "@id": "https://orcid.org/0000-0003-1444-9135" 42 | } 43 | ], 44 | "softwareSuggestions": [ 45 | { 46 | "@type": "SoftwareApplication", 47 | "identifier": "testthat", 48 | "name": "testthat", 49 | "provider": { 50 | "@id": "https://cran.r-project.org", 51 | "@type": "Organization", 52 | "name": "Comprehensive R Archive Network (CRAN)", 53 | "url": "https://cran.r-project.org" 54 | }, 55 | "sameAs": "https://CRAN.R-project.org/package=testthat" 56 | }, 57 | { 58 | "@type": "SoftwareApplication", 59 | "identifier": "knitr", 60 | "name": "knitr", 61 | "provider": { 62 | "@id": "https://cran.r-project.org", 63 | "@type": "Organization", 64 | "name": "Comprehensive R Archive Network (CRAN)", 65 | "url": "https://cran.r-project.org" 66 | }, 67 | "sameAs": "https://CRAN.R-project.org/package=knitr" 68 | }, 69 | { 70 | "@type": "SoftwareApplication", 71 | "identifier": "taxize", 72 | "name": "taxize", 73 | "provider": { 74 | "@id": "https://cran.r-project.org", 75 | "@type": "Organization", 76 | "name": "Comprehensive R Archive Network (CRAN)", 77 | "url": "https://cran.r-project.org" 78 | }, 79 | "sameAs": "https://CRAN.R-project.org/package=taxize" 80 | }, 81 | { 82 | "@type": "SoftwareApplication", 83 | "identifier": "maptools", 84 | "name": "maptools", 85 | "provider": { 86 | "@id": "https://cran.r-project.org", 87 | "@type": "Organization", 88 | "name": "Comprehensive R Archive Network (CRAN)", 89 | "url": "https://cran.r-project.org" 90 | }, 91 | "sameAs": "https://CRAN.R-project.org/package=maptools" 92 | }, 93 | { 94 | "@type": "SoftwareApplication", 95 | "identifier": "rgbif", 96 | "name": "rgbif", 97 | "provider": { 98 | "@id": "https://cran.r-project.org", 99 | "@type": "Organization", 100 | "name": "Comprehensive R Archive Network (CRAN)", 101 | "url": "https://cran.r-project.org" 102 | }, 103 | "sameAs": "https://CRAN.R-project.org/package=rgbif" 104 | } 105 | ], 106 | "softwareRequirements": [ 107 | { 108 | "@type": "SoftwareApplication", 109 | "identifier": "ggplot2", 110 | "name": "ggplot2", 111 | "provider": { 112 | "@id": "https://cran.r-project.org", 113 | "@type": "Organization", 114 | "name": "Comprehensive R Archive Network (CRAN)", 115 | "url": "https://cran.r-project.org" 116 | }, 117 | "sameAs": "https://CRAN.R-project.org/package=ggplot2" 118 | }, 119 | { 120 | "@type": "SoftwareApplication", 121 | "identifier": "leaflet", 122 | "name": "leaflet", 123 | "provider": { 124 | "@id": "https://cran.r-project.org", 125 | "@type": "Organization", 126 | "name": "Comprehensive R Archive Network (CRAN)", 127 | "url": "https://cran.r-project.org" 128 | }, 129 | "sameAs": "https://CRAN.R-project.org/package=leaflet" 130 | }, 131 | { 132 | "@type": "SoftwareApplication", 133 | "identifier": "spocc", 134 | "name": "spocc", 135 | "version": ">= 0.6.0", 136 | "provider": { 137 | "@id": "https://cran.r-project.org", 138 | "@type": "Organization", 139 | "name": "Comprehensive R Archive Network (CRAN)", 140 | "url": "https://cran.r-project.org" 141 | }, 142 | "sameAs": "https://CRAN.R-project.org/package=spocc" 143 | }, 144 | { 145 | "@type": "SoftwareApplication", 146 | "identifier": "sp", 147 | "name": "sp", 148 | "provider": { 149 | "@id": "https://cran.r-project.org", 150 | "@type": "Organization", 151 | "name": "Comprehensive R Archive Network (CRAN)", 152 | "url": "https://cran.r-project.org" 153 | }, 154 | "sameAs": "https://CRAN.R-project.org/package=sp" 155 | }, 156 | { 157 | "@type": "SoftwareApplication", 158 | "identifier": "maps", 159 | "name": "maps", 160 | "provider": { 161 | "@id": "https://cran.r-project.org", 162 | "@type": "Organization", 163 | "name": "Comprehensive R Archive Network (CRAN)", 164 | "url": "https://cran.r-project.org" 165 | }, 166 | "sameAs": "https://CRAN.R-project.org/package=maps" 167 | }, 168 | { 169 | "@type": "SoftwareApplication", 170 | "identifier": "RColorBrewer", 171 | "name": "RColorBrewer", 172 | "provider": { 173 | "@id": "https://cran.r-project.org", 174 | "@type": "Organization", 175 | "name": "Comprehensive R Archive Network (CRAN)", 176 | "url": "https://cran.r-project.org" 177 | }, 178 | "sameAs": "https://CRAN.R-project.org/package=RColorBrewer" 179 | }, 180 | { 181 | "@type": "SoftwareApplication", 182 | "identifier": "jsonlite", 183 | "name": "jsonlite", 184 | "provider": { 185 | "@id": "https://cran.r-project.org", 186 | "@type": "Organization", 187 | "name": "Comprehensive R Archive Network (CRAN)", 188 | "url": "https://cran.r-project.org" 189 | }, 190 | "sameAs": "https://CRAN.R-project.org/package=jsonlite" 191 | }, 192 | { 193 | "@type": "SoftwareApplication", 194 | "identifier": "gistr", 195 | "name": "gistr", 196 | "provider": { 197 | "@id": "https://cran.r-project.org", 198 | "@type": "Organization", 199 | "name": "Comprehensive R Archive Network (CRAN)", 200 | "url": "https://cran.r-project.org" 201 | }, 202 | "sameAs": "https://CRAN.R-project.org/package=gistr" 203 | }, 204 | { 205 | "@type": "SoftwareApplication", 206 | "identifier": "data.table", 207 | "name": "data.table", 208 | "provider": { 209 | "@id": "https://cran.r-project.org", 210 | "@type": "Organization", 211 | "name": "Comprehensive R Archive Network (CRAN)", 212 | "url": "https://cran.r-project.org" 213 | }, 214 | "sameAs": "https://CRAN.R-project.org/package=data.table" 215 | } 216 | ], 217 | "applicationCategory": "Geospatial", 218 | "isPartOf": "https://ropensci.org", 219 | "keywords": ["maps", "mapping", "ggplot", "leaflet", "interactive", "biodiversity", "occurrences", "rstats", "ggplot2", "map", "r", "spocc", "r-package"], 220 | "contIntegration": "https://codecov.io/gh/ropensci/mapr", 221 | "releaseNotes": "https://github.com/ropensci/mapr/blob/master/NEWS.md", 222 | "readme": "https://github.com/ropensci/mapr/blob/master/README.md", 223 | "fileSize": "588.972KB", 224 | "relatedLink": "https://docs.ropensci.org/mapr", 225 | "contributor": {}, 226 | "copyrightHolder": {}, 227 | "funder": {} 228 | } 229 | -------------------------------------------------------------------------------- /cran-comments.md: -------------------------------------------------------------------------------- 1 | ## Test environments 2 | 3 | * local OS X install, R 4.0.3 RC 4 | * ubuntu 16.04 (on travis-ci), R 4.0.3 5 | * win-builder (devel and release) 6 | 7 | ## R CMD check results 8 | 9 | 0 errors | 0 warnings | 0 notes 10 | 11 | ## Reverse dependencies 12 | 13 | There are no reverse dependencies. 14 | 15 | --- 16 | 17 | This version includes a fix to the vignette that was causing a warning on two CRAN platforms. 18 | 19 | Thanks! 20 | Scott Chamberlain 21 | -------------------------------------------------------------------------------- /data/gbif_eg1.rda: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ropensci-archive/mapr/efc0b34fc01d46769778db46c23e689538f51668/data/gbif_eg1.rda -------------------------------------------------------------------------------- /data/occdat_eg1.rda: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ropensci-archive/mapr/efc0b34fc01d46769778db46c23e689538f51668/data/occdat_eg1.rda -------------------------------------------------------------------------------- /inst/ignore/as.occkey.R: -------------------------------------------------------------------------------- 1 | #' Coerce occurrence keys to occkey objects 2 | #' 3 | #' @param obj The output from \code{\link{occ}} call. 4 | #' @examples \dontrun{ 5 | #' spnames <- c('Accipiter striatus', 'Setophaga caerulescens', 'Carduelis tristis') 6 | #' out <- occ(query=spnames, from=c('gbif','ebird'), gbifopts=list(hasCoordinate=TRUE), limit=4) 7 | #' res <- occ2df(out) 8 | #' as.occkey(res) 9 | #' } 10 | as.occkey <- function(x) UseMethod("as.occkey") 11 | 12 | #' @export 13 | #' @rdname as.occkey 14 | as.occkey.occkey <- function(x) x 15 | 16 | #' @export 17 | #' @rdname as.occkey 18 | as.occkey.occdat <- function(x) { 19 | 20 | } 21 | 22 | #' @export 23 | #' @rdname as.occkey 24 | as.occkey.data.frame <- function(x) { 25 | 26 | } 27 | 28 | #' @export 29 | #' @rdname as.occkey 30 | as.occkey.numeric <- function(x) { 31 | 32 | } 33 | 34 | #' @export 35 | #' @rdname as.occkey 36 | as.occkey.character <- function(x) { 37 | 38 | } 39 | 40 | #' @export 41 | #' @rdname as.occkey 42 | as.occkey.list <- function(x) { 43 | 44 | } 45 | 46 | to_occkey <- function(x){ 47 | 48 | } 49 | -------------------------------------------------------------------------------- /inst/ignore/clean_spocc_workingon.R: -------------------------------------------------------------------------------- 1 | #' Clean spocc data 2 | #' 3 | #' @export 4 | #' @importFrom sp zerodist2 proj4string over coordinates<- 5 | #' @param input An object of class occdat 6 | #' @param country (logical) Attempt to clean based on country. Ignored for now. 7 | #' @param country_which (character) One of include, xxx. Ignored for now. 8 | #' @param shppath (character) Path to shape file to check against. Ignored for now. 9 | #' @param habitat (character) Attempt to clean based on habitat. Ignored for now. 10 | #' @param provider_duplicates (logical) Whether to remove duplicates from the same provider in 11 | #' separate sources. Ignored for now. 12 | #' @details We'll continue to add options for cleaning data, but for now, this function: 13 | #' 14 | #' \itemize{ 15 | #' \item Removes impossible values of latitude and longitude 16 | #' \item Removes any NA values of latitude and longitude 17 | #' \item Removes points at 0,0 - these points are likely wrong 18 | #' } 19 | #' 20 | #' Some examples below don't actually work yet, but will soon. 21 | #' @return Returns an object of class occdat+occlean. See attributes of the return object for 22 | #' details on cleaning results. 23 | #' @examples \dontrun{ 24 | #' res <- occ(query = c('Ursus','Accipiter','Rubus'), from = 'bison', limit=10) 25 | #' class(res) 26 | #' res_cleaned <- clean_spocc(input=res) 27 | #' class(res_cleaned) # now with classes occdat and occclean 28 | #' 29 | #' #### THESE AREN'T WORKING... 30 | #' # Country cleaning 31 | #' res <- occ(query = 'Ursus americanus', from = 'gbif', limit=500, 32 | #' gbifopts = list(hasCoordinate=TRUE)) 33 | #' res$gbif 34 | #' plot(res) 35 | #' 36 | #' res <- occ(query = 'Ursus americanus', from = 'gbif', limit=1200, 37 | #' gbifopts = list(hasCoordinate=TRUE)) 38 | #' plot(res) 39 | #' res2 <- clean_spocc(res, country = "Mexico") 40 | #' plot(res2) 41 | #' 42 | #' # Clean provider duplicates 43 | #' res <- occ(query = 'Ursus americanus', from = c('gbif','inat'), limit=300) 44 | #' plot(res) 45 | #' res2 <- clean_spocc(input=res, provider_duplicates = TRUE) 46 | #' } 47 | 48 | clean_spocc <- function(input, country=NULL, country_which='include', shppath=NULL, habitat=NULL, 49 | provider_duplicates=FALSE) 50 | { 51 | stopifnot(is(input, "occdat") | is(input, "data.frame")) 52 | 53 | clean <- function(x){ 54 | if(all(sapply(x$data, nrow) < 1)){ 55 | x 56 | } else { 57 | clean_eachsp <- function(dat, what){ 58 | # dat <- replacelatlongcols(y, what) 59 | 60 | # Make lat/long data numeric 61 | dat$latitude <- as.numeric(as.character(dat$latitude)) 62 | dat$longitude <- as.numeric(as.character(dat$longitude)) 63 | 64 | # Remove points that are not physically possible 65 | notcomplete <- dat[!complete.cases(dat$latitude, dat$longitude), ] 66 | dat <- dat[complete.cases(dat$latitude, dat$longitude), ] 67 | notpossible <- dat[!abs(dat$latitude) <= 90 | !abs(dat$longitude) <= 180, ] 68 | dat <- dat[abs(dat$latitude) <= 90, ] 69 | dat <- dat[abs(dat$longitude) <= 180, ] 70 | 71 | # Remove points at lat 0 & long 0, these are very likely wrong 72 | dat <- dat[ !dat$latitude == 0 & !dat$longitude == 0, ] 73 | 74 | if(!is.null(habitat)){ 75 | # clean_habitat() 76 | # get polygons for terrestrial vs. marine vs. freshwater 77 | # calculate whether polygon encompasses points 78 | # remove points not in polygon 79 | } 80 | 81 | if(!is.null(country)){ 82 | # dat <- clean_country(data=dat, country=country, which=country_which, shppath=shppath) 83 | } 84 | 85 | # dat <- replacelatlongcols(dat, what, reverse = TRUE) 86 | 87 | list(nc = notcomplete, np = notpossible, d = dat) 88 | } 89 | 90 | dat_eachsp <- lapply(x$data, clean_eachsp, what=x$meta$source) 91 | 92 | nc <- lapply(dat_eachsp, function(x) ifnone(x$nc)) 93 | np <- lapply(dat_eachsp, function(x) ifnone(x$np)) 94 | datdat <- lapply(dat_eachsp, "[[", "d") 95 | 96 | # assign to a class and assign attributes 97 | x$meta <- c(x$meta, removed_incomplete_cases = list(nc), removed_impossible = list(np)) 98 | x$data <- datdat 99 | x 100 | } 101 | } 102 | 103 | output <- lapply(input, clean) 104 | 105 | # clean provider duplicates, takes in occdat object 106 | if(provider_duplicates){ 107 | # output <- clean_provider_duplicates(data=output) 108 | } 109 | 110 | class(output) <- c("occdat","occclean") 111 | return( output ) 112 | } 113 | 114 | ifnone <- function(x) if(nrow(x)==0){ NA } else { x } 115 | 116 | # replacelatlongcols <- function(w, z, reverse=FALSE){ 117 | # cols <- switch(z, 118 | # gbif = c('decimalLatitude','decimalLongitude'), 119 | # bison = c('decimalLongitude','decimalLatitude'), 120 | # inat = c('Latitude','Longitude'), 121 | # ebird = c('lng','lat'), 122 | # ecoengine = c('longitude','latitude'), 123 | # antweb = c('decimal_longitude','decimal_latitude')) 124 | # if(reverse){ 125 | # names(w)[ names(w) %in% c('latitude','longitude') ] <- cols 126 | # } else { 127 | # names(w)[ names(w) %in% cols ] <- c('latitude','longitude') 128 | # } 129 | # 130 | # return( w ) 131 | # } 132 | 133 | clean_country <- function(data, country=NULL, which='include', shppath=NULL) 134 | { 135 | shppath <- if(is.null(shppath)) "~/github/ropensci/shapefiles/ne_10m_admin_0_countries/" else shppath 136 | shppath <- path.expand(shppath) 137 | layer <- rgdal::ogrListLayers(shppath) 138 | shp <- rgdal::readOGR(shppath, layer = layer) 139 | country_shp <- switch(which, 140 | include = shp[shp@data$name %in% country,], 141 | exclude = shp[!shp@data$name %in% country,] 142 | ) 143 | 144 | sp::coordinates(data) <- ~longitude+latitude 145 | sp::proj4string(data) <- sp::CRS('+proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0') 146 | 147 | ss <- sp::over(data, country_shp) 148 | tmp <- data[!apply(ss, 1, function(b) is.na(b['scalerank'])), ] 149 | tmp <- as(tmp, "data.frame") 150 | return( tmp ) 151 | } 152 | 153 | clean_provider_duplicates <- function(data){ 154 | # if only 1 provider, pass 155 | # if no GBIF, pass 156 | # if GBIF and another provider, keep going... 157 | # 1) look for fields that have provider info in GBIF data, e.g., inaturalist in GBIF data could be 158 | # a problem if inat also used 159 | # 2) Match lat/long pairs against one another iteratively? Would take a while with large datasets 160 | records <- vapply(data, function(x) NROW(x$data[[1]]), numeric(1)) 161 | provs <- names(records[records > 0]) 162 | if(!length(provs) > 1){ ret <- NULL } else { 163 | if(!'gbif' %in% provs){ ret <- NULL } else { 164 | d1 <- data[[provs[1]]]$data[[1]] 165 | d2 <- data[[provs[2]]]$data[[1]] 166 | sp::coordinates(d1) <- ~decimalLongitude+decimalLatitude 167 | sp::coordinates(d2) <- ~Longitude+Latitude 168 | sp::zerodist2(d1, d2) 169 | } 170 | } 171 | } 172 | 173 | clean_habitat <- function(data){ 174 | # library(maptools) 175 | res <- ggplot2::map_data("world") 176 | # ogrListLayers("/Users/sacmac/Downloads/ne_110m_land") 177 | # land <- readOGR("/Users/sacmac/Downloads/ne_110m_land/", layer = 'ne_110m_land') 178 | land <- rgdal::readOGR("/Users/sacmac/Downloads/ne_10m_land/", layer = 'ne_10m_land') 179 | 180 | data <- na.omit(data) 181 | sp::coordinates(data) <- ~longitude+latitude 182 | sp::proj4string(data) <- sp::CRS('+proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0') 183 | 184 | sp::over(data, land) 185 | } 186 | -------------------------------------------------------------------------------- /inst/ignore/datasets.R: -------------------------------------------------------------------------------- 1 | ## mapr datasets preparation script 2 | 3 | # gbif_eg1 4 | library(rgbif) 5 | foo <- occ_search(scientificName = "Puma concolor", limit = 100) 6 | z <- foo$data 7 | for (i in seq_along(z)) { 8 | if (is.character(z[[i]])) { 9 | cat(names(z)[i], sep = "\n") 10 | tools::showNonASCII(na.omit(z[[i]])) 11 | } 12 | } 13 | foo$data$rightsHolder <- stringi::stri_escape_unicode(foo$data$rightsHolder) 14 | foo$data$stateProvince <- stringi::stri_escape_unicode(foo$data$stateProvince) 15 | foo$data$verbatimLocality <- stringi::stri_escape_unicode(foo$data$verbatimLocality) 16 | foo$data$recordedBy <- stringi::stri_escape_unicode(foo$data$recordedBy) 17 | foo$data$rights <- stringi::stri_escape_unicode(foo$data$rights) 18 | foo$data$identifiedBy <- stringi::stri_escape_unicode(foo$data$identifiedBy) 19 | foo$data$occurrenceRemarks <- stringi::stri_escape_unicode(foo$data$occurrenceRemarks) 20 | z <- foo$data 21 | gbif_eg1 <- foo$data 22 | gbif_eg1 <- dplyr::rename(gbif_eg1, latitude = decimalLatitude, longitude = decimalLongitude) 23 | save(gbif_eg1, file = "data/gbif_eg1.rda", version = 2) 24 | 25 | 26 | # occdat_eg1 27 | library(spocc) 28 | res <- occ(query='Accipiter striatus', from='gbif', limit=25, has_coords=TRUE) 29 | res$gbif$data[[1]]$rights <- stringi::stri_escape_unicode(res$gbif$data[[1]]$rights) 30 | tools::showNonASCII(res$gbif$data[[1]]$rights) 31 | occdat_eg1 <- res 32 | save(occdat_eg1, file = "data/occdat_eg1.rda", version = 2) 33 | -------------------------------------------------------------------------------- /inst/ignore/gbifxmlfxn.R: -------------------------------------------------------------------------------- 1 | #' Code based on the `gbifxmlToDataFrame` function from dismo package 2 | #' (http://cran.r-project.org/web/packages/dismo/index.html), 3 | #' by Robert Hijmans, 2012-05-31, License: GPL v3 4 | #' @import XML 5 | #' @param doc A parsed XML document. 6 | #' @param format Format to use. 7 | #' @export 8 | #' @keywords internal 9 | spocc_gbifxmlToDataFrame <- function(doc, format) { 10 | nodes <- getNodeSet(doc, "//to:TaxonOccurrence") 11 | if (length(nodes) == 0) 12 | return(data.frame()) 13 | if (!is.null(format) & format == "darwin") { 14 | varNames <- c("occurrenceID", "country", "stateProvince", "county", "locality", 15 | "decimalLatitude", "decimalLongitude", "coordinateUncertaintyInMeters", 16 | "maximumElevationInMeters", "minimumElevationInMeters", "maximumDepthInMeters", 17 | "minimumDepthInMeters", "institutionCode", "collectionCode", "catalogNumber", 18 | "basisOfRecordString", "collector", "earliestDateCollected", "latestDateCollected", 19 | "gbifNotes") 20 | } else { 21 | varNames <- c("occurrenceID", "country", "decimalLatitude", "decimalLongitude", 22 | "catalogNumber", "earliestDateCollected", "latestDateCollected") 23 | } 24 | dims <- c(length(nodes), length(varNames)) 25 | ans <- as.data.frame(replicate(dims[2], rep(as.character(NA), dims[1]), simplify = FALSE), 26 | stringsAsFactors = FALSE) 27 | names(ans) <- varNames 28 | for (i in seq(length = dims[1])) { 29 | ans[i, 1] <- xmlAttrs(nodes[[i]])[["gbifKey"]] 30 | ans[i, -1] <- xmlSApply(nodes[[i]], xmlValue)[varNames[-1]] 31 | } 32 | nodes <- getNodeSet(doc, "//to:Identification") 33 | varNames <- c("taxonName") 34 | dims <- c(length(nodes), length(varNames)) 35 | tax <- as.data.frame(replicate(dims[2], rep(as.character(NA), dims[1]), simplify = FALSE), 36 | stringsAsFactors = FALSE) 37 | names(tax) <- varNames 38 | for (i in seq(length = dims[1])) { 39 | tax[i, ] <- xmlSApply(nodes[[i]], xmlValue)[varNames] 40 | } 41 | cbind(tax, ans) 42 | } 43 | -------------------------------------------------------------------------------- /inst/ignore/latlongstuff.R: -------------------------------------------------------------------------------- 1 | ### Calculate a polygon around a point with a given distance radius 2 | #### make sure to convert this to UTM first with WGS84 datum 3 | 4 | library(rgeos) 5 | foo <- function(lat, lon, write=FALSE, ...) 6 | { 7 | latlon <- paste(lon, lat) 8 | # poly <- readWKT(sprintf("POLYGON((%s,%s,%s,%s))", latlon,latlon,latlon,latlon)) 9 | poly <- readWKT(sprintf("POLYGON((%s,%s,%s,%s))", latlon,latlon,latlon,latlon)) 10 | g <- gBuffer(poly, ...) 11 | if(write) 12 | writeWKT(g) 13 | else 14 | g 15 | } 16 | 17 | (p <- foo(lat=33.95, lon=-118.40, width=0.4)) 18 | plot(p) 19 | 20 | # this was Barry's solution 21 | 22 | require(sp) 23 | require(rgeos) 24 | require(rgdal) 25 | d <- data.frame(lat=c(33.95,34.95,34.70), lon=c(-118.40,-118.22,-118.43),ID=1:3) 26 | coordinates(d)=~lon+lat 27 | proj4string(d)=CRS("+init=epsg:4326") 28 | 29 | buf <- gBuffer(d,width=0.2) 30 | writeWKT(buf[1]) 31 | plot(buf) 32 | 33 | # Bounding box to WKT 34 | 35 | #' Converts a bounding box to a Well Known Text polygon 36 | #' 37 | #' @param minx Minimum x value, or the most western longitude 38 | #' @param miny Minimum y value, or the most southern latitude 39 | #' @param maxx Maximum x value, or the most eastern longitude 40 | #' @param maxy Maximum y value, or the most northern latitude 41 | #' @param all A vector of length 4, with the elements: minx, miny, maxx, maxy 42 | #' @return An object of class charactere, a Well Known Text string of the form 43 | #' 'POLYGON((minx miny, maxx miny, maxx maxy, minx maxy, minx miny))' 44 | #' @examples 45 | #' # Pass in a vector of length 4 with all values 46 | #' mm <- bbox2wkt(bbox=c(38.4,-125.0,40.9,-121.8)) 47 | #' plot(readWKT(mm)) 48 | #' 49 | #' # Or pass in each vdalue separately 50 | #' mm <- bbox2wkt(minx=38.4, miny=-125.0, maxx=40.9, maxy=-121.8) 51 | #' plot(readWKT(mm)) 52 | 53 | bbox2wkt <- function(minx=NA, miny=NA, maxx=NA, maxy=NA, bbox=NULL){ 54 | if(is.null(bbox)) 55 | bbox <- c(minx, miny, maxx, maxy) 56 | 57 | assert_that(length(bbox)==4) #check for 4 digits 58 | assert_that(noNA(bbox)) #check for NAs 59 | assert_that(is.numeric(as.numeric(bbox))) #check for numeric-ness 60 | paste('POLYGON((', 61 | sprintf('%s %s',bbox[1],bbox[2]), ',', sprintf('%s %s',bbox[3],bbox[2]), ',', 62 | sprintf('%s %s',bbox[3],bbox[4]), ',', sprintf('%s %s',bbox[1],bbox[4]), ',', 63 | sprintf('%s %s',bbox[1],bbox[2]), 64 | '))', sep="") 65 | } 66 | 67 | # assert_that(length(c(minx, miny, maxx, maxy))==4) #check for 4 digits 68 | # assert_that(noNA(c(minx, miny, maxx, maxy))) #check for NAs 69 | # assert_that(is.numeric(as.numeric(all))) #check for numeric-ness 70 | # paste('POLYGON((', 71 | # sprintf('%s %s',minx,miny), ',', sprintf('%s %s',maxx,miny), ',', 72 | # sprintf('%s %s',maxx,maxy), ',', sprintf('%s %s',minx,maxy), ',', 73 | # sprintf('%s %s',minx,miny), 74 | # '))', sep="") 75 | 76 | 77 | wkt="POLYGON((38.4 -125,40.9 -125,40.9 -121.8,38.4 -121.8,38.4 -125))" 78 | wkt2bbox <- function(wkt=NULL){ 79 | assert_that(!is.null(wkt)) 80 | tmp <- bbox(readWKT(wkt)) 81 | as.vector(tmp) 82 | } 83 | 84 | 85 | ##### 86 | # d <- cbind(c(33.95,34.95,34.70), c(-118.40,-118.22,-118.43)) 87 | # coordinates(d)=~lon+lat 88 | # spTransform(d, CRS("+proj=utm +zone=11 +datum=WGS84")) 89 | # project(d, "+proj=utm +zone=11 +datum=WGS84") 90 | 91 | # library(rgdal) 92 | # xy <- cbind(c(118, 119), c(10, 50)) 93 | # project(xy, "+proj=utm +zone=24 ellps=WGS84") 94 | # spTransform(xy, CRS("+proj=utm +zone=51 ellps=WGS84")) 95 | 96 | ### Get UTM zone for a set of lat/long coordinates 97 | #### for western hemisphere only 98 | long2UTM <- function(long) { 99 | (floor((long + 180)/6) %% 60) + 1 100 | } 101 | long2UTM(4) 102 | 103 | #### for globe 104 | long2utm <- function(lon, lat) { 105 | if(56 <= lat & lat < 64){ 106 | if(0 <= lon & lon < 3){ 31 } else 107 | if(3 <= lon & lon < 12) { 32 } else { NULL } 108 | } else 109 | if(72 <= lat) { 110 | if(0 <= lon & lon < 9){ 31 } else 111 | if(9 <= lon & lon < 21) { 33 } else 112 | if(21 <= lon & lon < 33) { 35 } else 113 | if(33 <= lon & lon < 42) { 37 } else { NULL } 114 | } 115 | (floor((lon + 180)/6) %% 60) + 1 116 | } 117 | long2utm(-60, 65) 118 | # 119 | 120 | 121 | # library(rgdal) 122 | 123 | ## Create an example SpatialPoints object 124 | # pts <- SpatialPoints(cbind(-120:-121, 39:40), 125 | # proj4string = CRS("+proj=longlat +datum=NAD27")) 126 | 127 | ## Construct a proper proj4string 128 | # UTM11N <- "+proj=utm +zone=11 +datum=NAD83 +units=m +no_defs" 129 | # UTM11N <- paste(UTM11N, "+ellps=GRS80 +towgs84=0,0,0") 130 | # UTM11N <- CRS(UTM11N) 131 | 132 | ## Project your points 133 | # ptsUTM <- spTransform(pts, UTM11N) 134 | 135 | 136 | ### Convert data.frame from spocc of lat/long coord's to UTM coords 137 | 138 | library(spocc) 139 | dat <- occ(query='Accipiter striatus', from='gbif') 140 | dat <- na.omit(dat@gbif@data) 141 | coordinates(dat)=~longitude+latitude 142 | proj4string(dat) = CRS("+init=epsg:4326") 143 | spTransform(dat, CRS("+proj=utm +zone=11 +datum=WGS84 +units=m +no_defs +towgs84=0,0,0")) -------------------------------------------------------------------------------- /inst/ignore/leaflet.R: -------------------------------------------------------------------------------- 1 | #' Make an interactive map with Leaflet 2 | #' 3 | #' @export 4 | #' @import leaflet 5 | #' 6 | #' @param data A data.frame, with any number of columns, but with at least the 7 | #' following: name (the taxonomic name), latitude (in dec. deg.), longitude 8 | #' (in dec. deg.) 9 | #' @param ... Further arguments passed on to \code{\link[leaflet]{xxx}} 10 | #' @examples \dontrun{ 11 | #' library("spocc") 12 | #' spp <- c('Danaus plexippus', 'Accipiter striatus', 'Pinus contorta') 13 | #' dat <- occ(spp, from = c('gbif', 'ecoengine'), limit=30, has_coords = TRUE) 14 | #' dat <- fixnames(dat, "query") 15 | #' (x <- map_leaflet(dat)) 16 | #' 17 | #' # add a convex hull 18 | #' x %>% hull() 19 | #' } 20 | map_leaflet <- function(x, ...) { 21 | stopifnot(is(x, "occdatind") | is(x, "occdat")) 22 | x <- if (is(x, "occdatind")) { 23 | do.call(rbind, x$data) 24 | } else { 25 | occ2df(x) 26 | } 27 | # spplist <- as.character(unique(x$name)) 28 | # datgeojson <- spocc_stylegeojson(input = x, var = "name", ...) 29 | leaflet::leaflet(x) %>% 30 | leaflet::addTiles() %>% 31 | leaflet::addMarkers(~longitude, ~latitude) 32 | } 33 | -------------------------------------------------------------------------------- /inst/ignore/map_shiny.R: -------------------------------------------------------------------------------- 1 | #' Shiny visualization of species occurrences 2 | #' 3 | #' @export 4 | #' 5 | #' @template args 6 | #' @param color Default color of your points. 7 | #' @param size point size, Default: 13 8 | #' @param ... Ignored 9 | #' @return Opens a shiny app in your default browser 10 | #' @examples \dontrun{ 11 | #' library("spocc") 12 | #' (x <- occ(query='Accipiter striatus', from='gbif', limit=50, 13 | #' has_coords=TRUE)) 14 | #' readr::write_csv(spocc::occ2df(x), path = "inst/shinysingle/dat.csv") 15 | #' map_shiny(x) 16 | #' 17 | map_shiny( 18 | query = 'Accipiter striatus', from = 'gbif', limit = 50, has_coords = TRUE 19 | ) 20 | #' } 21 | # map_shiny <- function(x, lon = 'longitude', lat = 'latitude', color = NULL, 22 | # size = 13, ...) { 23 | #x <- dat_cleaner(spocc::occ2df(x), lon, lat) 24 | map_shiny <- function(...) { 25 | (dots <- lazyeval::lazy_dots(...)) 26 | # message("Hit to stop") 27 | # shiny::runApp(system.file("shinysingle/app.R", package = "mapr")) 28 | } 29 | -------------------------------------------------------------------------------- /inst/ignore/occdat_vignette.Rmd: -------------------------------------------------------------------------------- 1 | 5 | 6 | rgbif vignette - Seach and retrieve data from the Global Biodiverity Information Facilty (GBIF) 7 | ====== 8 | 9 | ### About the package 10 | 11 | `rgbif` is an R package to search and retrieve data from the Global Biodiverity Information Facilty (GBIF). `rgbif` wraps R code around the [GBIF API][gbifapi] to allow you to talk to the BISON database from R. 12 | 13 | ******************** 14 | 15 | #### Install rgbif 16 | 17 | ```{r install, comment=NA, warning=FALSE} 18 | # install.packages("devtools"); library(devtools); install_github("rbison", "ropensci") 19 | library(rgbif); library(XML); library(RCurl); library(plyr); library(ggplot2); library(maps) 20 | ``` 21 | 22 | ******************** 23 | 24 | #### Get a list of the data networks in GBIF - and you can use the networkkey number to seach for occurrences for the specific provider in other functions 25 | 26 | ```{r networks, comment=NA, warning=FALSE} 27 | # Test the function for a few networks 28 | networks(maxresults=5) 29 | 30 | # By name 31 | networks('ORNIS') 32 | ``` 33 | 34 | ******************** 35 | 36 | #### Get a list of the data providers in GBIF - and you can use the dataproviderkey number to seach for occurrences for the specific provider in other functions 37 | 38 | ```{r providers, comment=NA, warning=FALSE} 39 | # Test the function for a few providers 40 | providers(maxresults=5) 41 | 42 | # By data provider name 43 | providers('University of Texas-Austin') 44 | ``` 45 | 46 | ******************** 47 | 48 | #### Get a list of the data resources in GBIF - and you can use the resourcekey number to seach for occurrences for the specific resource in other functions 49 | 50 | ```{r resources, comment=NA, warning=FALSE} 51 | # Test the function for a few resources 52 | resources(maxresults=5) 53 | 54 | # By name 55 | head(resources('Flora')) 56 | ``` 57 | 58 | ******************** 59 | 60 | #### Get number of occurrences for a set of search parameters 61 | ```{r occurrencecount, comment=NA, warning=FALSE} 62 | occurrencecount(scientificname = 'Accipiter erythronemius', coordinatestatus = TRUE) 63 | occurrencecount(scientificname = 'Helianthus annuus', coordinatestatus = TRUE, year=2005, maxlatitude=20) 64 | ``` 65 | 66 | ******************** 67 | 68 | #### Get possible values to be used in taxonomic rank arguments in functions 69 | 70 | ```{r taxrank, comment=NA, warning=FALSE} 71 | taxrank() 72 | ``` 73 | 74 | ******************** 75 | 76 | #### Seach by taxon to retrieve number of records per taxon found in GBIF 77 | ```{r taxoncount, comment=NA, warning=FALSE} 78 | taxoncount(scientificname = 'Puma concolor') 79 | taxoncount(scientificname = 'Helianthus annuus') 80 | taxoncount(rank = 'family') 81 | ``` 82 | 83 | ******************** 84 | 85 | #### Get taxonomic information on a specific taxon or taxa in GBIF by their taxon concept keys 86 | 87 | ```{r taxonget, comment=NA, warning=FALSE} 88 | (out <- taxonsearch(scientificname = 'Puma concolor')) 89 | ``` 90 | 91 | ******************** 92 | 93 | #### Search for taxa in GBIF 94 | 95 | ```{r taxonsearch, comment=NA, warning=FALSE} 96 | taxonsearch(scientificname = 'Puma concolor', rank="species", maxresults=10) 97 | taxonsearch(scientificname = 'Puma concolor', rank="species", dataproviderkey=1) 98 | ``` 99 | 100 | ******************** 101 | 102 | #### Get data for a single occurrence. Note that data is returned as a list, so you have to convert to a data.frame, etc. as you wish 103 | ```{r occurrenceget, comment=NA, warning=FALSE} 104 | occurrenceget(key = 13749100)$dataProvider$dataResources$dataResource$occurrenceRecords$TaxonOccurrence[1:10] 105 | ``` 106 | 107 | ******************** 108 | 109 | ```{r occurrencelist, comment=NA, warning=FALSE} 110 | out <- occurrencelist(scientificname = 'Puma concolor', coordinatestatus=TRUE, maxresults=20) 111 | ``` 112 | 113 | Note that the default object printed from a call to `occurrencelist` is a list that contains: 114 | 115 | + NumberFound: number of occurrences found in search results. 116 | + TaxonNames: Unique list of taxonomic names in search results. 117 | + Coordinates: Min and max latitude and longitude of all occurrences. 118 | + Countries: Countries contained in results set. 119 | 120 | ```{r defaultoccurencelist, comment=NA, warning=FALSE} 121 | out 122 | ``` 123 | 124 | Where do you get data after a call to the `occurrencelist` function? This is where `gbifdata` comes in. By default a call to `gbifdata` prints a minimal data.frame with just rows *name*, *latitude*, and *longitude*. 125 | 126 | ```{r minimaltrue, comment=NA, warning=FALSE} 127 | gbifdata(out) 128 | ``` 129 | 130 | Though you can get more detailed data by calling *minimal=FALSE*. 131 | 132 | ```{r minimalfalse, comment=NA, warning=FALSE} 133 | head( gbifdata(out, minimal=FALSE)[,1:6] ) 134 | ``` 135 | 136 | And you can get all possible data by specifying *format=darwin*. 137 | 138 | ```{r occurrencelistdarwin, comment=NA, warning=FALSE} 139 | out <- occurrencelist(scientificname = 'Puma concolor', coordinatestatus=TRUE, format="darwin", maxresults=20) 140 | head( gbifdata(out, minimal=FALSE)[,1:6] ) 141 | ``` 142 | 143 | ******************** 144 | 145 | #### Maps 146 | 147 | ```{r gbifmap1, comment=NA, warning=FALSE} 148 | splist <- c('Accipiter erythronemius', 'Junco hyemalis', 'Aix sponsa', 'Ceyx fallax', 'Picoides lignarius', 'Campephilus leucopogon') 149 | out <- occurrencelist_many(splist, coordinatestatus = TRUE, maxresults = 20) 150 | gbifmap_list(out) 151 | ``` 152 | 153 | Another example, setting scientificname="*" so we just get any species, and then mapping points only within the state of Texas in the US. 154 | 155 | ```{r gbifmap2, comment=NA, warning=FALSE} 156 | out <- occurrencelist(scientificname="*", minlatitude=30, maxlatitude=35, minlongitude=-100, maxlongitude=-95, coordinatestatus = TRUE, maxresults = 200) 157 | gbifmap_list(input=out, mapdatabase="state", region="texas", geom=geom_jitter, jitter=position_jitter(width = 0.3, height = 0.3)) 158 | ``` 159 | 160 | ******************** 161 | 162 | [gbifapi]: http://data.gbif.org/tutorial/services -------------------------------------------------------------------------------- /inst/shiny/egsmodal.html: -------------------------------------------------------------------------------- 1 | 2 | 3 |
4 | 5 | 6 | -------------------------------------------------------------------------------- /inst/shiny/global.r: -------------------------------------------------------------------------------- 1 | rcharts_prep1 <- function(sppchar, occurrs, datasource){ 2 | require(RColorBrewer) 3 | require(plyr) 4 | species2 <- strsplit(sppchar, ",")[[1]] 5 | 6 | if(datasource=="GBIF"){ 7 | dat <- occ(query=species2, from='gbif', gbifopts=list(hasCoordinate=TRUE, limit=occurrs)) 8 | dat <- occ2df(dat) 9 | # dat <- occtodfspp(dat, 'data') 10 | apply(dat, 1, as.list) 11 | } else if(datasource=="BISON"){ 12 | dat <- occ(query=species2, from='bison', bisonopts=list(count=occurrs)) 13 | dat <- occ2df(dat) 14 | # dat <- occtodfspp(dat, 'data') 15 | apply(dat, 1, as.list) 16 | } else 17 | { 18 | dat <- occ(query=species2, from='inat', inatopts=list(maxresults=occurrs)) 19 | dat <- occ2df(dat) 20 | # dat <- occtodfspp(dat, 'data') 21 | dat <- dat[as.character(dat$name) %in% species2, ] 22 | apply(dat, 1, as.list) 23 | } 24 | } 25 | 26 | get_colors <- function(vec, palette_name){ 27 | num_colours <- length(unique(vec)) 28 | brewer.pal(max(num_colours, 3), palette_name) 29 | } 30 | 31 | rcharts_prep2 <- function(out, palette_name, popup = FALSE){ 32 | require(rgbif) 33 | 34 | # colors 35 | uniq_name_vec <- unique(vapply(out, function(x) x[["name"]], "")) 36 | 37 | # colors 38 | mycolors <- get_colors(uniq_name_vec, palette_name) 39 | if(length(mycolors) > length(uniq_name_vec)) 40 | mycolors <- mycolors[1:length(uniq_name_vec)] 41 | mycolors_df <- data.frame(taxon=uniq_name_vec, color=mycolors) 42 | 43 | # Add fill color for points 44 | out_list2 <- lapply(out, function(x){ 45 | x$fillColor = mycolors_df[as.character(mycolors_df$taxon) %in% x$name, "color"] 46 | x 47 | }) 48 | 49 | # popup 50 | if(popup) 51 | out_list2 <- lapply(out_list2, function(l){ 52 | l$popup = paste(paste("", names(l), ": ", l, "
"), collapse = '\n') 53 | return(l) 54 | }) 55 | out_list2 <- Filter(function(x) !is.na(x$latitude), out_list2) 56 | toGeoJSON(out_list2, lat = 'latitude', lon = 'longitude') 57 | } 58 | 59 | toGeoJSON <- function(list_, lat = 'latitude', lon = 'longitude'){ 60 | x = lapply(list_, function(l){ 61 | if (is.null(l[[lat]]) || is.null(l[[lon]])){ 62 | return(NULL) 63 | } 64 | list( 65 | type = 'Feature', 66 | geometry = list( 67 | type = 'Point', 68 | coordinates = as.numeric(c(l[[lon]], l[[lat]])) 69 | ), 70 | properties = l[!(names(l) %in% c(lat, lon))] 71 | ) 72 | }) 73 | setNames(Filter(function(x) !is.null(x), x), NULL) 74 | } 75 | 76 | gbifmap2 <- function(input_data, map_provider = 'MapQuestOpen.OSM', map_zoom = 2, height = 600, width = 870){ 77 | require(rCharts) 78 | L1 <- Leaflet$new() 79 | L1$tileLayer(provider = map_provider, urlTemplate = NULL) 80 | L1$set(height = height, width = width) 81 | L1$setView(c(30, -73.90), map_zoom) 82 | L1$geoJson(input_data, 83 | onEachFeature = '#! function(feature, layer){ 84 | layer.bindPopup(feature.properties.popup || feature.properties.taxonName) 85 | } !#', 86 | pointToLayer = "#! function(feature, latlng){ 87 | return L.circleMarker(latlng, { 88 | radius: 4, 89 | fillColor: feature.properties.fillColor || 'red', 90 | color: '#000', 91 | weight: 1, 92 | fillOpacity: 0.8 93 | }) 94 | } !#" 95 | ) 96 | L1$fullScreen(TRUE) 97 | return(L1) 98 | } 99 | 100 | get_palette <- function(userselect){ 101 | colours_ <- data.frame( 102 | actual=c("Blues","BuGn","BuPu","GnBu","Greens","Greys","Oranges","OrRd","PuBu", 103 | "PuBuGn","PuRd","Purples","RdPu","Reds","YlGn","YlGnBu","YlOrBr","YlOrRd", 104 | "BrBG","PiYG","PRGn","PuOr","RdBu","RdGy","RdYlBu","RdYlGn","Spectral"), 105 | choices=c("Blues","BlueGreen","BluePurple","GreenBlue","Greens","Greys","Oranges","OrangeRed", 106 | "PurpleBlue","PurpleBlueGreen","PurpleRed","Purples", 107 | "RedPurple","Reds","YellowGreen","YellowGreenBlue","YellowOrangeBrown","YellowOrangeRed", 108 | "BrownToGreen","PinkToGreen","PurpleToGreen","PurpleToOrange","RedToBlue","RedToGrey", 109 | "RedYellowBlue","RedYellowGreen","Spectral")) 110 | as.character(colours_[colours_$choices %in% userselect, "actual"]) 111 | } -------------------------------------------------------------------------------- /inst/shiny/server.r: -------------------------------------------------------------------------------- 1 | require(shiny) 2 | require(leaflet) 3 | 4 | shinyServer(function(input, output) { 5 | 6 | points <- eventReactive(input$recalc, { 7 | cbind(rnorm(40) * 2 + 13, rnorm(40) + 48) 8 | }, ignoreNULL = FALSE) 9 | 10 | output$mymap <- renderLeaflet({ 11 | leaflet() %>% 12 | addProviderTiles("Stamen.TonerLite", 13 | options = providerTileOptions(noWrap = TRUE) 14 | ) %>% 15 | addMarkers(data = points()) 16 | }) 17 | }) 18 | 19 | # shinyServer(function(input, output){ 20 | # 21 | # # factor out code common to all functions. 22 | # species2 <- reactive({ 23 | # strsplit(input$spec, ",")[[1]] 24 | # }) 25 | # 26 | # occur_data <- reactive({ 27 | # rcharts_prep1(sppchar = input$spec, occurrs = input$numocc, datasource = input$datasource) 28 | # }) 29 | # 30 | # rcharts_data <- reactive({ 31 | # rcharts_prep2(occur_data(), palette_name = get_palette(input$palette), popup = TRUE) 32 | # }) 33 | # 34 | # output$map_leaflet <- renderMap({ 35 | # imap = gbifmap2(input = rcharts_data(), input$provider) 36 | # imap$legend( 37 | # position = 'bottomright', 38 | # colors = get_colors(species2(), get_palette(input$palette)), 39 | # labels = species2() 40 | # ) 41 | # imap 42 | # }) 43 | # 44 | # }) 45 | -------------------------------------------------------------------------------- /inst/shiny/ui.r: -------------------------------------------------------------------------------- 1 | require(shiny) 2 | require(leaflet) 3 | 4 | shinyUI(fluidPage( 5 | leafletOutput("map_leaflet"), 6 | p(), 7 | actionButton("recalc", "New points") 8 | ) 9 | ) 10 | # shinyUI(pageWithSidebar( 11 | # 12 | # headerPanel(title=HTML("spocc explorer"), windowTitle="Species occurrence data explorer"), 13 | # 14 | # sidebarPanel( 15 | # 16 | # HTML(''), 20 | # HTML(' 21 | # '), 24 | # includeHTML('egsmodal.html'), 25 | # HTML('

'), 26 | # 27 | # # HTML(''), 28 | # 29 | # HTML(''), 30 | # 31 | # # Map options 32 | # h5(strong("Map options:")), 33 | # # data source 34 | # selectInput(inputId="datasource", label="Select data source", choices=c("GBIF","BISON","INATURALIST"), selected="BISON"), 35 | # # number of occurrences for map 36 | # sliderInput(inputId="numocc", label="Select max. number of occurrences to search for per species", min=0, max=500, value=50), 37 | # # color palette for map 38 | # selectInput(inputId="palette", label="Select color palette", 39 | # choices=c("Blues","BlueGreen","BluePurple","GreenBlue","Greens","Greys","Oranges","OrangeRed","PurpleBlue","PurpleBlueGreen","PurpleRed","Purples","RedPurple","Reds","YellowGreen","YellowGreenBlue","YlOrBr","YellowOrangeRed", 40 | # "BrownToGreen","PinkToGreen","PurpleToGreen","PurpleToOrange","RedToBlue","RedToGrey","RedYellowBlue","RedYellowGreen","Spectral"), selected="Blues"), 41 | # selectInput('provider', 'Select map provider for interactive map', 42 | # choices = c("OpenStreetMap.Mapnik","OpenStreetMap.BlackAndWhite","OpenStreetMap.DE","OpenCycleMap","Thunderforest.OpenCycleMap","Thunderforest.Transport","Thunderforest.Landscape","MapQuestOpen.OSM","MapQuestOpen.Aerial","Stamen.Toner","Stamen.TonerBackground","Stamen.TonerHybrid","Stamen.TonerLines","Stamen.TonerLabels","Stamen.TonerLite","Stamen.Terrain","Stamen.Watercolor","Esri.WorldStreetMap","Esri.DeLorme","Esri.WorldTopoMap","Esri.WorldImagery","Esri.WorldTerrain","Esri.WorldShadedRelief","Esri.WorldPhysical","Esri.OceanBasemap","Esri.NatGeoWorldMap","Esri.WorldGrayCanvas","Acetate.all","Acetate.basemap","Acetate.terrain","Acetate.foreground","Acetate.roads","Acetate.labels","Acetate.hillshading"), 43 | # selected = 'MapQuestOpen.OSM' 44 | # ) 45 | # ), 46 | # 47 | # mainPanel( 48 | # mapOutput('map_leaflet') 49 | # ) 50 | # )) 51 | -------------------------------------------------------------------------------- /inst/shinydumb/server.r: -------------------------------------------------------------------------------- 1 | require(rCharts) 2 | 3 | shinyServer(function(input, output) { 4 | output$myChart <- renderChart({ 5 | names(iris) = gsub("\\.", "", names(iris)) 6 | p1 <- rPlot(input$x, input$y, data = iris, color = "Species", 7 | facet = "Species", type = 'point') 8 | p1$addParams(dom = 'myChart') 9 | return(p1) 10 | }) 11 | }) -------------------------------------------------------------------------------- /inst/shinydumb/ui.r: -------------------------------------------------------------------------------- 1 | require(rCharts) 2 | 3 | shinyUI(pageWithSidebar( 4 | headerPanel("rCharts: Interactive Charts from R using polychart.js"), 5 | 6 | sidebarPanel( 7 | selectInput(inputId = "x", 8 | label = "Choose X", 9 | choices = c('SepalLength', 'SepalWidth', 'PetalLength', 'PetalWidth'), 10 | selected = "SepalLength"), 11 | selectInput(inputId = "y", 12 | label = "Choose Y", 13 | choices = c('SepalLength', 'SepalWidth', 'PetalLength', 'PetalWidth'), 14 | selected = "SepalWidth") 15 | ), 16 | mainPanel( 17 | showOutput("myChart", "polycharts") 18 | ) 19 | )) -------------------------------------------------------------------------------- /inst/shinysingle/app.R: -------------------------------------------------------------------------------- 1 | library(shiny) 2 | library(leaflet) 3 | library(RColorBrewer) 4 | 5 | quakes <- readr::read_csv("dat.csv") 6 | 7 | ui <- bootstrapPage( 8 | tags$style(type = "text/css", "html, body {width:100%;height:100%}"), 9 | leafletOutput("map", width = "100%", height = "100%"), 10 | absolutePanel( 11 | top = 10, right = 10, 12 | sliderInput("range", "Dates", min(quakes$date), max(quakes$date), 13 | value = range(quakes$date), step = 0.1 14 | ), 15 | selectInput("colors", "Color Scheme", 16 | rownames(subset(brewer.pal.info, category %in% c("seq", "div"))) 17 | ), 18 | checkboxInput("legend", "Show legend", TRUE) 19 | ) 20 | ) 21 | 22 | server <- function(input, output, session) { 23 | 24 | # Reactive expression for the data subsetted to what the user selected 25 | filteredData <- reactive({ 26 | quakes[quakes$date >= input$range[1] & quakes$date <= input$range[2],] 27 | }) 28 | 29 | # This reactive expression represents the palette function, 30 | # which changes as the user makes selections in UI. 31 | colorpal <- reactive({ 32 | colorNumeric(input$colors, quakes$date) 33 | }) 34 | 35 | output$map <- renderLeaflet({ 36 | # Use leaflet() here, and only include aspects of the map that 37 | # won't need to change dynamically (at least, not unless the 38 | # entire map is being torn down and recreated). 39 | leaflet(quakes) %>% addTiles() %>% 40 | fitBounds(~min(longitude), ~min(latitude), ~max(longitude), ~max(latitude)) 41 | }) 42 | 43 | # Incremental changes to the map (in this case, replacing the 44 | # circles when a new color is chosen) should be performed in 45 | # an observer. Each independent set of things that can change 46 | # should be managed in its own observer. 47 | observe({ 48 | pal <- colorpal() 49 | 50 | leafletProxy("map", data = filteredData()) %>% 51 | clearShapes() %>% 52 | addCircles( 53 | radius = 3, weight = 3, color = "#777777", 54 | fillOpacity = 0.7, 55 | popup = ~ sprintf("

Taxon: %s
Date: %s

", name, date) 56 | ) 57 | }) 58 | 59 | # Use a separate observer to recreate the legend as needed. 60 | # observe({ 61 | # proxy <- leafletProxy("map", data = quakes) 62 | # 63 | # # Remove any existing legend, and only if the legend is 64 | # # enabled, create a new one. 65 | # proxy %>% clearControls() 66 | # if (input$legend) { 67 | # pal <- colorpal() 68 | # proxy %>% addLegend(position = "bottomright", 69 | # pal = pal, values = ~date 70 | # ) 71 | # } 72 | # }) 73 | } 74 | 75 | shinyApp(ui, server) 76 | -------------------------------------------------------------------------------- /inst/shinysingle/dat.csv: -------------------------------------------------------------------------------- 1 | name,longitude,latitude,prov,date,key 2 | Danaus plexippus,-122.47638,37.76798,gbif,2016-01-27,1233602399 3 | Danaus plexippus,-95.54156,29.9758,gbif,2016-01-30,1249279832 4 | Danaus plexippus,-118.1805,33.60486,gbif,2016-01-30,1249284344 5 | Danaus plexippus,-90.42949,-0.66994,gbif,2016-01-06,1229609074 6 | Danaus plexippus,-97.40678,27.65246,gbif,2016-01-12,1272092429 7 | Danaus plexippus,175.60247,-40.32696,gbif,2016-01-23,1233597271 8 | Danaus plexippus,-118.14452,34.14779,gbif,2016-01-04,1229612625 9 | Danaus plexippus,174.75819,-36.85885,gbif,2016-01-20,1305134376 10 | Danaus plexippus,175.60247,-40.32703,gbif,2016-01-23,1233597259 11 | Danaus plexippus,-98.6832,18.99407,gbif,2016-01-05,1257409941 12 | Danaus plexippus,-16.7223,28.36629,gbif,2016-01-28,1249289303 13 | Danaus plexippus,-77.00767,-12.08692,gbif,2016-01-11,1229923719 14 | Danaus plexippus,175.60256,-40.32695,gbif,2016-01-05,1227772837 15 | Danaus plexippus,-76.72766,37.14147,gbif,2016-01-27,1233602126 16 | Danaus plexippus,-122.69873,37.90206,gbif,2016-01-01,1229613061 17 | Danaus plexippus,-95.41972,29.0425,gbif,2016-01-15,1291068906 18 | Danaus plexippus,174.7582,-36.85887,gbif,2016-01-29,1305134380 19 | Danaus plexippus,-117.97879,33.69708,gbif,2016-01-04,1227771179 20 | Danaus plexippus,-156.32246,20.74108,gbif,2016-01-02,1227771523 21 | Danaus plexippus,175.60255,-40.32694,gbif,2016-01-08,1227773534 22 | Danaus plexippus,-121.9312,36.62673,gbif,2016-01-20,1233596950 23 | Danaus plexippus,175.60255,-40.32694,gbif,2016-01-04,1227772835 24 | Danaus plexippus,-95.45833,29.95514,gbif,2016-01-31,1249281002 25 | Danaus plexippus,-121.93105,36.62667,gbif,2016-01-20,1233596871 26 | Danaus plexippus,-95.5137,29.97124,gbif,2016-01-21,1229930184 27 | Danaus plexippus,-95.41972,29.0425,gbif,2016-01-07,1291068888 28 | Danaus plexippus,-120.84169,35.32107,gbif,2016-01-28,1233602998 29 | Danaus plexippus,-80.35358,26.13078,gbif,2016-01-16,1233599675 30 | Danaus plexippus,-100.33663,19.6492,gbif,2016-01-16,1273846983 31 | Danaus plexippus,-118.21051,34.12873,gbif,2016-01-28,1233602763 32 | Danaus plexippus,-122.49187,37.77066,gbif,2016-01-25,1233600340 33 | Danaus plexippus,-122.47762,37.77077,gbif,2016-01-25,1233600220 34 | Danaus plexippus,-118.28651,33.88423,gbif,2016-01-07,1229610052 35 | Danaus plexippus,-120.64128,35.14275,gbif,2016-01-29,1291075168 36 | Danaus plexippus,-97.40678,27.65246,gbif,2016-01-12,1229614602 37 | Danaus plexippus,-99.12497,19.25662,gbif,2016-01-26,1273842045 38 | Danaus plexippus,-95.43207,29.91343,gbif,2016-01-11,1229612536 39 | Danaus plexippus,-95.51398,29.80351,gbif,2016-01-14,1229615132 40 | Danaus plexippus,-84.0786,9.9817,gbif,2016-01-27,1249283339 41 | Danaus plexippus,175.60259,-40.32689,gbif,2016-01-05,1227772850 42 | Danaus plexippus,175.60262,-40.32691,gbif,2016-01-08,1227773583 43 | Danaus plexippus,-95.40515,29.97729,gbif,2016-01-24,1233599377 44 | Danaus plexippus,-119.88012,34.42187,gbif,2016-01-22,1233602153 45 | Danaus plexippus,-95.40194,29.82862,gbif,2016-01-15,1229924209 46 | Danaus plexippus,-95.61931,29.86029,gbif,2016-01-01,1229611453 47 | Danaus plexippus,-122.473,37.79977,gbif,2016-01-27,1233602005 48 | Danaus plexippus,174.77297,-36.94085,gbif,2016-01-08,1229609836 49 | Danaus plexippus,174.75818,-36.85884,gbif,2016-01-09,1305134418 50 | Danaus plexippus,-118.28676,33.88433,gbif,2016-01-31,1249281408 51 | Danaus plexippus,-118.00195,34.14426,gbif,2016-01-24,1306593058 52 | Accipiter striatus,-77.05161,38.87834,gbif,2016-01-02,1270044795 53 | Accipiter striatus,-97.94314,30.0458,gbif,2016-01-24,1233600470 54 | Accipiter striatus,-95.50117,29.76086,gbif,2016-01-09,1229610478 55 | Accipiter striatus,-96.74874,33.03102,gbif,2016-01-28,1257416040 56 | Accipiter striatus,-96.55633,17.8864,gbif,2016-01-14,1272110730 57 | Accipiter striatus,-75.65139,45.44557,gbif,2016-01-02,1227768502 58 | Accipiter striatus,-103.01232,36.38905,gbif,2016-01-04,1227771246 59 | Accipiter striatus,-72.48018,43.72704,gbif,2016-01-17,1233600875 60 | Accipiter striatus,-116.67145,32.94147,gbif,2016-01-12,1229613664 61 | Accipiter striatus,-123.44703,48.54571,gbif,2016-01-31,1249281491 62 | Accipiter striatus,-122.40089,37.49201,gbif,2016-01-09,1229610667 63 | Accipiter striatus,-92.44909,38.92764,gbif,2016-01-31,1249281004 64 | Accipiter striatus,-73.33156,44.71433,gbif,2016-01-18,1253311732 65 | Accipiter striatus,-98.24809,26.10815,gbif,2016-01-13,1229927690 66 | Accipiter striatus,-97.21962,32.88749,gbif,2016-01-31,1249281822 67 | Accipiter striatus,-106.31531,31.71593,gbif,2016-01-20,1233597063 68 | Accipiter striatus,-97.81493,26.0315,gbif,2016-01-14,1229927719 69 | Accipiter striatus,-81.85267,28.81852,gbif,2016-01-18,1253301153 70 | Accipiter striatus,-73.23131,44.28476,gbif,2016-01-03,1227769707 71 | Accipiter striatus,-81.85329,28.81806,gbif,2016-01-18,1249295043 72 | Accipiter striatus,-96.91463,32.82949,gbif,2016-01-20,1229929215 73 | Accipiter striatus,-135.32684,57.05398,gbif,2016-01-09,1229612615 74 | Accipiter striatus,-97.40153,30.74022,gbif,2016-01-27,1249286013 75 | Accipiter striatus,-123.44703,48.54571,gbif,2016-01-31,1249281424 76 | Accipiter striatus,-98.31887,26.04385,gbif,2016-02-12,1305143495 77 | Accipiter striatus,-135.32676,57.05407,gbif,2016-02-27,1253311635 78 | Accipiter striatus,-72.53203,42.99175,gbif,2016-02-11,1253301216 79 | Accipiter striatus,-97.33901,32.98793,gbif,2016-02-17,1253301996 80 | Accipiter striatus,-106.12754,38.67282,gbif,2016-02-09,1324192382 81 | Accipiter striatus,-116.80062,35.9156,gbif,2016-02-06,1257413987 82 | Accipiter striatus,-114.16574,51.08735,gbif,2016-02-21,1253305197 83 | Accipiter striatus,-96.98175,32.82409,gbif,2016-02-08,1249289564 84 | Accipiter striatus,-121.69263,36.90376,gbif,2016-02-03,1249287169 85 | Accipiter striatus,-75.79706,45.37368,gbif,2016-02-27,1253311157 86 | Accipiter striatus,-135.32907,57.05242,gbif,2016-02-10,1249293443 87 | Accipiter striatus,-96.84449,32.80045,gbif,2016-02-20,1253303948 88 | Accipiter striatus,-135.32908,57.05243,gbif,2016-02-10,1249291910 89 | Accipiter striatus,-72.42691,43.52833,gbif,2016-02-20,1253315419 90 | Accipiter striatus,-100.52378,25.77735,gbif,2016-02-21,1253304942 91 | Accipiter striatus,-97.0594,49.95094,gbif,2016-02-13,1305176324 92 | Accipiter striatus,-76.79219,40.18353,gbif,2016-03-07,1315051322 93 | Accipiter striatus,-122.12158,37.07833,gbif,2016-03-24,1262389472 94 | Accipiter striatus,-117.06333,33.097410000000004,gbif,2016-03-06,1257412928 95 | Accipiter striatus,-93.85853,29.91932,gbif,2016-03-31,1265550819 96 | Accipiter striatus,-123.97608,49.18573,gbif,2016-03-29,1265547985 97 | Accipiter striatus,-96.96927,32.99617,gbif,2016-03-01,1253315938 98 | Accipiter striatus,-73.46454,40.80439,gbif,2016-03-15,1291137787 99 | Accipiter striatus,-72.81245,43.77387,gbif,2016-03-20,1262384574 100 | Accipiter striatus,-105.01721,40.41241,gbif,2016-03-29,1273762649 101 | Accipiter striatus,-72.46131,44.34088,gbif,2016-03-24,1262388795 102 | Pinus contorta,168.85006,-44.94818,gbif,2016-01-30,1269541527 103 | Pinus contorta,-120.33987,39.34308,gbif,2016-01-03,1249276846 104 | Pinus contorta,176.32093,-39.33307,gbif,2016-02-16,1249301037 105 | Pinus contorta,-123.35278,48.90594,gbif,2016-02-29,1253314823 106 | Pinus contorta,-123.9821,46.20296,gbif,2016-02-07,1249288703 107 | Pinus contorta,7.01607,62.8677,gbif,2016-02-20,1272958740 108 | Pinus contorta,-123.90852,46.24632,gbif,2016-03-15,1265538444 109 | Pinus contorta,-123.35651,48.90668,gbif,2016-03-03,1265538015 110 | Pinus contorta,6.9002,58.37633,gbif,2016-03-24,1272971879 111 | Pinus contorta,-124.03073,46.52314,gbif,2016-03-29,1265549253 112 | Pinus contorta,10.2992,63.1868,gbif,2016-04-10,1272977243 113 | Pinus contorta,-115.55988,51.16078,gbif,2016-04-21,1265894722 114 | Pinus contorta,-135.3268,57.05413,gbif,2016-04-07,1265893946 115 | Pinus contorta,169.86365,-44.37911,gbif,2016-04-27,1265601314 116 | Pinus contorta,-123.93707,46.1007,gbif,2016-05-23,1291077604 117 | Pinus contorta,-123.93667,46.10185,gbif,2016-05-23,1269581192 118 | Pinus contorta,-111.37106,44.42436,gbif,2016-05-26,1305122165 119 | Pinus contorta,-124.20357,41.87682,gbif,2016-05-01,1291073778 120 | Pinus contorta,-121.10481,48.69273,gbif,2016-05-20,1272081884 121 | Pinus contorta,6.56216,58.27306,gbif,2016-05-12,1272994052 122 | Pinus contorta,-9.51761,51.42803,gbif,2016-06-08,1313447593 123 | Pinus contorta,7.62006,63.10301,gbif,2016-06-22,1323244428 124 | Pinus contorta,-9.89982,51.62921,gbif,2016-06-09,1313444768 125 | Pinus contorta,7.62423,63.10288,gbif,2016-06-22,1323244301 126 | Pinus contorta,-124.16598,40.85716,gbif,2016-06-11,1291082007 127 | Pinus contorta,-135.71261,58.40588,gbif,2016-06-25,1291087880 128 | Pinus contorta,-120.23639,39.43396,gbif,2016-06-25,1291089477 129 | Pinus contorta,7.01022,62.87423,gbif,2016-06-18,1323231903 130 | Pinus contorta,-120.12141,38.67326,gbif,2016-06-09,1273848214 131 | Pinus contorta,-119.05444,48.8509,gbif,2016-06-07,1272098674 132 | Pinus contorta,-135.80708,62.01669,gbif,2016-06-25,1315049949 133 | Pinus contorta,-120.24316,39.43183,gbif,2016-06-23,1273857360 134 | Pinus contorta,-118.37857,35.99255,gbif,2016-06-14,1272113221 135 | Pinus contorta,-120.02872,38.87559,gbif,2016-06-18,1273849641 136 | Pinus contorta,-9.84172,51.62114,gbif,2016-06-11,1313444807 137 | Pinus contorta,7.62,63.10423,gbif,2016-06-22,1323244287 138 | Pinus contorta,-120.61783,39.43138,gbif,2016-06-18,1291082872 139 | Pinus contorta,-119.25182,37.90818,gbif,2016-06-19,1273854458 140 | Pinus contorta,-118.3786,35.99249,gbif,2016-06-14,1272115957 141 | Pinus contorta,-123.92057,45.70848,gbif,2016-07-05,1291164213 142 | Pinus contorta,-118.60455,37.72449,gbif,2016-07-30,1291666457 143 | Pinus contorta,6.63771,58.67098,gbif,2016-07-19,1323278957 144 | Pinus contorta,-121.45473,45.34683,gbif,2016-07-03,1291105136 145 | Pinus contorta,-123.93886,45.65393,gbif,2016-07-30,1291157544 146 | Pinus contorta,-120.24387,39.42851,gbif,2016-07-20,1291143024 147 | Pinus contorta,-120.33929,39.34084,gbif,2016-07-20,1291140722 148 | Pinus contorta,-121.94898,46.79819,gbif,2016-07-28,1305156511 149 | Pinus contorta,-151.06303,60.77443,gbif,2016-07-12,1305123900 150 | Pinus contorta,11.24959,63.13348,gbif,2016-07-15,1323312764 151 | Pinus contorta,-113.66794,48.78797,gbif,2016-07-09,1291665469 152 | -------------------------------------------------------------------------------- /man-roxygen/args.R: -------------------------------------------------------------------------------- 1 | #' @param x The data. An object of class \code{occdat}, \code{occdatind}, 2 | #' \code{gbif}, \code{gbif_data}, \code{SpatialPoints}, 3 | #' \code{SpatialPointsDataFrame}, or \code{data.frame}. The package 4 | #' \pkg{spocc} needed for 5 | #' the first two, and \pkg{rgbif} needed for the third. When \code{data.frame} 6 | #' input, any number of columns allowed, but with at least the following: 7 | #' name (the taxonomic name), latitude (in dec. deg.), longitude (in dec. deg.) 8 | #' @param lon,lat (character) Longitude and latitude variable names. Ignored 9 | #' unless \code{data.frame} input to \code{x} parameter. We attempt to guess, 10 | #' but if nothing close, we stop. Default: \code{longitude} and 11 | #' \code{latitude} 12 | -------------------------------------------------------------------------------- /man/figures/gist.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ropensci-archive/mapr/efc0b34fc01d46769778db46c23e689538f51668/man/figures/gist.png -------------------------------------------------------------------------------- /man/figures/leaflet.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ropensci-archive/mapr/efc0b34fc01d46769778db46c23e689538f51668/man/figures/leaflet.png -------------------------------------------------------------------------------- /man/figures/unnamed-chunk-5-1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ropensci-archive/mapr/efc0b34fc01d46769778db46c23e689538f51668/man/figures/unnamed-chunk-5-1.png -------------------------------------------------------------------------------- /man/figures/unnamed-chunk-6-1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ropensci-archive/mapr/efc0b34fc01d46769778db46c23e689538f51668/man/figures/unnamed-chunk-6-1.png -------------------------------------------------------------------------------- /man/figures/unnamed-chunk-7-1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ropensci-archive/mapr/efc0b34fc01d46769778db46c23e689538f51668/man/figures/unnamed-chunk-7-1.png -------------------------------------------------------------------------------- /man/gbif_eg1.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/mapr-package.R 3 | \docType{data} 4 | \name{gbif_eg1} 5 | \alias{gbif_eg1} 6 | \title{Example dataset: output from call to \code{rgbif::occ_search()}} 7 | \format{ 8 | A data frame with 50 rows and 101 variables 9 | } 10 | \description{ 11 | A dataset with 50 rows, and 101 columns, from the query: 12 | \code{rgbif::occ_search(scientificName = "Puma concolor", limit = 100)} 13 | } 14 | \details{ 15 | See \code{inst/ignore/datasets.R} for the code to prepare this dataaset 16 | } 17 | \keyword{datasets} 18 | -------------------------------------------------------------------------------- /man/hull.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/hull.R 3 | \name{hull} 4 | \alias{hull} 5 | \title{Add a convex hull to a map} 6 | \usage{ 7 | hull(x, ...) 8 | } 9 | \arguments{ 10 | \item{x}{input} 11 | 12 | \item{...}{ignored} 13 | } 14 | \value{ 15 | Adds a convex hull to the plot. See \code{\link[grDevices:chull]{grDevices::chull()}} 16 | for info. 17 | } 18 | \description{ 19 | Add a convex hull to a map 20 | } 21 | \details{ 22 | Can be used with \code{\link[=map_leaflet]{map_leaflet()}}, \code{\link[=map_plot]{map_plot()}}, 23 | and \code{\link[=map_ggplot]{map_ggplot()}}. Other methods in this package may be supported 24 | in the future. 25 | } 26 | \examples{ 27 | # map spocc output, here using a built in object 28 | data(occdat_eg1) 29 | map_plot(occdat_eg1, hull = TRUE) 30 | 31 | # map rgbif output, here using a built in object 32 | hull(map_ggplot(occdat_eg1)) 33 | 34 | \dontrun{ 35 | # leaflet 36 | library("spocc") 37 | spp <- c('Danaus plexippus', 'Accipiter striatus', 'Pinus contorta') 38 | dat <- occ(spp, from = 'gbif', limit = 30, has_coords = TRUE) 39 | hull(map_leaflet(dat)) 40 | 41 | # ggplot 42 | if (requireNamespace("rgbif")) { 43 | library("rgbif") 44 | res <- occ_search(scientificName = "Puma concolor", limit = 100) 45 | hull(map_ggplot(res)) 46 | } 47 | 48 | # base plots 49 | library("spocc") 50 | out <- occ(query='Accipiter striatus', from='gbif', limit=25, 51 | has_coords=TRUE) 52 | map_plot(out, hull = TRUE) 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /man/map_ggmap-defunct.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/map_ggmap.R 3 | \name{map_ggmap} 4 | \alias{map_ggmap} 5 | \title{ggpmap visualization of species occurences} 6 | \usage{ 7 | map_ggmap(...) 8 | } 9 | \arguments{ 10 | \item{...}{Ignored} 11 | } 12 | \description{ 13 | THIS FUNCTION IS DEFUNCT 14 | } 15 | \keyword{internal} 16 | -------------------------------------------------------------------------------- /man/map_ggplot.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/map_ggplot.R 3 | \name{map_ggplot} 4 | \alias{map_ggplot} 5 | \title{ggplot2 mapping} 6 | \usage{ 7 | map_ggplot( 8 | x, 9 | map = "world", 10 | point_color = "#86161f", 11 | color = NULL, 12 | size = 3, 13 | lon = "longitude", 14 | lat = "latitude", 15 | name = NULL, 16 | ... 17 | ) 18 | } 19 | \arguments{ 20 | \item{x}{The data. An object of class \code{occdat}, \code{occdatind}, 21 | \code{gbif}, \code{gbif_data}, \code{SpatialPoints}, 22 | \code{SpatialPointsDataFrame}, or \code{data.frame}. The package 23 | \pkg{spocc} needed for 24 | the first two, and \pkg{rgbif} needed for the third. When \code{data.frame} 25 | input, any number of columns allowed, but with at least the following: 26 | name (the taxonomic name), latitude (in dec. deg.), longitude (in dec. deg.)} 27 | 28 | \item{map}{(character) One of world, world2, state, usa, county, france, 29 | italy, or nz} 30 | 31 | \item{point_color}{Default color of your points. Deprecated, use 32 | \code{color}} 33 | 34 | \item{color}{Default color of your points.} 35 | 36 | \item{size}{point size, Default: 3} 37 | 38 | \item{lon, lat}{(character) Longitude and latitude variable names. Ignored 39 | unless \code{data.frame} input to \code{x} parameter. We attempt to guess, 40 | but if nothing close, we stop. Default: \code{longitude} and 41 | \code{latitude}} 42 | 43 | \item{name}{(character) the column name that contains the name to use in 44 | creating the map. If left \code{NULL} we look for a "name" column. 45 | default: \code{NULL}} 46 | 47 | \item{...}{Ignored} 48 | } 49 | \value{ 50 | A ggplot2 map, of class \code{gg/ggplot} 51 | } 52 | \description{ 53 | ggplot2 mapping 54 | } 55 | \examples{ 56 | # map spocc output, here using a built in object 57 | data(occdat_eg1) 58 | map_ggplot(occdat_eg1) 59 | 60 | # map rgbif output, here using a built in object 61 | data(gbif_eg1) 62 | map_ggplot(gbif_eg1) 63 | 64 | \dontrun{ 65 | ## spocc 66 | library("spocc") 67 | ddat <- occ(query = 'Lynx rufus californicus', from = 'gbif', limit=100) 68 | map_ggplot(ddat) 69 | map_ggplot(ddat$gbif) 70 | map_ggplot(ddat$gbif, "usa") 71 | map_ggplot(ddat, "county") 72 | 73 | ### usage of occ2sp() 74 | #### SpatialPoints 75 | spdat <- occ2sp(ddat) 76 | map_ggplot(spdat) 77 | #### SpatialPointsDataFrame 78 | spdatdf <- as(spdat, "SpatialPointsDataFrame") 79 | map_ggplot(spdatdf) 80 | 81 | ## rgbif 82 | if (requireNamespace("rgbif")) { 83 | library("rgbif") 84 | library("ggplot2") 85 | ### occ_search() output 86 | res <- occ_search(scientificName = "Puma concolor", limit = 100) 87 | map_ggplot(res) 88 | 89 | ### occ_data() output 90 | res <- occ_data(scientificName = "Puma concolor", limit = 100) 91 | map_ggplot(res) 92 | 93 | #### many taxa 94 | res <- occ_data(scientificName = c("Puma concolor", "Quercus lobata"), 95 | limit = 30) 96 | map_ggplot(res) 97 | 98 | ### add a convex hull 99 | hull(map_ggplot(res)) 100 | } 101 | 102 | ## data.frame 103 | df <- data.frame(name = c('Poa annua', 'Puma concolor', 'Foo bar'), 104 | longitude = c(-120, -121, -121), 105 | latitude = c(41, 42, 45), stringsAsFactors = FALSE) 106 | map_ggplot(df) 107 | 108 | # many species, each gets a different color 109 | library("spocc") 110 | spp <- c('Danaus plexippus', 'Accipiter striatus', 'Pinus contorta') 111 | dat <- occ(spp, from = 'gbif', limit = 30, has_coords = TRUE) 112 | map_ggplot(dat, color = c('#976AAE', '#6B944D', '#BD5945')) 113 | } 114 | } 115 | -------------------------------------------------------------------------------- /man/map_gist.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/map_gist.R 3 | \name{map_gist} 4 | \alias{map_gist} 5 | \title{Make an interactive map to view in the browser as a GitHub gist} 6 | \usage{ 7 | map_gist( 8 | x, 9 | description = "", 10 | public = TRUE, 11 | browse = TRUE, 12 | lon = "longitude", 13 | lat = "latitude", 14 | name = NULL, 15 | ... 16 | ) 17 | } 18 | \arguments{ 19 | \item{x}{The data. An object of class \code{occdat}, \code{occdatind}, 20 | \code{gbif}, \code{gbif_data}, \code{SpatialPoints}, 21 | \code{SpatialPointsDataFrame}, or \code{data.frame}. The package 22 | \pkg{spocc} needed for 23 | the first two, and \pkg{rgbif} needed for the third. When \code{data.frame} 24 | input, any number of columns allowed, but with at least the following: 25 | name (the taxonomic name), latitude (in dec. deg.), longitude (in dec. deg.)} 26 | 27 | \item{description}{Description for the Github gist, or leave to 28 | default (=no description)} 29 | 30 | \item{public}{(logical) Whether gist is public (default: \code{TRUE})} 31 | 32 | \item{browse}{If \code{TRUE} (default) the map opens in your default browser.} 33 | 34 | \item{lon, lat}{(character) Longitude and latitude variable names. Ignored 35 | unless \code{data.frame} input to \code{x} parameter. We attempt to guess, 36 | but if nothing close, we stop. Default: \code{longitude} and 37 | \code{latitude}} 38 | 39 | \item{name}{(character) the column name that contains the name to use in 40 | creating the map. If left \code{NULL} we look for a "name" column.} 41 | 42 | \item{...}{Further arguments passed on to \code{\link[=style_geojson]{style_geojson()}}} 43 | } 44 | \description{ 45 | Make an interactive map to view in the browser as a GitHub gist 46 | } 47 | \details{ 48 | See \code{\link[gistr:gist_auth]{gistr::gist_auth()}} for help on authentication 49 | 50 | Does not support adding a convex hull via \code{\link[=hull]{hull()}} 51 | } 52 | \examples{ 53 | \dontrun{ 54 | ## spocc 55 | library("spocc") 56 | spp <- c('Danaus plexippus', 'Accipiter striatus', 'Pinus contorta') 57 | dat <- spocc::occ(spp, from=c('gbif','ecoengine'), limit=30, 58 | gbifopts=list(hasCoordinate=TRUE)) 59 | 60 | # Define colors 61 | map_gist(dat, color=c('#976AAE','#6B944D','#BD5945')) 62 | 63 | # Define colors and marker size 64 | map_gist(dat, color=c('#976AAE','#6B944D','#BD5945'), 65 | size=c('small','medium','large')) 66 | 67 | # Define symbols 68 | map_gist(dat, symbol=c('park','zoo','garden')) 69 | 70 | ## rgbif 71 | if (requireNamespace("rgbif")) { 72 | library("rgbif") 73 | ### occ_search() output 74 | res <- occ_search(scientificName = "Puma concolor", limit = 100) 75 | map_gist(res) 76 | 77 | ### occ_data() output 78 | res <- occ_data(scientificName = "Puma concolor", limit = 100) 79 | map_gist(res) 80 | 81 | #### many taxa 82 | res <- occ_data(scientificName = c("Puma concolor", "Quercus lobata"), 83 | limit = 30) 84 | res 85 | map_gist(res) 86 | } 87 | 88 | ## data.frame 89 | df <- data.frame(name = c('Poa annua', 'Puma concolor', 'Foo bar'), 90 | longitude = c(-120, -121, -121), 91 | latitude = c(41, 42, 45), stringsAsFactors = FALSE) 92 | map_gist(df) 93 | 94 | ### usage of occ2sp() 95 | #### SpatialPoints 96 | spdat <- occ2sp(dat) 97 | map_gist(spdat) 98 | #### SpatialPointsDataFrame 99 | spdatdf <- as(spdat, "SpatialPointsDataFrame") 100 | map_gist(spdatdf) 101 | } 102 | } 103 | -------------------------------------------------------------------------------- /man/map_leaflet.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/map_leaflet.R 3 | \name{map_leaflet} 4 | \alias{map_leaflet} 5 | \title{Make interactive maps with Leaflet.js} 6 | \usage{ 7 | map_leaflet( 8 | x, 9 | lon = "longitude", 10 | lat = "latitude", 11 | color = NULL, 12 | size = 13, 13 | name = NULL, 14 | ... 15 | ) 16 | } 17 | \arguments{ 18 | \item{x}{The data. An object of class \code{occdat}, \code{occdatind}, 19 | \code{gbif}, \code{gbif_data}, \code{SpatialPoints}, 20 | \code{SpatialPointsDataFrame}, or \code{data.frame}. The package 21 | \pkg{spocc} needed for 22 | the first two, and \pkg{rgbif} needed for the third. When \code{data.frame} 23 | input, any number of columns allowed, but with at least the following: 24 | name (the taxonomic name), latitude (in dec. deg.), longitude (in dec. deg.)} 25 | 26 | \item{lon, lat}{(character) Longitude and latitude variable names. Ignored 27 | unless \code{data.frame} input to \code{x} parameter. We attempt to guess, 28 | but if nothing close, we stop. Default: \code{longitude} and 29 | \code{latitude}} 30 | 31 | \item{color}{Default color of your points.} 32 | 33 | \item{size}{point size, Default: 13} 34 | 35 | \item{name}{(character) the column name that contains the name to use in 36 | creating the map. If left \code{NULL} we look for a "name" column.} 37 | 38 | \item{...}{Ignored} 39 | } 40 | \value{ 41 | a Leaflet map in Viewer in Rstudio, or in your default browser 42 | otherwise 43 | } 44 | \description{ 45 | Make interactive maps with Leaflet.js 46 | } 47 | \details{ 48 | We add popups by default, and add all columns to the popup. The 49 | html is escaped with \code{htmltools::htmlEscape()} 50 | } 51 | \examples{ 52 | \dontrun{ 53 | ## spocc 54 | library("spocc") 55 | (out <- occ(query='Accipiter striatus', from='gbif', limit=50, 56 | has_coords=TRUE)) 57 | ### with class occdat 58 | map_leaflet(out) 59 | ### with class occdatind 60 | map_leaflet(out$gbif) 61 | ### use occ2sp 62 | map_leaflet(occ2sp(out)) 63 | 64 | ## rgbif 65 | if (requireNamespace("rgbif")) { 66 | library("rgbif") 67 | res <- occ_search(scientificName = "Puma concolor", limit = 100) 68 | map_leaflet(res) 69 | } 70 | 71 | ## SpatialPoints class 72 | library("sp") 73 | df <- data.frame(longitude = c(-120,-121), 74 | latitude = c(41, 42), stringsAsFactors = FALSE) 75 | x <- SpatialPoints(df) 76 | map_leaflet(x) 77 | 78 | ## SpatialPointsDataFrame class 79 | if (requireNamespace("rgbif")) { 80 | library("rgbif") 81 | ### occ_search() output 82 | res <- occ_search(scientificName = "Puma concolor", limit = 100) 83 | x <- res$data 84 | library("sp") 85 | x <- x[stats::complete.cases(x$decimalLatitude, x$decimalLongitude), ] 86 | coordinates(x) <- ~decimalLongitude+decimalLatitude 87 | map_leaflet(x) 88 | 89 | ### occ_data() output 90 | res <- occ_data(scientificName = "Puma concolor", limit = 100) 91 | map_leaflet(res) 92 | } 93 | 94 | #### many taxa 95 | res <- occ_data(scientificName = c("Puma concolor", "Quercus lobata"), 96 | limit = 30) 97 | res 98 | map_leaflet(res) 99 | 100 | 101 | ## data.frame 102 | df <- data.frame(name = c('Poa annua', 'Puma concolor'), 103 | longitude = c(-120,-121), 104 | latitude = c(41, 42), stringsAsFactors = FALSE) 105 | map_leaflet(df) 106 | 107 | # many species 108 | library("spocc") 109 | spp <- c('Danaus plexippus', 'Accipiter striatus', 'Pinus contorta') 110 | dat <- occ(spp, from = 'gbif', limit = 50, has_coords = TRUE) 111 | map_leaflet(dat) 112 | map_leaflet(dat, color = c('#AFFF71', '#AFFF71', '#AFFF71')) 113 | map_leaflet(dat, color = c('#976AAE', '#6B944D', '#BD5945')) 114 | 115 | # add a convex hull 116 | ## map_leaflet(dat) \%>\% hull() # using pipes 117 | hull(map_leaflet(dat)) 118 | } 119 | } 120 | -------------------------------------------------------------------------------- /man/map_plot.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/map_plot.R 3 | \name{map_plot} 4 | \alias{map_plot} 5 | \title{Base R mapping} 6 | \usage{ 7 | map_plot( 8 | x, 9 | lon = "longitude", 10 | lat = "latitude", 11 | color = NULL, 12 | size = 1, 13 | pch = 16, 14 | hull = FALSE, 15 | name = NULL, 16 | ... 17 | ) 18 | } 19 | \arguments{ 20 | \item{x}{The data. An object of class \code{occdat}, \code{occdatind}, 21 | \code{gbif}, \code{gbif_data}, \code{SpatialPoints}, 22 | \code{SpatialPointsDataFrame}, or \code{data.frame}. The package 23 | \pkg{spocc} needed for 24 | the first two, and \pkg{rgbif} needed for the third. When \code{data.frame} 25 | input, any number of columns allowed, but with at least the following: 26 | name (the taxonomic name), latitude (in dec. deg.), longitude (in dec. deg.)} 27 | 28 | \item{lon, lat}{(character) Longitude and latitude variable names. Ignored 29 | unless \code{data.frame} input to \code{x} parameter. We attempt to guess, 30 | but if nothing close, we stop. Default: \code{longitude} and 31 | \code{latitude}} 32 | 33 | \item{color}{Default color of your points.} 34 | 35 | \item{size}{point size, passed to \code{cex} Default: 1} 36 | 37 | \item{pch}{point symbol shape, Default: 16} 38 | 39 | \item{hull}{(logical) whether to add a convex hull. Default: \code{FALSE}} 40 | 41 | \item{name}{(character) the column name that contains the name to use in 42 | creating the map. If left \code{NULL} we look for a "name" column.} 43 | 44 | \item{...}{Further args to \code{\link[graphics:points]{graphics::points()}}} 45 | } 46 | \value{ 47 | Plots a world scale map 48 | } 49 | \description{ 50 | Base R mapping 51 | } 52 | \examples{ 53 | # map spocc output, here using a built in object 54 | data(occdat_eg1) 55 | map_plot(occdat_eg1) 56 | 57 | # map rgbif output, here using a built in object 58 | data(gbif_eg1) 59 | map_plot(gbif_eg1) 60 | 61 | \dontrun{ 62 | ## spocc 63 | library("spocc") 64 | (out <- occ(query='Accipiter striatus', from='gbif', limit=25, 65 | has_coords=TRUE)) 66 | ### class occdat 67 | map_plot(out) 68 | map_plot(out, hull = TRUE) 69 | ### class occdatind 70 | map_plot(out$gbif) 71 | map_plot(out$gbif, hull = TRUE) 72 | 73 | ## rgbif 74 | if (requireNamespace("rgbif")) { 75 | library("rgbif") 76 | ### occ_search() output 77 | res <- occ_search(scientificName = "Puma concolor", limit = 100) 78 | map_plot(res) 79 | map_plot(res, hull = TRUE) 80 | 81 | ### occ_data() output 82 | res <- occ_data(scientificName = "Puma concolor", limit = 100) 83 | map_plot(res) 84 | #### many taxa 85 | res <- occ_data(scientificName = c("Puma concolor", "Quercus lobata"), 86 | limit = 30) 87 | res 88 | map_plot(res) 89 | } 90 | 91 | 92 | ## data.frame 93 | df <- data.frame( 94 | name = c('Poa annua', 'Puma concolor', 'Foo bar', 'Stuff things'), 95 | longitude = c(-125, -123, -121, -110), 96 | latitude = c(41, 42, 45, 30), stringsAsFactors = FALSE) 97 | map_plot(df) 98 | map_plot(df, hull = TRUE) 99 | 100 | ### usage of occ2sp() 101 | #### SpatialPoints 102 | spdat <- occ2sp(out) 103 | map_plot(spdat) 104 | map_plot(spdat, hull = TRUE) 105 | 106 | #### SpatialPointsDataFrame 107 | spdatdf <- as(spdat, "SpatialPointsDataFrame") 108 | map_plot(spdatdf) 109 | map_plot(spdatdf, hull = TRUE) 110 | 111 | # many species, each gets a different color 112 | library("spocc") 113 | spp <- c('Danaus plexippus', 'Accipiter striatus', 'Pinus contorta', 114 | 'Ursus americanus') 115 | dat <- occ(spp, from = 'gbif', limit = 30, has_coords = TRUE, 116 | gbifopts = list(country = 'US')) 117 | map_plot(dat) 118 | map_plot(dat, hull = TRUE) 119 | ## diff. color for each taxon 120 | map_plot(dat, color = c('#976AAE', '#6B944D', '#BD5945', 'red')) 121 | map_plot(dat, color = c('#976AAE', '#6B944D', '#BD5945', 'red'), hull = TRUE) 122 | 123 | # add a convex hull 124 | if (requireNamespace("rgbif")) { 125 | library("rgbif") 126 | res <- occ_search(scientificName = "Puma concolor", limit = 100) 127 | map_plot(res, hull = FALSE) 128 | map_plot(res, hull = TRUE) 129 | } 130 | } 131 | } 132 | -------------------------------------------------------------------------------- /man/mapr-package.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/mapr-package.R 3 | \docType{package} 4 | \name{mapr-package} 5 | \alias{mapr-package} 6 | \alias{mapr} 7 | \title{mapr} 8 | \description{ 9 | Visualize species occurrence data 10 | } 11 | \section{Many inputs}{ 12 | 13 | All functions take the following kinds of inputs: 14 | \itemize{ 15 | \item An object of class \code{occdat}, from the package \pkg{spocc}. 16 | An object of this class is composed of many objects of class 17 | \code{occdatind} 18 | \item An object of class \code{occdatind}, from the package \pkg{spocc} 19 | \item An object of class \code{gbif}, from the package \pkg{rgbif} 20 | \item An object of class \code{data.frame}. This data.frame can have any 21 | columns, but must include a column for taxonomic names (e.g., \code{name}), 22 | and for latitude and longitude (we guess your lat/long columns, starting 23 | with the default \code{latitude} and \code{longitude}) 24 | \item An object of class \code{SpatialPoints} 25 | \item An object of class \code{SpatialPointsDatFrame} 26 | } 27 | } 28 | 29 | \section{Package API}{ 30 | 31 | \itemize{ 32 | \item \code{\link[=map_plot]{map_plot()}} - static Base R plots 33 | \item \code{\link[=map_ggplot]{map_ggplot()}} - static ggplot2 plots 34 | \item \code{\link[=map_leaflet]{map_leaflet()}} - interactive Leaflet.js interactive maps 35 | \item \code{\link[=map_gist]{map_gist()}} - ineractive, shareable maps on GitHub Gists 36 | } 37 | } 38 | 39 | \author{ 40 | Scott Chamberlain 41 | } 42 | \keyword{package} 43 | -------------------------------------------------------------------------------- /man/occ2sp.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/occ2sp.R 3 | \name{occ2sp} 4 | \alias{occ2sp} 5 | \title{Create a spatial points dataframe from a spocc search} 6 | \usage{ 7 | occ2sp(x, coord_string = "+proj=longlat +datum=WGS84", just_coords = FALSE) 8 | } 9 | \arguments{ 10 | \item{x}{The resuslts of a spocc search called by \code{\link[spocc:occ]{spocc::occ()}}} 11 | 12 | \item{coord_string}{A valid EPGS cooridate string from the sp package, 13 | the default is WSGS 84} 14 | 15 | \item{just_coords}{Return data frame with specios names and provenance or 16 | just a spatial points object, which is the default.} 17 | } 18 | \description{ 19 | Create a spatial points dataframe from a spocc search 20 | } 21 | \details{ 22 | This function will return either a spatial points dataframe or 23 | spatial points object. Conversion to spatial points objects allows spocc 24 | searches to interact with other spatial data sources. More coordinate system 25 | codes can be found at the EPGS registry 26 | } 27 | \examples{ 28 | \dontrun{ 29 | ### See points on a map 30 | library("maptools") 31 | library("spocc") 32 | data(wrld_simpl) 33 | plot(wrld_simpl[wrld_simpl$NAME == "United States", ], xlim = c(-70, -60)) 34 | out <- occ(query = "Accipiter striatus", from = c("vertnet", "gbif"), 35 | limit = 50) 36 | xx <- occ2sp(out, just_coords = TRUE) 37 | points(xx, col = 2) 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /man/occdat_eg1.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/mapr-package.R 3 | \docType{data} 4 | \name{occdat_eg1} 5 | \alias{occdat_eg1} 6 | \title{Example dataset: output from call to \code{\link[spocc:occ]{spocc::occ()}}} 7 | \format{ 8 | A data frame with 25 rows and 62 variables 9 | } 10 | \description{ 11 | A dataset with 25 rows, and 62 columns, from the query: 12 | \code{occ(query='Accipiter striatus', from='gbif', limit=25, has_coords=TRUE)} 13 | } 14 | \details{ 15 | See \code{inst/ignore/datasets.R} for the code to prepare this dataaset 16 | } 17 | \keyword{datasets} 18 | -------------------------------------------------------------------------------- /man/style_geojson.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/util_github.r 3 | \name{style_geojson} 4 | \alias{style_geojson} 5 | \title{Style a data.frame prior to converting to geojson.} 6 | \usage{ 7 | style_geojson( 8 | input, 9 | var = NULL, 10 | var_col = NULL, 11 | var_sym = NULL, 12 | var_size = NULL, 13 | color = NULL, 14 | symbol = NULL, 15 | size = NULL 16 | ) 17 | } 18 | \arguments{ 19 | \item{input}{A data.frame} 20 | 21 | \item{var}{A single variable to map colors, symbols, and/or sizes to.} 22 | 23 | \item{var_col}{The variable to map colors to.} 24 | 25 | \item{var_sym}{The variable to map symbols to.} 26 | 27 | \item{var_size}{The variable to map size to.} 28 | 29 | \item{color}{Valid RGB hex color} 30 | 31 | \item{symbol}{An icon ID from the Maki project 32 | https://labs.mapbox.com/maki-icons/ or a single alphanumeric character 33 | (a-z or 0-9).} 34 | 35 | \item{size}{One of 'small', 'medium', or 'large'} 36 | } 37 | \description{ 38 | Style a data.frame prior to converting to geojson. 39 | } 40 | -------------------------------------------------------------------------------- /tests/test-that.R: -------------------------------------------------------------------------------- 1 | library('testthat') 2 | test_check("mapr") 3 | -------------------------------------------------------------------------------- /tests/testthat/spocc_df_1.rda: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ropensci-archive/mapr/efc0b34fc01d46769778db46c23e689538f51668/tests/testthat/spocc_df_1.rda -------------------------------------------------------------------------------- /tests/testthat/test-map_ggplot.R: -------------------------------------------------------------------------------- 1 | context("map_ggplot works correctly") 2 | 3 | test_that("map_ggplot work as expected", { 4 | skip_on_cran() 5 | 6 | library("ggplot2") 7 | library("spocc") 8 | 9 | res <- occ(query = "Lynx rufus californicus", from = "gbif") 10 | map1 <- suppressMessages(map_ggplot(res)) 11 | res2 <- occ(query = 'Accipiter striatus', from = 'gbif') 12 | map2 <- suppressMessages(map_ggplot(res2)) 13 | expect_is(res, "occdat") 14 | expect_is(res2, "occdat") 15 | expect_is(map1, "ggplot") 16 | expect_is(map2, "ggplot") 17 | unlink("ggmapTemp.png") 18 | }) 19 | -------------------------------------------------------------------------------- /tests/testthat/test-map_gist.R: -------------------------------------------------------------------------------- 1 | context("map_gist works correctly") 2 | 3 | test_that("map_gist works as expected", { 4 | skip_on_cran() 5 | skip_if_not_installed("scrubr") 6 | 7 | library("spocc") 8 | library("scrubr") 9 | 10 | spp <- c('Danaus plexippus','Accipiter striatus','Pinus contorta') 11 | dat <- occ(spp, from = 'gbif', limit = 30, has_coords = TRUE) 12 | df <- occ2df(dat) 13 | dat <- suppressWarnings(fix_names(df, "shortest")) 14 | 15 | # Define colors 16 | g <- suppressMessages( 17 | map_gist(dat, color = c('#976AAE','#6B944D','#BD5945'), 18 | browse = FALSE)) 19 | 20 | expect_is(g, "gist") 21 | expect_is(g$url, "character") 22 | expect_equal(g$description, "") 23 | }) 24 | -------------------------------------------------------------------------------- /tests/testthat/test-map_leaflet.R: -------------------------------------------------------------------------------- 1 | context("map_leaflet works correctly") 2 | 3 | test_that("Leaflet maps and geoJSON work", { 4 | skip_on_cran() 5 | 6 | library("spocc") 7 | spp <- c('Danaus plexippus','Accipiter striatus','Pinus contorta','Puma concolor', 'Ursus americanus','Gymnogyps californianus') 8 | dat <- occ(query = spp, from = 'gbif', limit = 25, gbifopts = list(hasCoordinate = TRUE)) 9 | gg <- map_leaflet(dat) 10 | expect_is(gg, "leaflet") 11 | expect_is(attr(gg$x, "leafletData"), "data.frame") 12 | expect_true("longitude" %in% names(attr(gg$x, "leafletData"))) 13 | }) 14 | -------------------------------------------------------------------------------- /tests/testthat/test-utils.R: -------------------------------------------------------------------------------- 1 | test_that("name param works to change taxon name used", { 2 | skip_on_cran() 3 | 4 | # prepare data 5 | # library(spocc) 6 | # res <- occ(query = "Lynx rufus californicus", from = "gbif") 7 | # spocc_df_1 <- occ2df(res) 8 | # save(spocc_df_1, file = "tests/testthat/spocc_df_1.rda", version = 2) 9 | 10 | load("spocc_df_1.rda") 11 | expect_is(check_name(spocc_df_1), "data.frame") 12 | # column not found 13 | expect_error(check_name(spocc_df_1, "foobar")) 14 | # column found 15 | expect_message((new1 <- check_name(spocc_df_1, "key"))) 16 | expect_named(new1, 17 | c("name_old", "longitude", "latitude", "prov", "date", "name")) 18 | # column name given as "name" 19 | ## no message given 20 | expect_message((new2 <- check_name(spocc_df_1, "name")), NA) 21 | expect_named(new2, 22 | c("name", "longitude", "latitude", "prov", "date", "key")) 23 | }) 24 | -------------------------------------------------------------------------------- /vignettes/mapr.Rmd: -------------------------------------------------------------------------------- 1 | --- 2 | title: "mapr introduction" 3 | author: "Scott Chamberlain" 4 | date: "2020-07-29" 5 | output: rmarkdown::html_vignette 6 | vignette: > 7 | %\VignetteIndexEntry{mapr introduction} 8 | %\VignetteEngine{knitr::rmarkdown} 9 | \usepackage[utf8]{inputenc} 10 | --- 11 | 12 | 13 | 14 | ## Load spocc and mapr 15 | 16 | 17 | ```r 18 | library("spocc") 19 | library("mapr") 20 | ``` 21 | 22 | ## Interactive maps 23 | 24 | ### Leaflet.js 25 | 26 | [Leaflet JS](http://leafletjs.com/) is an open source mapping library that can leverage various layers from multiple sources. Using the [`leaflet`](https://cran.r-project.org/package=leaflet) library, we can generate a local interactive map of species occurrence data. 27 | 28 | An example: 29 | 30 | 31 | ```r 32 | spp <- c('Danaus plexippus','Accipiter striatus','Pinus contorta') 33 | dat <- occ(query = spp, from = 'gbif', has_coords = TRUE, limit = 100) 34 | map_leaflet(dat) 35 | ``` 36 | 37 | ![leaflet](../man/figures/leaflet.png) 38 | 39 | ### Geojson map as a Github gist 40 | 41 | You can also create interactive maps via the `mapgist` function. You have to have a Github account to use this function. Github accounts are free though, and great for versioning and collaborating on code or papers. When you run the `map_gist` function it will ask for your Github username and password. You can alternatively store those in your `.Rprofile` file by adding entries for username (`options(github.username = 'username')`) and password (`options(github.password = 'password')`). 42 | 43 | 44 | ```r 45 | spp <- c('Danaus plexippus', 'Accipiter striatus', 'Pinus contorta') 46 | dat <- occ(query = spp, from = 'gbif', has_coords = TRUE, limit = 100) 47 | dat <- fixnames(dat) 48 | map_gist(dat, color = c("#976AAE", "#6B944D", "#BD5945")) 49 | ``` 50 | 51 | ![gist](../man/figures/gist.png) 52 | 53 | ## Static maps 54 | 55 | ### base plots 56 | 57 | Base plots, or the built in plotting facility in R accessed via `plot()`, is quite fast, but not easy or efficient to use, but are good for a quick glance at some data. 58 | 59 | 60 | ```r 61 | spnames <- c('Accipiter striatus', 'Setophaga caerulescens', 'Spinus tristis') 62 | out <- occ(query = spnames, from = 'gbif', has_coords = TRUE, limit = 100) 63 | map_plot(out, size = 1, pch = 10) 64 | ``` 65 | 66 | ![plot of chunk unnamed-chunk-5](../man/figures/unnamed-chunk-5-1.png) 67 | 68 | ### ggplot2 69 | 70 | `ggplot2` is a powerful package for making visualizations in R. Read more about it [here](https://cran.r-project.org/package=ggplot2). 71 | 72 | 73 | ```r 74 | dat <- occ(query = 'Lynx rufus californicus', from = 'gbif', has_coords = TRUE, limit = 200) 75 | map_ggplot(dat, map = "usa") 76 | ``` 77 | 78 | ![plot of chunk unnamed-chunk-6](../man/figures/unnamed-chunk-6-1.png) 79 | 80 | ### via dismo 81 | 82 | if that's your jam, though you might find `rgbif` easier 83 | 84 | 85 | ```r 86 | library("dismo") 87 | g <- gbif('Batrachoseps', 'luciae', geo = TRUE, end = 300) 88 | map_leaflet(g, lon = "lon", lat = "lat", name = "scientificName") 89 | ``` 90 | 91 | ![dismomap](http://f.cl.ly/items/2u2V0n0B3Y2y0p1d0f1M/Screen%20Shot%202016-01-22%20at%204.46.12%20PM.png) 92 | 93 | ## The supported inputs 94 | 95 | All functions take the following kinds of inputs: 96 | 97 | * An object of class `occdat`, from the package `spocc`. An object of 98 | this class is composed of many objects of class `occdatind` 99 | * An object of class `occdatind`, from the package `spocc` 100 | * An object of class `gbif`, from the package `rgbif` 101 | * An object of class `data.frame`. This data.frame can have any columns, but 102 | must include a column for taxonomic names (e.g., `name`), and for latitude 103 | and longitude (we guess your lat/long columns, starting with the default 104 | `latitude` and `longitude`). 105 | * An object of class `SpatialPoints` 106 | * An object of class `SpatialPointsDatFrame` 107 | -------------------------------------------------------------------------------- /vignettes/mapr.Rmd.og: -------------------------------------------------------------------------------- 1 | --- 2 | title: "mapr introduction" 3 | author: "Scott Chamberlain" 4 | date: "`r Sys.Date()`" 5 | output: rmarkdown::html_vignette 6 | vignette: > 7 | %\VignetteIndexEntry{mapr introduction} 8 | %\VignetteEngine{knitr::rmarkdown} 9 | \usepackage[utf8]{inputenc} 10 | --- 11 | 12 | ```{r echo=FALSE} 13 | knitr::opts_chunk$set( 14 | fig.path = "../man/figures/", 15 | comment = "#>", 16 | warning = FALSE, 17 | message = FALSE 18 | ) 19 | ``` 20 | 21 | ## Load spocc and mapr 22 | 23 | ```{r} 24 | library("spocc") 25 | library("mapr") 26 | ``` 27 | 28 | ## Interactive maps 29 | 30 | ### Leaflet.js 31 | 32 | [Leaflet JS](http://leafletjs.com/) is an open source mapping library that can leverage various layers from multiple sources. Using the [`leaflet`](https://cran.r-project.org/package=leaflet) library, we can generate a local interactive map of species occurrence data. 33 | 34 | An example: 35 | 36 | ```{r eval=FALSE} 37 | spp <- c('Danaus plexippus','Accipiter striatus','Pinus contorta') 38 | dat <- occ(query = spp, from = 'gbif', has_coords = TRUE, limit = 100) 39 | map_leaflet(dat) 40 | ``` 41 | 42 | ![leaflet](../man/figures/leaflet.png) 43 | 44 | ### Geojson map as a Github gist 45 | 46 | You can also create interactive maps via the `mapgist` function. You have to have a Github account to use this function. Github accounts are free though, and great for versioning and collaborating on code or papers. When you run the `map_gist` function it will ask for your Github username and password. You can alternatively store those in your `.Rprofile` file by adding entries for username (`options(github.username = 'username')`) and password (`options(github.password = 'password')`). 47 | 48 | ```{r eval=FALSE} 49 | spp <- c('Danaus plexippus', 'Accipiter striatus', 'Pinus contorta') 50 | dat <- occ(query = spp, from = 'gbif', has_coords = TRUE, limit = 100) 51 | dat <- fixnames(dat) 52 | map_gist(dat, color = c("#976AAE", "#6B944D", "#BD5945")) 53 | ``` 54 | 55 | ![gist](../man/figures/gist.png) 56 | 57 | ## Static maps 58 | 59 | ### base plots 60 | 61 | Base plots, or the built in plotting facility in R accessed via `plot()`, is quite fast, but not easy or efficient to use, but are good for a quick glance at some data. 62 | 63 | ```{r} 64 | spnames <- c('Accipiter striatus', 'Setophaga caerulescens', 'Spinus tristis') 65 | out <- occ(query = spnames, from = 'gbif', has_coords = TRUE, limit = 100) 66 | map_plot(out, size = 1, pch = 10) 67 | ``` 68 | 69 | ### ggplot2 70 | 71 | `ggplot2` is a powerful package for making visualizations in R. Read more about it [here](https://cran.r-project.org/package=ggplot2). 72 | 73 | ```{r} 74 | dat <- occ(query = 'Lynx rufus californicus', from = 'gbif', has_coords = TRUE, limit = 200) 75 | map_ggplot(dat, map = "usa") 76 | ``` 77 | 78 | ### via dismo 79 | 80 | if that's your jam, though you might find `rgbif` easier 81 | 82 | ```{r eval=FALSE} 83 | library("dismo") 84 | g <- gbif('Batrachoseps', 'luciae', geo = TRUE, end = 300) 85 | map_leaflet(g, lon = "lon", lat = "lat", name = "scientificName") 86 | ``` 87 | 88 | ![dismomap](http://f.cl.ly/items/2u2V0n0B3Y2y0p1d0f1M/Screen%20Shot%202016-01-22%20at%204.46.12%20PM.png) 89 | 90 | ## The supported inputs 91 | 92 | All functions take the following kinds of inputs: 93 | 94 | * An object of class `occdat`, from the package `spocc`. An object of 95 | this class is composed of many objects of class `occdatind` 96 | * An object of class `occdatind`, from the package `spocc` 97 | * An object of class `gbif`, from the package `rgbif` 98 | * An object of class `data.frame`. This data.frame can have any columns, but 99 | must include a column for taxonomic names (e.g., `name`), and for latitude 100 | and longitude (we guess your lat/long columns, starting with the default 101 | `latitude` and `longitude`). 102 | * An object of class `SpatialPoints` 103 | * An object of class `SpatialPointsDatFrame` 104 | --------------------------------------------------------------------------------