Browse Source

scale_fill_rradar()

master
boB Rudis 4 years ago
parent
commit
b480bc8016
No known key found for this signature in database GPG Key ID: 1D7529BE14E2BBA9
  1. 9
      DESCRIPTION
  2. 4
      NAMESPACE
  3. 3
      R/rradar-package.R
  4. 61
      R/scale-rradar.R
  5. 20
      README.Rmd
  6. 32
      README.md
  7. BIN
      man/figures/README-stars-1.png
  8. 46
      man/scale_fill_rradar.Rd

9
DESCRIPTION

@ -1,8 +1,8 @@
Package: rradar
Type: Package
Title: Animate NOAA NWSRadar Images by Station Id
Version: 0.2.0
Date: 2019-12-06
Version: 0.3.0
Date: 2019-12-15
Authors@R: c(
person("Bob", "Rudis", email = "bob@rud.is", role = c("aut", "cre"),
comment = c(ORCID = "0000-0001-5670-2640")),
@ -28,6 +28,9 @@ Imports:
magrittr,
stars,
sf,
utils
utils,
scales,
ggplot2,
viridis
Roxygen: list(markdown = TRUE)
RoxygenNote: 7.0.2

4
NAMESPACE

@ -4,11 +4,15 @@ export("%>%")
export(animate_conus_mosaic)
export(animate_radar)
export(latest_radar)
export(scale_fill_rradar)
export(stations)
import(httr)
import(magick)
import(rvest)
importFrom(ggplot2,scale_fill_gradientn)
importFrom(magrittr,"%>%")
importFrom(scales,rescale)
importFrom(sf,st_set_crs)
importFrom(stars,read_stars)
importFrom(utils,download.file)
importFrom(viridis,viridis_pal)

3
R/rradar-package.R

@ -12,4 +12,7 @@
#' @importFrom stars read_stars
#' @importFrom sf st_set_crs
#' @importFrom utils download.file
#' @importFrom scales rescale
#' @importFrom ggplot2 scale_fill_gradientn
#' @importFrom viridis viridis_pal
"_PACKAGE"

61
R/scale-rradar.R

@ -0,0 +1,61 @@
#' Continuous fill scale for use with NWS raster radar data
#'
#' The dBZ values associated with the base reflectivity radar images/data represent
#' the strength of the energy returned to the radar from the precipitation. Values lower than 20 dBZ
#' are generally when there is actual light rain. The NWS tends to use a light gray alpha for values
#' below ~5 dBZ since there are particles in the air but they really aren't something that constitutes
#' precipitation.\cr
#' \cr
#' This `{ggplot2}` continuous fill scale is an adaptation of the `{viridis}` color palette scales
#' where the viridis scale mappings only start when dBZ values are around 5 dBZ. Values lower than that
#' receive light gray with a 5% alpha (see the `low_dbz` parameter).
#'
#' @param option,direction passed on to [viridis::viridis_pal()]; defaults to "`plasma`" and `-1`, respectively.
#' @param low_dbz color to be uses the dbZ values are lower than ~5 dBZ. Defaults to "`#00000010`"
#' @param name The name of the scale
#' @param na.value Missing values will be replaced with this value. Defaults to a fully transparent color.
#' @param limits scale limits; defaults to NWS dBZ limits [-30, 70]
#' @param breaks scale breaks; defaults to the NWS every 5 sequence from -30 to 70
#' @param ... passed on to [ggplot2::scale_fill_gradientn()]
#' @return a `{ggplot2}` continuous fill scale
#' @export
#' @examples \dontrun{
#' library(sf)
#' library(stars)
#' library(rradar)
#' library(rnaturalearth)
#' library(hrbrthemes)
#' library(tidyverse)
#'
#' us <- ne_states(country = "united states of america", returnclass = "sf")
#'
#' ne_radar <- latest_radar("northeast")
#'
#' ne_us <- st_crop(us, st_bbox(ne_radar))
#'
#' ggplot() +
#' geom_sf(data = ne_us, size = 0.125, fill = '#fefefe') +
#' geom_stars(data = ne_radar) +
#' coord_sf(datum = NA) +
#' scale_fill_rradar() +
#' labs(
#' x = NULL, y = NULL,
#' title = "NWS Radar Mosaic — Northeast Sector",
#' subtitle = "1538 UTC 2019-12-07"
#' ) +
#' theme_ipsum_es(grid="") +
#' theme(legend.key.height = unit(5, "lines"))
#' }
scale_fill_rradar <- function(option = "plasma", direction = -1, name = "dBZ", na.value = "#00000000",
limits = c(-30, 70), breaks = seq(-30, 70, 5), low_dbz = "#00000010", ...) {
dbz <- seq(-30, 70, 1)
dbzrs <- scales::rescale(dbz)
ggplot2::scale_fill_gradientn(
colours = c(rep(low_dbz, 38), viridis::viridis_pal(option = option, direction = direction)(63)),
values = dbzrs, limits = limits, breaks = breaks,
na.value = na.value, name = name, ...
)
}

20
README.Rmd

@ -61,31 +61,37 @@ animate_radar("VBX")
animate_conus_mosaic("small")
```
```{r stars}
```{r stars, fig.width=800/96, fig.height=800/96, cache=TRUE}
library(sf)
library(stars)
library(rradar)
library(viridis)
library(rnaturalearth)
library(hrbrthemes)
library(tidyverse)
us <- ne_states(country = "united states of america", returnclass = "sf")
wrld <- ne_countries(scale = "medium", country = c("united states of america", "canada"),
continent = "north america", returnclass = "sf")
us <- ne_states("united states of america", returnclass = "sf")
ne_radar <- latest_radar("northeast")
ne_us <- st_crop(us, st_bbox(ne_radar))
wrld <- st_crop(wrld, st_bbox(ne_radar))
us <- st_crop(us, st_bbox(ne_radar))
ggplot() +
geom_sf(data = ne_us, size = 0.125) +
geom_sf(data = wrld, size = 0.125, fill = '#fefefe') +
geom_sf(data = us, size = 0.125, fill = '#fefefe') +
geom_stars(data = ne_radar) +
scale_fill_viridis_c(name = "dBZ", na.value = "#00000000") +
coord_sf(datum = NA) +
scale_fill_rradar() +
labs(
x = NULL, y = NULL,
title = "NWS Radar Mosaic — Northeast Sector",
subtitle = "1538 UTC 2019-12-07"
subtitle = "1758 UTC 2019-12-15"
) +
theme_ipsum_es(grid="")
theme_ipsum_es(grid="") +
theme(legend.key.height = unit(5, "lines"))
```
## rradar Metrics

32
README.md

@ -32,6 +32,8 @@ The following functions are implemented:
station
- `latest_radar`: Read latest NWS regional or ConUS radar mosaics as a
stars object
- `scale_fill_rradar`: Continuous fill scale for use with NWS raster
radar data
- `stations`: NOAA U.S. Radar Stations
## Installation
@ -60,7 +62,7 @@ library(rradar)
# current version
packageVersion("rradar")
## [1] '0.2.0'
## [1] '0.3.0'
```
### Stations
@ -114,37 +116,43 @@ animate_conus_mosaic("small")
library(sf)
library(stars)
library(rradar)
library(viridis)
library(rnaturalearth)
library(hrbrthemes)
library(tidyverse)
us <- ne_states(country = "united states of america", returnclass = "sf")
wrld <- ne_countries(scale = "medium", country = c("united states of america", "canada"),
continent = "north america", returnclass = "sf")
us <- ne_states("united states of america", returnclass = "sf")
ne_radar <- latest_radar("northeast")
ne_us <- st_crop(us, st_bbox(ne_radar))
wrld <- st_crop(wrld, st_bbox(ne_radar))
us <- st_crop(us, st_bbox(ne_radar))
ggplot() +
geom_sf(data = ne_us, size = 0.125) +
geom_sf(data = wrld, size = 0.125, fill = '#fefefe') +
geom_sf(data = us, size = 0.125, fill = '#fefefe') +
geom_stars(data = ne_radar) +
scale_fill_viridis_c(name = "dBZ", na.value = "#00000000") +
coord_sf(datum = NA) +
scale_fill_rradar() +
labs(
x = NULL, y = NULL,
title = "NWS Radar Mosaic — Northeast Sector",
subtitle = "1538 UTC 2019-12-07"
subtitle = "1758 UTC 2019-12-15"
) +
theme_ipsum_es(grid="")
theme_ipsum_es(grid="") +
theme(legend.key.height = unit(5, "lines"))
```
<img src="man/figures/README-stars-1.png" width="672" />
<img src="man/figures/README-stars-1.png" width="800" />
## rradar Metrics
| Lang | \# Files | (%) | LoC | (%) | Blank lines | (%) | \# Lines | (%) |
| :--- | -------: | ---: | --: | ---: | ----------: | ---: | -------: | ---: |
| R | 8 | 0.89 | 229 | 0.87 | 34 | 0.56 | 63 | 0.62 |
| Rmd | 1 | 0.11 | 34 | 0.13 | 27 | 0.44 | 39 | 0.38 |
| Lang | \# Files | (%) | LoC | (%) | Blank lines | (%) | \# Lines | (%) |
| :--- | -------: | --: | --: | ---: | ----------: | ---: | -------: | ---: |
| R | 9 | 0.9 | 239 | 0.86 | 37 | 0.58 | 114 | 0.75 |
| Rmd | 1 | 0.1 | 40 | 0.14 | 27 | 0.42 | 39 | 0.25 |
## Code of Conduct

BIN
man/figures/README-stars-1.png

Binary file not shown.

Before

Width:  |  Height:  |  Size: 40 KiB

After

Width:  |  Height:  |  Size: 88 KiB

46
man/scale_fill_rradar.Rd

@ -0,0 +1,46 @@
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/scale-rradar.R
\name{scale_fill_rradar}
\alias{scale_fill_rradar}
\title{Continuous fill scale for use with NWS raster radar data}
\usage{
scale_fill_rradar(
option = "plasma",
direction = -1,
name = "dBZ",
na.value = "#00000000",
limits = c(-30, 70),
breaks = seq(-30, 70, 5),
low_dbz = "#00000010",
...
)
}
\arguments{
\item{option, direction}{passed on to \code{\link[viridis:viridis_pal]{viridis::viridis_pal()}}; defaults to "\code{plasma}" and \code{-1}, respectively.}
\item{name}{The name of the scale}
\item{na.value}{Missing values will be replaced with this value. Defaults to a fully transparent color.}
\item{limits}{scale limits; defaults to NWS dBZ limits \link{-30, 70}}
\item{breaks}{scale breaks; defaults to the NWS every 5 sequence from -30 to 70}
\item{low_dbz}{color to be uses the dbZ values are lower than ~5 dBZ. Defaults to "\verb{#00000010}"}
\item{...}{passed on to \code{\link[ggplot2:scale_fill_gradientn]{ggplot2::scale_fill_gradientn()}}}
}
\value{
a \code{{ggplot2}} continuous fill scale
}
\description{
The dBZ values associated with the base reflectivity radar images/data represent
the strength of the energy returned to the radar from the precipitation. Values lower than 20 dBZ
are generally when there is actual light rain. The NWS tends to use a light gray alpha for values
below ~5 dBZ since there are particles in the air but they really aren't something that constitutes
precipitation.\cr
\cr
This \code{{ggplot2}} continuous fill scale is an adaptation of the \code{{viridis}} color palette scales
where the viridis scale mappings only start when dBZ values are around 5 dBZ. Values lower than that
receive light gray with a 5\% alpha (see the \code{low_dbz} parameter).
}
Loading…
Cancel
Save