Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 28 additions & 23 deletions R/palette.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,42 +2,47 @@

#' Recommended slope colours (dark green to dark red)
#'
#' A character vector of twelve hex colours for use with \code{\link{slope_breaks}}
#' and \code{\link{plot_slope}}. Colours run from dark green (flat) through amber
#' to dark red (steep), covering both downhill (negative) and uphill (positive)
#' slope directions — six colours per direction.
#' A character vector of six hex colours ranging from dark green (flat, \eqn{\le}3\%)
#' to dark red (steep, >20\%), representing increasing slope severity.
#' Use directly with \code{\link{slope_breaks}} and \code{cut()} to colour
#' segments by slope proportion (as returned by \code{\link{slope_xyz}}).
#'
#' @format A character vector of length 12.
#' @seealso \code{\link{slope_breaks}}, \code{\link{slopes_palette}}
#' @format A character vector of length 6.
#' @seealso \code{\link{slope_breaks}}, \code{\link{plot_slope}}
#' @examples
#' slope_colors
#' \donttest{
#' route_xyz <- elevation_add(lisbon_route, dem = dem_lisbon())
#' plot_slope(route_xyz, pal = slope_colors, brks = slope_breaks)
#'
#' # 1. Colour segments by steepness (ignoring direction) using abs()
#' segs <- route_to_segments(route_xyz)
#' segs$slope <- slope_xyz(segs)
#' col_idx <- cut(abs(segs$slope), breaks = slope_breaks,
#' labels = FALSE, include.lowest = TRUE)
#' plot(sf::st_geometry(segs), col = slope_colors[col_idx], lwd = 3)
#'
#' # 2. Symmetric palette for plot_slope(): green = gentle, red = steep either way
#' plot_slope(route_xyz, pal = c(rev(slope_colors), slope_colors))
#' }
#' @export
slope_colors <- c(
"#730000", "#A80000", "#E60000", "#FFAA00", "#70A800", "#267300", # downhill
"#267300", "#70A800", "#FFAA00", "#E60000", "#A80000", "#730000" # uphill
)
slope_colors <- c("#267300", "#70A800", "#FFAA00", "#E60000", "#A80000", "#730000")

#' Recommended slope break thresholds
#' Recommended slope break thresholds (as proportions)
#'
#' A numeric vector of seven break points in **proportion units**
#' (e.g. 0.05 = 5\%), including \code{0} and \code{Inf} as outer bounds.
#' Defines six slope severity classes for use with \code{\link{slope_colors}}:
#' 0--3\%, 3--5\%, 5--8\%, 8--10\%, 10--20\%, >20\%.
#'
#' A numeric vector of six positive break points in **percentage units**
#' (e.g. 5 = 5\%), without \code{0} or \code{Inf}.
#' \code{\link{plot_slope}} uses these to build a symmetric set of 12 intervals
#' (downhill and uphill), matched to the 12 colours in \code{\link{slope_colors}}.
#' Use with \code{cut(slope_values, breaks = slope_breaks)} where slope values
#' are proportions as returned by \code{\link{slope_xyz}}.
#'
#' @format A numeric vector of length 6.
#' @seealso \code{\link{slope_colors}}, \code{\link{slopes_palette}}
#' @format A numeric vector of length 7.
#' @seealso \code{\link{slope_colors}}, \code{\link{plot_slope}}
#' @examples
#' slope_breaks
#' \donttest{
#' route_xyz <- elevation_add(lisbon_route, dem = dem_lisbon())
#' plot_slope(route_xyz, pal = slope_colors, brks = slope_breaks)
#' }
#' @export
slope_breaks <- c(3, 5, 8, 10, 20, 100)
slope_breaks <- c(0, 0.03, 0.05, 0.08, 0.10, 0.20, Inf)


#' Get color palette for slopes visualization
Expand Down
22 changes: 11 additions & 11 deletions man/slope_breaks.Rd

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

23 changes: 16 additions & 7 deletions man/slope_colors.Rd

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

18 changes: 11 additions & 7 deletions vignettes/slopes.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -132,9 +132,9 @@ slope
You can visualize the elevation profile of a 3D linestring using `plot_slope()`:

```{r}
# Use the recommended green-to-red palette and matching break thresholds

plot_slope(sf_linestring_xyz_local, pal = slope_colors, brks = slope_breaks)
# For plot_slope() use a symmetric palette: steep slopes are red on both sides
# (downhill and uphill), gentle slopes in the middle are green.
plot_slope(sf_linestring_xyz_local, pal = c(rev(slope_colors), slope_colors))
```

## Working with segments
Expand All @@ -154,9 +154,12 @@ summary(lisbon_route_segments_xyz$slope)
```

```{r}
# Segments are coloured by steepness (absolute slope), regardless of direction
# (uphill or downhill). slope_breaks are in proportions, matching slope_xyz() output.
col_idx <- cut(abs(lisbon_route_segments_xyz$slope),
breaks = slope_breaks, labels = FALSE, include.lowest = TRUE)
plot(st_geometry(lisbon_route_segments_xyz),
col = slope_colors[7:12][cut(abs(lisbon_route_segments_xyz$slope) * 100,
breaks = slope_breaks, labels = FALSE, include.lowest = TRUE)],
col = slope_colors[col_idx],
lwd = 3, main = "Slope by vertex segments"
)
```
Expand All @@ -174,9 +177,10 @@ summary(lisbon_route_100m_xyz$slope)
```

```{r}
col_idx <- cut(abs(lisbon_route_100m_xyz$slope),
breaks = slope_breaks, labels = FALSE, include.lowest = TRUE)
plot(st_geometry(lisbon_route_100m_xyz),
col = slope_colors[7:12][cut(abs(lisbon_route_100m_xyz$slope) * 100,
breaks = slope_breaks, labels = FALSE, include.lowest = TRUE)],
col = slope_colors[col_idx],
lwd = 3, main = "Slope by 100 m segments"
)
```
Expand Down
Loading