Skip to content

Commit

Permalink
Update function arg names for plots
Browse files Browse the repository at this point in the history
  • Loading branch information
Robinlovelace committed Aug 17, 2021
1 parent 7e3a64d commit 7651276
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 77 deletions.
116 changes: 62 additions & 54 deletions R/plot_slope.R
Original file line number Diff line number Diff line change
@@ -1,76 +1,83 @@
#' Plot slope data for a 3d linestring with base R graphics
#'
#' @inheritParams slope_raster
#' @inheritParams plot_dz
#' @inheritParams sequential_dist
#' @param route_xyz An sf linestring with x, y and z coordinates,
#' representing a route or other linear object.
#' @param fill Should the profile be filled? `TRUE` by default
#' @param horiz Should the legend be horizontal (`FALSE` by default)
#' @param pal Color palette to use, `colorspace::diverging_hcl` by default.
#' @param x Keyword, one of "bottomright", "bottom",
#' "bottomleft", "left", "topleft", "top", "topright", "right" and "center"
#' @param col Line colour, black by default
#' @param cex Legend size, 0.9 by default
#' @param bg Legend background colour, `grDevices::rgb(1, 1, 1, 0.8)` by default.
#' @param title Title of the legend
#' @param brks Breaks in colour palette to show.
#' `c(1, 3, 6, 10, 20, 40, 100)` by default.
#' @param seq_brks Sequence of breaks to show in legend.
#' Includes negative numbers and omits zero by default
#' @param ncol Number of columns in legend
#' @param ... Additional parameters to pass to legend
#' @inheritParams slope_raster
#' @inheritParams sequential_dist
#'
#' @export
#' @examples
#' plot_slope(lisbon_route_3d)
#' route_xyz = lisbon_road_segment_3d
#' plot_slope(route_xyz)
plot_slope = function(route_xyz,
lonlat = sf::st_is_longlat(route_xyz),
fill = TRUE,
horiz = FALSE,
p = colorspace::diverging_hcl,
...,
x = "top",
col = "black",
cex = 0.9,
bg = grDevices::rgb(1, 1, 1, 0.8),
title = "Slope colors (percentage gradient)",
s = 3:18,
ncol = 4) {
#' plot_slope(route_xyz, brks = c(1, 2, 4, 8, 16, 30))
#' plot_slope(route_xyz, s = 5:8)
plot_slope = function(
route_xyz,
lonlat = sf::st_is_longlat(route_xyz),
fill = TRUE,
horiz = FALSE,
pal =colorspace::diverging_hcl,
x = "top",
col = "black",
cex = 0.9,
bg = grDevices::rgb(1, 1, 1, 0.8),
title = "Slope colors (percentage gradient)",
brks = c(3, 6, 10, 20, 40, 100),
seq_brks = seq(from = 3, to = length(brks) * 2 - 2),
ncol = 4,
...
) {
dz = distance_z(route_xyz, lonlat = lonlat)
plot_dz(dz$d, dz$z, fill = fill)
plot_dz(dz$d, dz$z, seq_brks = seq_brks, brks = brks, ...)
}
#' Plot a digital elevation profile based on xyz data
#'
#' @param d Cumulative distance
#' @param z Elevations at points across a linestring
#' @param p Color palette to use
#' @param fill Should the profile be filled? `TRUE` by default
#' @param x Keyword, one of "bottomright", "bottom",
#' "bottomleft", "left", "topleft", "top", "topright", "right" and "center"
#' @param col Line colour
#' @param cex Legend size
#' @param bg Legend background colour
#' @param title Title of the legend
#' @param brks Breaks in colour palette to show.
#' `c(1, 3, 6, 10, 20, 40, 100)` by default.
#' @param s Sequence of numbers to show in legend
#' @param ncol Number of columns in legend
#' @param horiz Should the legend be horizontal (`FALSE` by default)
#' @param ... Additional parameters to pass to legend
#' @inherit plot_slope
#' @examples
#' route_xyz = lisbon_road_segment_3d
#' m = sf::st_coordinates(route_xyz)
#' d = cumsum(sequential_dist(m, lonlat = FALSE))
#' d = c(0, d)
#' z = m[, 3]
#' # not exported
#' # plot_dz(d, z)
plot_dz = function(d,
z,
fill = TRUE,
horiz = FALSE,
p = colorspace::diverging_hcl,
...,
x = "top",
col = "black",
cex = 0.9,
bg = grDevices::rgb(1, 1, 1, 0.8),
title = "Slope colors (percentage gradient)",
brks = c(3, 6, 10, 20, 40, 100),
s = NULL,
ncol = 4) {
#' slopes:::plot_dz(d, z, brks = c(3, 6, 10, 20, 40, 100))
plot_dz = function(
d,
z,
fill = TRUE,
horiz = FALSE,
pal = colorspace::diverging_hcl,
...,
x = "top",
col = "black",
cex = 0.9,
bg = grDevices::rgb(1, 1, 1, 0.8),
title = "Slope colors (percentage gradient)",
brks = NULL,
seq_brks = NULL,
ncol = 4
) {
graphics::plot(d, z, type = "l", col = "brown", lwd = 2)
if (fill) {
b = make_breaks(brks)
pal = make_pal(p, b)
pal = make_pal(pal, b)
g = slope_vector(x = d, elevations = z)
colz = make_colz(g, b, pal)
lapply(seq(d)[-(length(d))], function(i) {
Expand All @@ -82,7 +89,8 @@ plot_dz = function(d,
)
})
graphics::lines(d, z, col = col, lwd = 2)
if(is.null(s)) s = seq(from = 3, to = length(b) - 2)
if(is.null(seq_brks)) seq_brks = seq(from = 3, to = length(b) - 2)
s = c(seq_brks[-(length(seq_brks) / 2) -1], max(seq_brks) + 1)
graphics::legend(x = x, legend = b[s] * 100, fill = pal[s],
..., bg = bg, title = title, horiz = horiz,
ncol = ncol, cex = cex)
Expand All @@ -99,7 +107,7 @@ distance_z = function(route_xyz, lonlat) {

make_breaks = function(brks) {
n = brks
n = c(-rev(n), (n))
n = c(-rev(n), 0, (n))
b = n / 100
b
}
Expand All @@ -113,11 +121,11 @@ make_colz = function(g, b, pal) {
as.character(colz)
}

make_pal = function(p, b) {
if (identical(p, colorspace::diverging_hcl)) {
pal = p(n = length(b) - 1, palette = "Green-Brown")
make_pal = function(pal, b) {
if (identical(pal, colorspace::diverging_hcl)) {
pal = pal(n = length(b) - 1, palette = "Green-Brown")
} else {
pal = p(n = length(b) - 1)
pal = pal(n = length(b) - 1)
}
pal
}
20 changes: 10 additions & 10 deletions man/plot_dz.Rd

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

29 changes: 18 additions & 11 deletions man/plot_slope.Rd

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

4 changes: 2 additions & 2 deletions tests/testthat/test-plots.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ test_that("plotting functions work", {
b = make_breaks(brks)
expect_equal(
b,
c(-0.4, -0.2, -0.1, -0.06, -0.03, 0.03, 0.06, 0.1, 0.2, 0.4)
c(-0.4, -0.2, -0.1, -0.06, -0.03, 0, 0.03, 0.06, 0.1, 0.2, 0.4)
)
g = c(-0.05, -0.04, -0.03, -0.06, -0.02, 0.01, 0.02)
pal = c("#004B40", "#007D6F", "#00AB9C", "#6CCFC3", "#BAEAE4", "#F6F6F6",
"#F3DEC6", "#DBB98C", "#B98E45")
colz = make_colz(g = g, b = b, pal = pal)
colz = make_colz(g = g, b = b[b != 0], pal = pal)
expect_equal(
colz,
c("#6CCFC3", "#6CCFC3", "#6CCFC3", "#00AB9C", "#BAEAE4", "#BAEAE4",
Expand Down

0 comments on commit 7651276

Please sign in to comment.