Skip to content

Commit

Permalink
merge main
Browse files Browse the repository at this point in the history
Merge branch 'master' of https://github.com/davidcarslaw/openairmaps

# Conflicts:
#	R/network_networkMap.R
  • Loading branch information
jack-davison committed Apr 20, 2024
2 parents 903e09d + 68ae72c commit e8f2b34
Show file tree
Hide file tree
Showing 32 changed files with 948 additions and 229 deletions.
1 change: 1 addition & 0 deletions .Rbuildignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@ README_cache
^pkgdown$
^CODE_OF_CONDUCT\.md$
^codecov\.yml$
man/figures/
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Type: Package
Package: openairmaps
Title: Create Maps of Air Pollution Data
Version: 0.8.1.9001
Version: 0.8.1.9002
Authors@R: c(
person("Jack", "Davison", , "jack.davison@ricardo.com", role = c("cre", "aut")),
person("David", "Carslaw", , "david.carslaw@york.ac.uk", role = "aut")
Expand Down
4 changes: 4 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

* BREAKING: The `names` and `cols` arguments of `buildPopup()` have been coalesced into a single `columns` argument for less verbose function usage.

* BREAKING: The `collapse.control` argument has been renamed to `control.collapsed` and the `draw.legend` argument to `legend`. This is to allow these options to sit more nicely with their new argument family members - `legend.title`, `legend.title.autotext`, `legend.position`, and so on.

## New features

* The `polarMapStatic()` family of functions have been combined with the `polarMap()` family, with static maps available to be accessed using the `static` argument. The `polarMapStatic()` family are therefore deprecated, and will later be removed from `{openairmaps}`. The justification for this is as follows:
Expand All @@ -22,6 +24,8 @@

* The `crs` argument has been added to the `polarMap()` and `polarMapStatic()` families and to `searchNetwork()`. This argument allows for users to specify that their data is using an alternative coordinate system to the standard longitude/latitude (e.g., the British National Grid CRS). Alternate CRS will be re-projected to longitude/latitude for plotting as this is expected by `{leaflet}` / `{ggspatial}`.

* Users now have greater control over the positions of legends and layer control menus, and the titles of legends, throughout `{openairmaps}` functions, including the `polarMap()` family, `trajMap()` family, and `networkMap()`.

* Popups for the dynamic `polarMap()` family will now be near the top of the plot rather than the centre. This will obscure less of the plot itself while the marker is visible.

* Two examples of the use of `{openairmaps}` with `{shiny}` have been added to the package. Run `shiny::runExample(package = "openairmaps")` to view these.
Expand Down
35 changes: 35 additions & 0 deletions R/traj_data.R → R/data.R
Original file line number Diff line number Diff line change
@@ -1,3 +1,38 @@
#' Example data for polar mapping functions
#'
#' The `polar_data` dataset is provided as an example dataset as part of
#' the `openairmaps` package. The dataset contains hourly measurements of
#' air pollutant concentrations, location and meteorological data.
#'
#' `polar_data` is supplied with the `openairmaps` package as an
#' example dataset for use with documented examples.
#'
#' @name polar_data
#' @docType data
#' @format Data frame with example data from four sites in London in 2009.
#' \describe{
#' \item{date}{The date and time of the measurement}
#' \item{nox, no2, pm2.5, pm10}{Pollutant concentrations}
#' \item{site}{The site name. Useful for use with the `popup` and `label` arguments in `openairmaps` functions.}
#' \item{latitude, longitude}{Decimal latitude and longitude of the sites.}
#' \item{site.type}{Site type of the site (either "Urban Traffic" or "Urban Background").}
#' \item{wd}{Wind direction, in degrees from North, as a numeric vector.}
#' \item{ws}{Wind speed, in m/s, as numeric vector.}
#' \item{visibility}{The visibility in metres.}
#' \item{air_temp}{Air temperature in degrees Celcius.}
#' }
#' @source `polar_data` was compiled from data using the
#' [openair::importAURN()] function from the `openair` package with
#' meteorological data from the `worldmet` package.
#'
#' @keywords datasets
#' @examples
#'
#' # basic structure
#' head(polar_data)
#'
NULL

#' Example data for trajectory mapping functions
#'
#' The `traj_data` dataset is provided as an example dataset as part of the
Expand Down
64 changes: 45 additions & 19 deletions R/network_networkMap.R
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
#' "aurn")` and the "AURN" layer control group when `source = c("aurn",
#' "saqn")`.
#'
#'
#' @param source *One or more UK or European monitoring networks.*
#'
#' *default:* `"aurn"`
Expand Down Expand Up @@ -62,7 +61,7 @@
#'
#' @param cluster *Cluster markers together when zoomed out?*
#'
#' *default:* `FALSE`
#' *default:* `TRUE`
#'
#' When `cluster = TRUE`, markers are clustered together. This may be useful
#' for sources like "kcl" where there are many markers very close together.
Expand All @@ -81,20 +80,36 @@
#' own using a named vector (e.g., `c("Default" = "OpenStreetMap", "Satellite"
#' = "Esri.WorldImagery")`)
#'
#' @param draw.legend *Draw a shared legend?*
#' @param legend *Draw a shared legend?*
#'
#' *default:* `TRUE` | *scope:* dynamic & static
#' *default:* `TRUE`
#'
#' When multiple `source`s are defined, should a shared legend be created at
#' the side of the map?
#'
#' @param collapse.control *Show the layer control as a collapsed?*
#' @param legend.position *Position of the legend*
#'
#' *default:* `"topright"`
#'
#' *default:* `FALSE` | *scope:* dynamic
#' Where should the shared legend be placed? One of "topleft", "topright",
#' "bottomleft" or "bottomright". Passed to the `position` argument of
#' [leaflet::addLayersControl()].
#'
#' @param control.collapsed *Show the layer control as a collapsed?*
#'
#' *default:* `FALSE`
#'
#' Should the "layer control" interface be collapsed? If `TRUE`, users will
#' have to hover over an icon to view the options.
#'
#' @param control.position *Position of the layer control menu*
#'
#' *default:* `"topright"`
#'
#' Where should the "layer control" interface be placed? One of "topleft",
#' "topright", "bottomleft" or "bottomright". Passed to the `position`
#' argument of [leaflet::addLayersControl()].
#'
#' @returns A leaflet object.
#' @export
#' @family uk air quality network mapping functions
Expand All @@ -119,8 +134,10 @@ networkMap <-
"Default" = "OpenStreetMap",
"Satellite" = "Esri.WorldImagery"
),
draw.legend = TRUE,
collapse.control = FALSE) {
legend = TRUE,
legend.position = "topright",
control.collapsed = FALSE,
control.position = "topright") {
# if year isn't provided, use current year
if (is.null(year)) {
year <- lubridate::year(Sys.Date())
Expand Down Expand Up @@ -161,7 +178,7 @@ networkMap <-
"#ff8ee9"
)
) %>%
dplyr::mutate(colour2 = ifelse(.data$colour == "white", "gray", "white"))
dplyr::mutate(colour2 = ifelse(.data$colour == "#FFFFFF", "#303030", "#FFFFFF"))

# read in data
meta <-
Expand Down Expand Up @@ -228,6 +245,7 @@ networkMap <-

# sort out control
if (!is.null(control)) {
control.position <- check_legendposition(control.position, static = FALSE)
if (!control %in% names(meta)) {
trycols <- names(meta)[!names(meta) %in%
c(
Expand Down Expand Up @@ -297,9 +315,10 @@ networkMap <-
label = dat[["site"]],
clusterOptions = clusteropts,
icon = leaflet::makeAwesomeIcon(
library = "fa",
icon = "info-circle",
markerColor = dat$colour,
iconColor = dat$colour2,
icon = "info-sign"
iconColor = dat$colour2
)
)
}
Expand All @@ -309,7 +328,8 @@ networkMap <-
map <-
leaflet::addLayersControl(
map,
options = leaflet::layersControlOptions(collapsed = collapse.control, autoZIndex = FALSE),
position = control.position,
options = leaflet::layersControlOptions(collapsed = control.collapsed, autoZIndex = FALSE),
baseGroups = quickTextHTML(sort(control_vars)),
overlayGroups = names(provider)
) %>%
Expand All @@ -318,7 +338,8 @@ networkMap <-
map <-
leaflet::addLayersControl(
map,
options = leaflet::layersControlOptions(collapsed = collapse.control, autoZIndex = FALSE),
position = control.position,
options = leaflet::layersControlOptions(collapsed = control.collapsed, autoZIndex = FALSE),
baseGroups = quickTextHTML(sort(control_vars))
)
}
Expand All @@ -327,7 +348,8 @@ networkMap <-
map <-
leaflet::addLayersControl(
map,
options = leaflet::layersControlOptions(collapsed = collapse.control, autoZIndex = FALSE),
position = control.position,
options = leaflet::layersControlOptions(collapsed = control.collapsed, autoZIndex = FALSE),
overlayGroups = quickTextHTML(sort(control_vars)),
baseGroups = names(provider)
) %>%
Expand All @@ -336,7 +358,8 @@ networkMap <-
map <-
leaflet::addLayersControl(
map,
options = leaflet::layersControlOptions(collapsed = collapse.control, autoZIndex = FALSE),
position = control.position,
options = leaflet::layersControlOptions(collapsed = control.collapsed, autoZIndex = FALSE),
overlayGroups = quickTextHTML(sort(control_vars))
)
}
Expand All @@ -356,29 +379,32 @@ networkMap <-
label = dat[["site"]],
clusterOptions = clusteropts,
icon = leaflet::makeAwesomeIcon(
library = "fa",
icon = "info-circle",
markerColor = dat$colour,
iconColor = dat$colour2,
icon = "info-sign"
iconColor = dat$colour2
)
)

if (length(provider) > 1) {
map <-
leaflet::addLayersControl(
map,
options = leaflet::layersControlOptions(collapsed = collapse.control, autoZIndex = FALSE),
position = control.position,
options = leaflet::layersControlOptions(collapsed = control.collapsed, autoZIndex = FALSE),
baseGroups = names(provider)
) %>%
leaflet::hideGroup(group = names(provider)[[-1]])
}
}

# multiple sources - add legend
if (length(source) > 1 & draw.legend) {
if (length(source) > 1 & legend) {
map <-
leaflet::addLegend(
map,
opacity = 1,
position = check_legendposition(legend.position, static = FALSE),
title = "Network",
colors = cols$realcolour,
labels = paste0("<span style='line-height:1.6'>", cols$network, "</span>")
Expand Down
38 changes: 31 additions & 7 deletions R/polar_annulusMap.R
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,20 @@ annulusMap <- function(data,
cols = "turbo",
alpha = 1,
key = FALSE,
draw.legend = TRUE,
collapse.control = FALSE,
legend = TRUE,
legend.position = NULL,
legend.title = NULL,
legend.title.autotext = TRUE,
control.collapsed = FALSE,
control.position = "topright",
d.icon = 200,
d.fig = 3.5,
static = FALSE,
static.nrow = NULL,
...) {
# check basemap providers are valid
provider <- check_providers(provider, static)
legend.position <- check_legendposition(legend.position, static)

# check for old facet/control opts
type <- type %||% check_facet_control(...)
Expand Down Expand Up @@ -208,7 +213,15 @@ annulusMap <- function(data,
)

# create colorbar if limits specified
if (!all(is.na(theLimits)) & draw.legend) {
if (!all(is.na(theLimits)) & legend) {
legend.title <-
create_legend_title(
static = static,
legend.title.autotext = legend.title.autotext,
legend.title = legend.title,
str = paste(pollutant, collapse = ", ")
)

map <-
map +
ggplot2::geom_point(
Expand All @@ -220,7 +233,8 @@ annulusMap <- function(data,
limits = theLimits,
colours = openair::openColours(scheme = cols)
) +
ggplot2::labs(color = openair::quickText(paste(pollutant, collapse = ", ")))
ggplot2::labs(color = legend.title) +
ggplot2::theme(legend.position = legend.position)
}
}

Expand All @@ -237,19 +251,29 @@ annulusMap <- function(data,
popup,
label,
split_col,
collapse.control
control.collapsed,
control.position
)

# add legend if limits are set
if (!all(is.na(theLimits)) & draw.legend) {
if (!all(is.na(theLimits)) & legend) {
legend.title <-
create_legend_title(
static = static,
legend.title.autotext = legend.title.autotext,
legend.title = legend.title,
str = paste(pollutant, collapse = ",<br>")
)

map <-
leaflet::addLegend(
map,
title = quickTextHTML(paste(pollutant, collapse = ",<br>")),
title = legend.title,
pal = leaflet::colorNumeric(
palette = openair::openColours(scheme = cols),
domain = theLimits
),
position = legend.position,
values = theLimits
)
}
Expand Down
34 changes: 0 additions & 34 deletions R/polar_data.R

This file was deleted.

Loading

0 comments on commit e8f2b34

Please sign in to comment.